PracticalVim Tip90 - yszheda/wiki GitHub Wiki

Table of Contents

Tip 90: Reuse the Last Search Pattern

Return to the top: <>

  • Leaving the search field of the substitute command blank instructs Vim to reuse the most recent search pattern.
  • → Decouple composing a pattern and devising a suitable replacement string.
Example:

equivalent to:

It’s Not Always Appropriate

Example: join every line of a file by replacing newlines with commas:

Implications for Command History

  • Patterns are saved in Vim’s search history, while substitute commands are saved in the history of Ex commands
  • → Decoupling the search and replacement tasks causes the two pieces of information to be placed in separate silos, which could cause difficulty if you want to reuse an old substitute command later.

References:

  • Solution: pressing `/` at the command line pastes the contents of the last search register in place.
Example:
⚠️ **GitHub.com Fallback** ⚠️