Given a text with multiple occurrences of the word foo, I would like to do an interactive search through the text, and upon the Nth occurrence of foo, edit it.

I can only find the search and replace patterns for this kind of action, but the point is that I do not want to edit all occurrences of foo, just the one I found in some context.

5 Answers 5


You can use c flag when doing search and replace:


Each time vim found foo, it will prompt yes/no to confirm replacing or not.

Or if you want to search for the nth occurrence of foo, you can:


Then vim will jump to nth occurrence of foo, so you can decide to replace or not.

  • Thanks, but as per my comment to @Thushi, I am looking for an interactive way of doing this.
    – Steen
    Jan 21, 2015 at 9:47
  • @Steen: What do you mean interactive way? The above method do it interactively.
    – cuonglm
    Jan 21, 2015 at 9:49
  • I mean interactive as when just casually browsing the file via /{search-phrase}, pressing n (or p) repeatedly and then stumbling over some occurrence of 'foo' that should not be there or be something else. When browsing a file, I just /foo and n for some file without knowing in advance whether or not I am going to be replacing stuff.
    – Steen
    Jan 21, 2015 at 9:54
  • Thanks for the update, but with your solution I get either/or. It is exactly when "so you can decide to replace or not" happens, that I would like to press e.g. ciw to replace that occurrence of the word. I don't want to first search for a word, n-ing through the file, finding an occurrence I don't like, esc-ing and then doing ::%s/foo/bar/gc to step through the file once more.
    – Steen
    Jan 21, 2015 at 10:08

If I read you correctly:

  1. Search


  2. Next by n

  3. “Oh, this one I want to delete”
    • dw
  4. Continue by n
  5. “Oh, this one I want to change to bar
    • cwbarEsc
  6. Continue by n ...
  7. “Oh, this one I want to change to baz
    • cwbazEsc
  8. Continue by n ...

Edit / Correction:

For convenience I have added:

imap <C-d> <ESC>

to .vimrc - had forgotten about that and thought it was a default mapping. Corrected.

  • Yes, exactly! I would like to add that you need to do enter when you would like to make the "normal" operations (cw or the like) to the found occurrence.
    – Steen
    Jan 21, 2015 at 10:10
  • 1
    @Steen: Updated.
    – user367890
    Jan 21, 2015 at 11:28

For the normal search and replace option in vim you can use the confirmation option by passing gc for example.

For example if you want to replace the word foo with bar then you add the below line


So wherever it finds the occurrence of the word foo it asks for the confirmation for replacement of word bar.If you press y it will be get replaced and n for no.

  • Thanks, but I'm looking for a solution when just casually browsing the file via /{search-phrase}, pressing n repeatedly and then stumbling over some occurrence of 'foo' that should not be there or be something else. I have already found the answers when I up-front know that I'm going to (possibly or most likely) be replacing some text.
    – Steen
    Jan 21, 2015 at 9:46

you can use a global search 'g' to qualify which lines you should substitute on


to further restrict you can even specify markers

  • Thanks for answering, but please see the accepted answer for the correct interpretation of my question.
    – Steen
    Jan 21, 2015 at 13:59

You can repeat a lot of commands in vim by typing first the number how often they should be repeated. This also works for the n command.

To change e.g. the 9th occurrence of foo type

  1. type /fooEnter to jump to the first location of foo
  2. type 8n to jump eight times to the the next location. Then you are are on the ninth occurrence
  3. type cw to change the word and type in the new word.

You can continue the steps 2. and 3. as described in this answer

  • Thank you for the answer, but in this case I don't know in advance how many (if any) ns there are.
    – Steen
    Jan 23, 2015 at 12:03

You must log in to answer this question.

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