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


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


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

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

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:


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


Your Answer

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

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