vi editing  «Prev  Next»
Lesson 8Editing multiple files
ObjectiveEdit multiple files within the same vi session.

Editing multiple Files using vi

Editing multiple files offers many conveniences. Your :map, :ab, or :set commands will be in effect for all files in your session. Also, your search patterns will be remembered between files. This means the "repeat search" commands, N and n, will work across files. Most importantly, a multi-file vi session will let you copy and paste text from one file to another. To edit several files within a single vi session, you supply vi with any number of file names as arguments:
% vi file1 file2 file3 You also can use wildcards to specify file names. For example, suppose you have five files: memo1, memo2, and so on. To open them all for editing, you could enter:

% vi memo?

vi displays only one file at a time and you can edit only one file at a time. However, you can switch to a different file in several ways:
:n Go to the next file
:e file Edit the specified file
:e# Edit the previous file
:rew Rewind and return to the first file

Before you can switch to another file, you must save any changes in the current file by entering the :w command. Ifyou want to quit vi before you have visited every file, you must enter :q twice.
The following SlideShow shows some of the ways that vi lets you travel between files:
1) Traverse Files 1 2) Traverse Files 2 3) Traverse Files 3 4) Traverse Files 4 5) Traverse Files 5 6) Traverse Files 6 7) Traverse Files 7 8) Traverse Files 8 9) Traverse Files 9
  1. By entering the UNIX command vi recipe quartet, you can edit both files within the same vi session
  2. vi displays the first file, recipe
  3. To go to the next file, enter the :n command.
  4. Now you are in the quartet file, as confirmed by the status line.
  5. If you try to use :n again, the message "No more files to edit
  6. The :rew command starts the list over, rewinding to the first file.
  7. After rewinding the list, vi reports that there are 2 files to edit and that you are currently in the recipe file
  8. Now use the :q command to quit vi.
  9. Instead of quitting, vi report that there is (1 more file to edit)

Switching between Files within vi Session
In the next lesson, you will learn how to paste text from one file to another.