| Tip 90 | Reuse the Last Search Pattern |
Leaving the search field of the substitute command blank instructs Vim to reuse the most recent search pattern. We can exploit this fact to streamline our workflow.
Let’s face it: to execute a substitute command, we have to do a lot of typing. First we specify the range, then we fill out the pattern and replacement fields, and finally we append any necessary flags. That’s a lot to think about, and making a mistake in any of these fields could change the outcome.
Here’s the good news: leaving the search field blank tells Vim to use the current pattern.
Take this monolithic substitute command (from Tip 85):
| => | :%s/\v'(([^']|'\w)+)'/“\1”/g |
It’s equivalent to these two separate commands:
| => | /\v'(([^']|'\w)+)' ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access