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?

  • 4
    I realise this is the Vi(m) SE site, but 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
  • @MichaelColes Yes, but not all Vim users have sed on their computers. – Rich Mar 4 '15 at 7:54
  • @MichaelColes Very nice. It's always nice to know of alternatives. Not everyone here might agree, but I'd upvote that as an answer. – Christopher Bottoms Mar 4 '15 at 12:10
  • @Rich: fair point :) – Michael Coles Mar 4 '15 at 12:12
  • 1
    @ChristopherBottoms I might too, especially if the answer pointed out the relation between sed and vi, and noted the similarities in some of the things you can do with them. – Rich Mar 4 '15 at 14:40

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.


Not the answer you're looking for? Browse other questions tagged or ask your own question.