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

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.3 Mice vs. vi Chapter 30
vi Tips and Tricks
Next: 30.5 Edits Between Files

30.4 Editing Multiple Files with vi

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 yank buffers ( 30.5 ) 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 and :n calls in the next file ( file2 ). You can even say vi * to edit all the files in a directory. Type CTRL-g or :f to get the name of your current file; :args lists all filenames from the command line and puts brackets around the [ current ] file.

You can also switch at any time to another file that you didn't specify on the command line with the ex command :e . If 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 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. The command :e# is always "switch to the other one." (On some systems, the vi command CTRL- ^ (control-caret) is a synonym for :e# .)

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.

The command:


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

In contrast to the # symbol, % 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:

:w %.new

instead of:

:w letter.new

- LL from O'Reilly & Associates' Learning the vi Editor , Chapter 5

Previous: 30.3 Mice vs. vi UNIX Power Tools Next: 30.5 Edits Between Files
30.3 Mice vs. vi Book Index 30.5 Edits Between Files

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System