Interactive search/replace regex in Vim?

Solution 1:

Add the flag c (in the vim command prompt):

:%s/old/new/gc

will give you a yes/no prompt at each occurrence of 'old'.

"old" is highlighted in the text; at the bottom of the window it says "replace with new (y/n/a/q/l/^E/^y)?)"

Vim's built-in help offers useful info on the options available once substitution with confirmation has been selected. Use:

:h :s

Then scroll to section on confirm options. Screenshot below:

Text that says "[C] Confirm each substitution. [...] CTRL-Y to scroll the screen down"

For instance, to substitute this and all remaining matches, use a.

Solution 2:

Mark Biek pointed out using:

%s/old/new/gc

for a global search replace with confirmation for each substitution. But, I also enjoy interactively verifying that the old text will match correctly. I first do a search with a regex, then I reuse that pattern:

/old.pattern.to.match
%s//replacement/gc

The s// will use the last search pattern.