How to recall search pattern when writing replace regex pattern in Vim?
Here's the scenario: I've got a big file filled with all sorts of eclectic rubbish that I want to regex. I fiddle around and come up with a perfect search pattern by using the / command and seeing what it highlights.
Now I want to use that pattern to replace with. So, I start typing :%s/ and I cannot recall what the pattern was. Is there some magical keyboard command that will pull in my last search pattern here? If I'm writing a particularly complex regex, I have even opened up a new MacVim window, typed the rege开发者_JS百科x from the first window into a buffer there, then typed it back into the Vim window when writing the replace pattern. There has got to be a better way of doing so.
Found my own answer after having written up the question: Vim tips lists it as substitute last search and can be done in one of two ways.
Using an empty search:
:%s//replace/g
By pressing Ctrl + r then / to recall the material from the search buffer (/ - you can use any other named buffer by substituting the / for the letter representing that buffer).
There is a /
register that contains the current search pattern, so you can insert the search pattern into whatever you are typing (either in insert mode or on the command line) with <CTRL-R>/
Another option: after entering the pattern with <CTRL-R>/
, edit the command line with <CTRL-F>
.
:help cmdwin
精彩评论