Search and replace in Vim across all the project files

EDIT: Use cfdo command instead of cdo to significantly reduce the amount of commands that will be run to accomplish this (because cdo runs commands on each element while cfdo runs commands on each file)

Thanks to the recently added cdo command, you can now do this in two simple commands using whatever grep tool you have installed. No extra plugins required!:

1. :grep <search term>
2. :cdo %s/<search term>/<replace term>/gc
3. (If you want to save the changes in all files) :cdo update

(cdo executes the given command to each term in the quickfix list, which your grep command populates.)

(Remove the c at the end of the 2nd command if you want to replace each search term without confirming each time)


The other big option here is simply not to use vim:

sed -i 's/pattern/replacement/' <files>

or if you have some way of generating a list of files, perhaps something like this:

find . -name *.cpp | xargs sed -i 's/pattern/replacement/'
grep -rl 'pattern1' | xargs sed -i 's/pattern2/replacement/'

and so on!

Tags:

Vim

Replace