home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the vi EditorSearch this book

5.5. Editing Multiple Files

ex commands enable you to switch between multiple files. The advantage to editing multiple files is speed. If you are sharing the system with other users, it takes time to exit and reenter vi for each file you want to edit. Staying in the same editing session and traveling between files is not only faster for access, but you also save abbreviations and command sequences that you have defined (see Chapter 7), and you keep yank buffers so that you can copy text from one file to another.

5.5.3. Calling in New Files

You don't have to call in multiple files at the beginning of your editing session. You can switch to another file at any time with the ex command :e. If you want to edit another file within vi, you first need to save your current file (:w), then give the command:

:e filename

Suppose you are editing the file practice and want to edit the file letter, then return to practice.

Keystrokes Results
:w Figure

Save practice with w and press RETURN. practice is saved and remains on the screen. You can now switch to another file, because your edits are saved.

:e letter Figure

Call in the file letter with e and press RETURN. Perform any edits.

vi "remembers" two filenames at a time as the current and alternate filenames. These can be referred to by the symbols % (current filename) and # (alternate filename). # is particularly useful with :e, since it allows you to switch easily back and forth between two files. In the example given just above, you could return to the first file, practice, by typing the command :e #. You could also read the file practice into the current file by typing :r #.

If you have not first saved the current file, vi will not allow you to switch files with :e or :n unless you tell it imperatively to do so by adding an exclamation point after the command.

For example, if after making some edits to letter, you wanted to discard the edits and return to practice, you could type :e! #.

The following command is also useful. It discards your edits and returns to the last saved version of the current file:

:e!

In contrast to the # symbol, % is useful mainly when writing out the contents of the current buffer to a new file. For example, a few pages earlier, in the section "Renaming the Buffer," we showed how to save a second version of the file practice with the command:

:w practice.new

Since % stands for the current filename, that line could also have been typed:

:w %.new


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.