Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 5.4 Copying a File into Another File Chapter 5
Introducing the ex Editor
Next: 6. Global Replacement
 

5.5 Editing Multiple Files

ex commands enable you to switch between multiple files. The advantage to editing multiple files is speed. When 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, Advanced Editing , and you keep yank buffers so that you can copy text from one file to another.

5.5.1 Invoking vi on Multiple Files one

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

invokes 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).

Suppose you want to edit two files, practice and note.
KeystrokesResults
vi practice note
With a screen editor you can scroll the 
the page, move the cursor, delete lines, 
insert characters, and more, while seeing

Open the two files practice and note. The first-named file, practice, appears on your screen. Perform any edits.

:w
"practice" 6 lines 328 characters

Save the edited file practice with the ex command w. Press RETURN.

:n
Dear Mr.  
Henshaw:
Thank you for the prompt . . .

Call in the next file, note, with the ex command n. Press RETURN. Perform any edits.

:x
"note" 23 lines 1343 characters

Save the second file, note, and quit the editing session.

5.5.2 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.
KeystrokesResults
:w
"practice" 6 lines 328 characters

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
"letter" 23 lines 1344 characters

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 command:

:e!

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, 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, the previous line could also have been typed:

:w %.new

5.5.3 Edits Between Files

When you give a yank buffer a one-letter name, you have a convenient way to move text from one file to another. Named buffers are not cleared when a new file is loaded into the vi buffer with the :e command. Thus, by yanking or deleting text from one file (into multiple named buffers if necessary), calling in a new file with :e, and putting the named buffer into the new file, you can transfer material between files.

The following example illustrates how to transfer text from one file to another.

KeystrokesResults
"f4yy
With a screen editor you can scroll
the page, move the cursor, delete lines,
insert characters, and more, while seeing
the results of the edits as you make them

Yank four lines into buffer f.

:w
"practice" 6 lines 238 characters

Save the file.

:e letter
Dear Mr.  
Henshaw:
I thought that you would
be interested to know that:
Yours truly,

Enter the file letter with :e. Move cursor to where the copied text will be placed.

"fp
Dear Mr.  
Henshaw:
I thought that you would
be interested to know that:
With a screen editor you can scroll
the page, move the cursor, delete lines,
insert characters, and more, while seeing
the results of the edits as you make them
Yours truly,

Place yanked text from named buffer f below the cursor.

Another way to move text from one file to another is to use the ex commands :ya (yank) and :pu (put). These commands work the same way as the equivalent vi commands y and p, but they are used with ex's line addressing capability and named buffers. For example:

:160,224ya  a

would yank (copy) lines 160 through 224 into buffer a. Next you would move with :e to the file where you want to put these lines. Place the cursor on the line where you want to put the yanked lines. Then type:

:pu a

to put the contents of buffer a after the current line.


Previous: 5.4 Copying a File into Another File Learning the vi EditorNext: 6. Global Replacement
5.4 Copying a File into Another File Book Index6. Global Replacement

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


Banner.Novgorod.Ru