12

By searching, I've highlighted the string that I want to change. Now I want to replace only the highlighted text. I'm using n to advance to the next instance of the search string. I was thinking I could do something like ciw but that changes the whole word not just the highlighted text.

I'm familiar with :%s/old/new/g but I don't want to change all instances. Instead, I want to advance my cursor to each instance and then decide whether to change it.

3 Answers 3

16

Use the gn motion to edit the current search match. The following example will change the current match to "FooBar":

cgnFooBar

The great part about this technique is that you can repeat the change via .. Assuming the next match will be changed the same way you can just use . to repeat. Or skip a few matches via n and then use ..

Vimcasts has a nice episode on this matter: Operating on search matches using gn

For more help see:

:h gn
4
  • This is definitely the way to go when you have different replacement strings you need to use at different places. I'm not sure about the nuances of visual mode which seems to be invoked by gn, so here's a similar method that I just discovered: c//e<CR>FooBar<Esc>//<CR>
    – Jeff
    Sep 13, 2017 at 1:51
  • Is there a way to change all selections/matches at once? Basically, to refactor?
    – Geremia
    Nov 17, 2018 at 22:57
  • @Geremia, yes, :%s/oldtext/newstuff/g will change all matches of oldtext to newstuff.
    – zundarz
    Jul 30, 2019 at 16:28
  • @zundarz I don't want to do a regexp, but to replace variable names only. Instead of hitting . multiple times, I can use :%norm . (source).
    – Geremia
    Jul 30, 2019 at 17:41
7

If you have already searched for a word with /word or ?word.

You can use :%s//newword/gc to replace the word you previously searched for with newword, by leaving the search term (s//..) empty.

The c flag says that every change should be confirmed, by pressing y for yes or n for no.

Take a look at :help :s_flags for more information about the confirm flag.

1
  • 1
    I know you said to look at the help, but you might as well expressly mention you can also press l, a, and q (or <Esc>) for various ways to wrap up the confirmation cycle.
    – Jeff
    Sep 13, 2017 at 1:48
2

Method #1

You can take advantage of the gn and gN mappings to accomplish what you want. gn will search forward for the last used search term and visually select the match. gN does exactly the same thing, except it searches backwards. When combined with an operator such as c, the operator will be applied to the match, For example, if your last search string is "foo" and you type cgn, it will search forwards for the next instance of "foo", delete it, and put you in insert mode to enter the replacement text.

For more on these, see :help gn and :help gN.

Method #2

You can use the :substitute command with the c flag. This will ask you to confirm each substitution. For instance, say you wanted to replace foo with bar in the entire file, but wanted to confirm each replacement:

:%s/foo/bar/gc

For more on this, see these help topics: :help :s_flags, :help cmdline-ranges, and :help :substitute.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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