ex commands enable you to switch between multiple files. The advantage is speed. When you are sharing the system with other users, it takes time to exit and re-enter 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, and you keep so that you can copy text from one file to another.
When you first invoke vi, you can name more than one file to edit, and then use ex commands to travel between the files:
vi file1 file2
This edits file1 first. After you have finished editing the
first file, the ex command
:w writes (saves) file1
:n calls in the next file (file2). You can even
vi * to edit all the files in a directory. Type
:f to get the name of your current file;
:args lists all filenames from the command line and puts
brackets around the
You can also switch at any time to another file that you didn't
specify on the command line with the ex command
you want to edit another file within vi, you first need to save
your current file (
:w), then you can type the following command:
vi "remembers" two filenames at a time as the current and
These can be referred to by the symbols
% (current filename)
# (alternate filename).
# is particularly useful with
:e, since it allows you to
switch easily back and forth between two files.
:e# is always
"switch to the other one."
(On some systems, the vi command
(control-caret) is a synonym for
If you have not first saved the current file, vi will not allow
you to switch files with
:n unless you tell it
imperatively to do so by adding an exclamation point after the command.
is also useful. It discards your edits and returns to the last saved version of the current file.
In contrast to the
% is useful mainly when writing out the
contents of the current buffer to a new file.
For example, you could
save a second
version of the file letter with the command:
- from O'Reilly & Associates' Learning the vi Editor, Chapter 5