More vi Magic

More vi Magic

If we need to, we can also start vi toedit multiple files. This is done like this:

vi file1 file2 file3

Once we are editing, we can switch between files with :n for the next file and :p for the previous one. Keep in mind that the file names do not wrap around. In other words, if we keep pressing :n and get to file3, doing it again does not wrap around and bring me to file1. If we know the name of the file, we can jump directly there, with the ex-mode edit command, as in

:e file3

The ability to edit multiple files has another advantage. Do you remember those numbered and named buffers? They are assigned for a single instance of vi, not on a per-file basis. Therefore, you can delete or yank text from one file, switch to the next and then insert it. This is a crude but effective cut and paste mechanism between files.

If you switch to editing another file (using :n or :r), reload an original file (using :rew! or :e!), the contents of the deletion buffers are preserved so that you can cut and paste between files. The contents of all buffers are lost, however, when you quit vi.