Wonders of vim: Multiple file editing

Joel Hammer Joel
Mon May 17 11:55:18 PDT 2004


Just to share the joy of vim, which is the vi loaded with many distros.

I had to edit about 30 documents, converted from .doc via staroffice to
text files. StarOffice did the conversion perfectly. Each had a similar
format as .doc files, and I needed to make changes in the newly created
text files so that they could be converted into html files of a particular
format. In the past, I had to edit each file individually, or run sed
scripts from the command line, which always involved a big hassle in
saving the new file to a different name, then moving them back again.

Now, I find that the command vi *.txt loads all 30 documents at once.

There are a few commands for handling multiple files at one time which we
all should know:
buf! BufferID 
switches to the buffer desired. You can use the buffer number or the
first, unique part of the file name.
:buf! MyFi or
:buf! 2
:ls   lists them all
:bufdo!   runs a command on all of them, for example:
:bufdo! 1,$ ! sed "s/PATTERN/REPLACE/"
If you have a complicated sed script:
:bufdo! 1,$ ! sed -f script

This is really magic.

If you mess up, a very likely occurrence:
:bufdo! u
changes everything back.

You can try out the sed command or script on just one document, then use the 
:bufdo! command to run it against them all.

:wall saves everything.

:help buffers is very useful.
Navigate all the links in help with CNTRL-] 

Enjoy.

Joel
 


More information about the Linux-users mailing list