If I type /regex then the cursor temporarily jumps to the first match and all matches are highlighted. This updates as I type. Is it possible to get this behavior when I'm getting ready to make a substitution? For instance, when I'm working on a complicated regular expression :%s/\<regex\>/, I would like to know what is matching before I pull the trigger and change everything. Any way to do it?

  • It would be nice if it would work in a way it works with the search: highlighting what currently matches. Did you find the best way to do it? Commented Jan 27, 2015 at 12:59

3 Answers 3


Type your search using / (or ?) to begin with, which will allow you to tweak your regex (since you're using incsearch)

Once you get it all nice, you can use the previous search pattern (the one you just used) in the substitute command by using two separators for the 'search' item:

/elephant                       " find the next elephant
:%s//rhino/gc                   " make then all rhinos!

Of course, you can use selections and ranges, as normal.

  • +1 for teaching me about s//
    – Steven Lu
    Commented Jul 17, 2013 at 15:36

One option is that you can have a confirmation flag c.


:h s_flags:

[c] Confirm each substitution.  Vim highlights the matching string.  You can type:              
        'y'     to substitute this match
        'l'     to substitute this match and then quit ("last")
        'n'     to skip this match
        <Esc>   to quit substituting
        'a'     to substitute this and all remaining matches
        'q'     to quit substituting

You can also try grep command to see the filtered output and the highlighted original file.

:g/re/p or :g/re/

  • I like it, but it still require me to complete the regular expression before I see what if anything was matched. Commented Jun 12, 2012 at 17:41

Well, the short way to see what your regular expression will match is doing a search with / using your desired regex. It will jump to the first match and mark it up while your're typing it. Try it - vim will not disappoint you.

  • True, though that's what I stated in the question. I wish it would update as I'm getting ready to substitute. With your solution I have to try the regex and then copy it (how?) and then use it in a substitution. That's not good enough. Commented Jun 13, 2012 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .