3

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.

4

If I read you correctly:

  1. Search

    /fooEnter

  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.

2
  • 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 '15 at 10:10
  • 1
    @Steen: Updated. – user367890 Jan 21 '15 at 11:28
5

You can use c flag when doing search and replace:

:%s/foo/bar/gc

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:

n/foo

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

4
  • Thanks, but as per my comment to @Thushi, I am looking for an interactive way of doing this. – Steen Jan 21 '15 at 9:47
  • @Steen: What do you mean interactive way? The above method do it interactively. – cuonglm Jan 21 '15 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 '15 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 '15 at 10:08
1

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

:%s/foo/bar/gc

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.

1
  • 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 '15 at 9:46
0

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

:g/qualifyingregexp/s/foo/bar/igc

to further restrict you can even specify markers

:'a,'bg/qualifyingregexp/s/foo/bar/igc
1
  • Thanks for answering, but please see the accepted answer for the correct interpretation of my question. – Steen Jan 21 '15 at 13:59
0

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

1
  • 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 '15 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.