Interactive search/replace regex in Vim?
I think you're looking for c
, eg s/abc/123/gc
, this will cause VIM to confirm the replacements. See :help :substitute for more information.
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.
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'.
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:
For instance, to substitute this and all remaining matches, use a
.
I usually use the find/substitute/next/repeat command :-)
/old<CR>3snew<ESC>n.n.n.n.n.n.n.
That's find "old"
, substitute 3 characters for "new"
, find next
, repeat substitute
, and so on.
It's a pain for massive substitutions but it lets you selectively ignore some occurrences of old (by just pressing n
again to find the next one instead of .
to repeat a substitution).