[lug] Little vim trick, editing multiple files

Andrew Diederich andrewdied at gmail.com
Wed Nov 30 15:35:18 MST 2005


I needed to edit a bunch of files, changing /old/path to /new/path.  I
knew there was a way to do it in vim, but couldn't remember offhand. 
Since it's cool I thought I'd share with the group.

1) Startup vim, editing the files you need.
$ vim *.xml

2) Check out the args list.  This is the list of files you're going to edit.
:args

3) Make the substitution across all files.
:argdo %s:/old/path:/new/path:ge | update

a) argdo will run the command or commands after it on every file in args.
b) by using : as the separator I can use slashes without escaping.
c) "g", of course, substitutes on every occurrence in a line, not just
the first one.  "e" ignores the error where that match isn't in a
file.
d) | separates multiple commands.
e) update writes the file if there was a change.

I hope that's helpful.

--
Andrew Diederich



More information about the LUG mailing list