Vim: use incremental search to replace file

I know that the incsearch parameter controls how a search in vim is highlighted as you type. I would like to have the same incremental search and highlight when using replace ( :%s/foo/bar/ )

+4
source share
2 answers

The easiest way to do this is to do a search as usual using 'incsearch' to ensure that the pattern matches what you want. Once you have a nail, you can either

  • Leave the search pattern in :%s//bar/ . If there is no specified search pattern, the current value of the register / , which will be performed only by you.
  • Paste the search pattern into the command :s using Ctrl + r / (see :help c_ctrl-r ) or Ctrl + r Ctrl + o / (if the search contains control characters such as ^H ). This is useful if you want to make some final adjustments to the template or if you want to have it in your command history so that you can reuse it even after doing another search.
+9
source

You can add c at the end of your command:

 :%s/foo/bar/c 

It will go through all instances of foo and ask for confirmation (which means c ) before changing it to bar .

This is not exactly what you need.

+5
source

All Articles