Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace in Vim across all the project files

Tags:

replace

vim

I'm looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By "project files" I mean files in the current directory, some of which do not have to be open.

One way to do this could be to simply open all of the files in the current directory:

:args ./**

and then do the search and replace on all open files:

:argdo %s/Search/Replace/gce

However, when I do this, Vim's memory usage jumps from a couple dozen of MB to over 2 GB, which doesn't work for me.

I also have the EasyGrep plugin installed, but it almost never works—either it doesn't find all the occurrences, or it just hangs until I press CtrlC. So far my preferred way to accomplish this task it to ack-grep for the search term, using it's quickfix window open any file that contains the term and was not opened before, and finally :bufdo %s/Search/Replace/gce.

I'm looking either for a good, working plugin that can be used for this, or alternatively a command/sequence of commands that would be easier than the one I'm using now.

like image 716
psyho Avatar asked Oct 12 '22 17:10

psyho


1 Answers

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!

like image 113
Cascabel Avatar answered Oct 22 '22 08:10

Cascabel