Fandom

Vim Tips Wiki

Substitute last search

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 1501 Printable Monobook Previous Next

created 2007 · complexity basic · author Jerome · version n/a


When doing a complex substitution with a regular expression, it's useful to first search for what you want to replace, to confirm that the search pattern works. Or, just press * to search for the current exact word, followed by `` (two backticks) if you want to jump back to your original word.

To do the substitute, type:

:%s//New String/g

In this substitute, the search pattern is empty, so it will use the last search.

Alternately, you can press Ctrl-r followed by / to insert the last-search register (for example, you might want to edit the command before pressing Enter):

:%s/<c-r>//New String/g

If the pattern is in a register, say the a register, you can press Ctrl-r then a to insert the contents of the register at the appropriate place in the command.

CommentsEdit

  • why would anyone use ``? Just get accustomed to CTRL-o and CTRL-i and enjoy.
    • Because, it does almost the same thing, but is easier and faster to type if you only need to go back a single position.

Also on Fandom

Random Wiki