tags:

views:

79

answers:

2

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.

+6  A: 

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

Tom Morris
Sorry for the self-answer, folks. I did have difficulty actually finding the answer so felt it was worth putting up. And Meta says it is okay to answer your own questions - http://meta.stackoverflow.com/questions/12513/should-i-not-answer-my-own-questions
Tom Morris
If you end up needing to use more than one arcane regex to sift through your eclectic rubbish, it might be useful to maintain them in variables, using `:let b:tires_sifter = @/` for one type of rubbish, and `:let b:pope_on_a_ropes_sifter = @/` later after you've figured out how to locate the instances of that entity that one can never have too many of. Pull them out by assigning them back to `@/`, building an `exec` line to use them, or using the `search()` (IIRC.. maybe `matchadd`) function.
intuited
+3  A: 

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>/

Dave Kirby