Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: use incremental search for file replace

Tags:

replace

vim

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

like image 898
Gijs Avatar asked Oct 13 '11 12:10

Gijs


2 Answers

The easiest way to do that is to do a search like normal, using 'incsearch' to help ensure the pattern is matching what you want. Once you've got that nailed down, you can either

  • Leave out the search pattern in :%s//bar/. When there's no specified search pattern, the current value of the / register is used, which will be the search you just did.
  • Insert the search pattern into the :s command using Ctrl+r/ (see :help c_ctrl-r) or Ctrl+rCtrl+o/ (if the search contains control characters, like ^H). This is useful if you want to make some final tweaks to the pattern or if you want to have it in your command history so you can reuse it even after performing another search.
like image 162
jamessan Avatar answered Oct 23 '22 15:10

jamessan


You could add c at the end of your command like that:

:%s/foo/bar/c

It will walk through all the instances of foo and ask for your confirmation (that's what the c stands for) before changing it to bar.

It's not exactly what you are after though.

like image 42
romainl Avatar answered Oct 23 '22 16:10

romainl