I have the same text that I want to replace in multiple files (say, I'm updating version numbers in multiple files). Even using the command history to reuse commands, it sure is tedious to open each one, search and replace in it, and then save it. Can I do just all of this in one search and replace operation on all of these files?
Let's say that you decided that you want to update the text use 5.008
to use 5.010
in multiple Perl script and Perl modules.
:arg *.pl
:argadd *.pm
:argdo %s/use 5\.008/use 5\.010/ge | update
note that you need to escape '.', since it it's the wildcard for one character.
:arg *.pl
fills the argument list with all of the files ending in .pl
in the current directory.
:argadd *.pm
adds all of the files ending in .pm
to the argument list.
:argdo %s/use 5.008/use 5.010/ge | update
Does the search and replace on each file in the argument list and saves each file for you.
See Search and Replace in Multiple Buffers for more details and variations.
-
2Also see the following Vimcasts episode: Project-wide find and replace – Peter Rincker Mar 3 '15 at 21:36
-
@PeterRincker Thanks for the link. I'd upvote that if you'd post it as an answer. – Christopher Bottoms Mar 4 '15 at 12:15
sed
is a rather lighter solution to this:sed -i 's/v0.0.2/v0.0.3/g' *.pl
– Michael Coles Mar 3 '15 at 15:26