Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recall search pattern when writing replace regex pattern in Vim?

Tags:

regex

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 regex 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.

like image 699
Tom Morris Avatar asked May 29 '10 14:05

Tom Morris


1 Answers

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.

  1. Using an empty search:

    :%s//replace/g

  2. 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).

like image 150
Tom Morris Avatar answered Sep 26 '22 00:09

Tom Morris