vimtricks.wiki Concise Vim tricks, one at a time.

How do I review and approve each substitution match before it is replaced?

Answer

:s/pattern/replacement/gc

Explanation

Adding the c flag to a :s (substitute) command makes Vim confirm each match before replacing it. For each occurrence, Vim highlights the match and asks replace with ...? (y/n/a/q/l/^E/^Y). You choose for each one individually — allowing surgical, selective substitution across a range or the whole file.

The confirmation prompt

Vim pauses on each match and shows:

replace with newtext? (y/n/a/q/l/^E/^Y)
Key Action
y Replace this match
n Skip this match
a Replace all remaining matches (turns off confirm)
q Quit — stop substituting
l Replace this one, then quit ("last")
<C-e> Scroll the view up to see more context
<C-y> Scroll the view down

Common usage

Review every foobar replacement in the file:

:%s/foo/bar/gc

Confirm only in the current paragraph:

:'[,']s/oldname/newname/gc

Tips

  • Combine with i for case-insensitive confirm: :%s/pattern/replacement/gci
  • Use a to approve all remaining after you've manually reviewed the ambiguous early occurrences
  • l (last) is handy when you find the one tricky match and want to replace only it
  • Without the g flag, :s/p/r/c confirms just the first match on each line

Next

How do I run a search and replace only within a visually selected region?