Friday, July 08, 2005

Writing Part of a File with vi

If you should ever need to write a consecutive part of the text you are working on to another file, then vi allows to do just that. It is not that obvious, though.
Simply move the cursor to the first line you want to write out to a file and use
m a
in command mode, move the cursor to the last of the consecutive lines and use
m b
again, in command mode. To write the text between the two markers a and b to a file, use the following command:
:'a,'b w filename
If you want to keep the current filename and add an extension to it use
:'a,'b w %.backup
vi will expand the % to the name of the current file and appends .backup to it.
Easy, isn't it?