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
maybe do this:
:noautocmd vim /Search/ **/*
:set hidden
:cfirst
qa
:%s//Replace/gce
:cnf
q
1000@a
:wa
Explanation:
:noautocmd vim /Search/ **/*
⇒ lookup (vim
is an abbreviation for vimgrep
) pattern in all files in all subdirectories of the cwd without triggering autocmds (:noautocmd
), for speed's sake.:set hidden
⇒ allow having modified buffers not displayed in a window (could be in your vimrc):cfirst
⇒ jump to first search resultqa
⇒ start recording a macro into register a:%s//Replace/gce
⇒ replace all occurrences of the last search pattern (still /Search/
at that time) with Replace
:
g
flag)c
flag)e
flag):cnf
⇒ jump to next file in the list created by the vim
commandq
⇒ stop recording macro1000@a
⇒ play macro stored in register a 1000 times:wa
⇒ save all modified buffers* EDIT * Vim 8 way:
Starting with Vim 8 there is a better way to do it, as :cfdo
iterates on all files in the quickfix list:
:noautocmd vim /Search/ **/*
:set hidden
:cfdo %s//Replace/gce
:wa
I've decided to use ack and Perl to solve this problem in order to take advantage of the more powerful full Perl regular expressions rather than the GNU subset.
ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'
ack is an awesome command line tool that is a mix of grep
, find
, and full Perl regular expressions (not just the GNU subset). Its written in pure Perl, its fast, it has syntax highlighting, works on Windows and its friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep
.
Xargs is an old unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacemnt/g'
command.
Perl is a programming language. The -p option causes Perl to create a loop around your program which iterates over filename arguments. The -i option causes Perl to edit the file in place. You can modify this to create backups. The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution. For more information on Perl command line options perldoc perlrun. For more information on Perl see http://www.perl.org/.
1. :grep <search term> (or whatever you use to populate the quickfix window)
2. :cfdo %s/<search term>/<replace term>/g | update
Step 1 populates the quickfix list with items you want. In this case, it's propagated with search terms you want to change via grep
.
cfdo
runs the command following on each file in the quickfix list. Type :help cfdo
for details.
s/<search term>/<replace term>/g
replaces each term. /g
means replace every occurrence in the file.
| update
saves the file after every replace.
I pieced this together based upon this answer and its comments, but felt it deserved its own answer since it's all in one place.
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)
One more option in 2016, far.vim plugin: