Interactive search/replace regex in Vim?

RegexVim

Regex Problem Overview


I know the regex for doing a global replace,

     %s/old/new/g

How do you go about doing an interactive search-replace in Vim?

Regex Solutions


Solution 1 - Regex

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:

Text that says

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

Solution 2 - Regex

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.

Solution 3 - Regex

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.

Solution 4 - Regex

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

Solution 5 - Regex

If you just want to count the number of occurrences of 'abc' then you can do %s/abc//gn. This doesn't replace anything but just reports the number of occurrences of 'abc'.

Solution 6 - Regex

If your replacement text needs to change for each matched occurrence (i.e. not simply choosing Yes/No to apply a singular replacement) you can use a Vim plugin I made called interactive-replace.

Solution 7 - Regex

Neovim now has a feature inccommand which allows you to preview the substitution:

inccommand has two options:

  • set inccommand=split previews substitutions in a split pane
  • set inccommand=nosplit previews substitution in the active buffer

enter image description here

Image taken from: https://medium.com/@eric.burel/stop-using-open-source-5cb19baca44d Documentation of the feature: https://neovim.io/doc/user/options.html#'inccommand';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkalView Question on Stackoverflow
Solution 1 - RegexMark BiekView Answer on Stackoverflow
Solution 2 - RegexVincent ScheibView Answer on Stackoverflow
Solution 3 - RegexLogan CapaldoView Answer on Stackoverflow
Solution 4 - RegexpaxdiabloView Answer on Stackoverflow
Solution 5 - RegexAmjithView Answer on Stackoverflow
Solution 6 - RegexhwrodView Answer on Stackoverflow
Solution 7 - RegexsumekView Answer on Stackoverflow