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

Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 4.2 Options When Starting vi  Chapter 4
Beyond the Basics
Next: 4.4 Marking Your Place

4.3 Making Use of Buffers

You have seen that while you are editing, your last deletion ( d or x ) or yank ( y ) is saved in a buffer (a place in stored memory). You can access the contents of that buffer and put the saved text back in your file with the put command ( p or P ).

The last nine deletions are stored by vi in numbered buffers. You can access any of these numbered buffers to restore any (or all) of the last nine deletions. (Small deletions, of only parts of lines, are not saved in numbered buffers, however.) These deletions can only be recovered by using the p or P command immediately after you've made the deletion.

vi also allows you to place yanks (copied text) in buffers identified by letters. You can fill up to 26 (a-z) buffers with yanked text and restore that text with a put command at any time in your editing session.

4.3.1 Recovering Deletions

Being able to delete large blocks of text at a single bound is all very well and good, but what if you mistakenly delete 53 lines that you need? There is a way to recover any of your past nine deletions, for they are saved in numbered buffers. The last delete is saved in buffer 1, the second-to-last in buffer 2, and so on.

To recover a deletion, type <"> (double quote), identify the buffered text by number, then give the put command. To recover your second-to-last deletion from buffer 2, type:


The deletion in buffer 2 is placed after the cursor.

If you're not sure which buffer contains the deletion you want to restore, you don't have to keep typing " n p over and over again. If you use the repeat command (.) with p after u , it automatically increments the buffer number. As a result, you can search through the numbered buffers as follows:


to put the contents of each succeeding buffer in the file one after the other. Each time you type u , the restored text is removed; when you type a dot (.), the contents of the next buffer is restored to your file. Keep typing u and . until you've recovered the text you're looking for.

4.3.2 Yanking to Named Buffers

You have seen that you must "put" ( p or P ) the contents of an unnamed buffer before you make any other edit, or the buffer will be overwritten. You can also use y and d with a set of 26 named buffers (a-z) which are specifically available for copying and moving text. If you name a buffer to store the yanked text, you can place the contents of the named buffer at any time during your editing session.

To yank into a named buffer, precede the yank command with a double quote ( " ) and the character for the name of the buffer you want to load. For example:

Yank current line into buffer d.

Yank next seven lines into buffer a.

After loading the named buffers and moving to the new position, use p or P to put the text back:

Put the contents of buffer d before cursor.

Put the contents of buffer a after cursor.

There is no way to put part of a buffer into the text - it is all or nothing.

In the next chapter, you'll learn to edit multiple files. Once you know how to travel between files without leaving vi , you can use named buffers to selectively transfer text between files.

You can also delete text into named buffers using much the same procedure. For example:

Delete five lines into buffer a.

If you specify a buffer name with a capital letter, your yanked or deleted text will be appended to the current contents of that buffer. This allows you to be selective in what you move or copy. For example:


Delete from cursor to end of current sentence and save in buffer z.


Move two sentences further on.


Add the next sentence to buffer z. Note that you can continue adding more text to a named buffer for as long as you like - but be warned: if you once forget, and yank or delete to the buffer without specifying its name in capitalized form, you'll overwrite the buffer, losing whatever you previously accumulated in it.

Previous: 4.2 Options When Starting vi  Learning the vi Editor Next: 4.4 Marking Your Place
4.2 Options When Starting vi Book Index 4.4 Marking Your Place

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