Moving lines of text around in the file



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

Vi has simple mechanisms to select sections of text to be deleted or copied from one part of a file, and then inserted in another section.

Delete buffer

Whenever you delete text, vi actually puts the deleted text in a "delete" buffer, replacing any previous contents of that buffer. Until you give another delete command, the contents of that buffer are accessible and can be put back into the file. A "change" command is also a "delete" command in that old text is replaced by new; the old text goes into the buffer. As we have seen, the "undo" facility allows you to put those contents back where they came from. There is also a more general facility to put copies of those contents at one or more places in the file.

Yank operator

In addition to deleting text into the delete buffer with the delete or change operators, you can copy text there with the yank operator y. The yank operator has the same syntax as the delete operator d, and works exactly like the delete operator, except that the text is just copied into the delete buffer, and not deleted from its current position. Examples:

5yw (y5w) yanks (copies) the next five words into the delete buffer.
2yy yanks (copies) the next two lines into the delete buffer.

Put command

Once you have text in the delete buffer, from either the delete, change, or yank operators, you can then put a copy of the buffer contents somewhere else with the put command, p. The p command copies the current contents of the delete buffer after the cursor position.

If the current delete buffer contains only a portion of a line (e.g., five characters from a 5x command), then the buffer contents are inserted after the cursor (like an a command). If the current delete buffer contains one or more whole lines, then new lines are created after the current line to insert the text (like an o command).

P is equivalent to p, except that it inserts the copy of the delete buffer contents before the cursor position (like an i or O command).

Additional named delete buffers

There are actually multiple delete buffers.

If you do not specify which buffer to use, a "default buffer" is used by the delete, change, and yank operators to store the most recently deleted or copied text. If you do specify a buffer, then the "default buffer" is not used or changed.

This "default buffer" is divided into a set of nine named "registers" which are used automatically to store the nine previous most recent deletions, changes, or copies. They are named simply 1, 2, ..., 9. Whenever you issue a new "ordinary" delete, change, or yank command (one that does not explicitly refer to a named buffer), the deleted or copied text is put into register number 1 of the default buffer. But the previous contents of register 1 are not just discarded as the new contents go in; rather they are pushed down into the register named 2, whose contents are pushed down into the register named 3, etc., until finally the former contents of register named 9 are discarded as they are replaced with the contents pushed down from register 8. In this fashion, the nine most recent text changes are stored and available to be recovered, as described below.

In addition to the default registers whose contents are updated by every new delete, change, or yank operation, there are 26 addressable buffers with the names a through z, which you control. You decide when to put content into these buffers, or when to replace existing content of one of these buffers. Vi never automatically uses or erases these buffers. Of course, when you quit vi, the contents of all registers and buffers are erased because they are only held in memory, not on disk.

You put text into one of the addressable buffers by preceding the desired delete, change, or yank operator with a double quote mark (") and the name of the buffer. You can then insert the contents of one of these addressable buffers, or one of the default registers, anywhere in the file by preceding the "put" command with the quote mark and name of the buffer or register. The contents of any buffer are only copied by the put command. They are not erased. So the contents of the same buffer can be put into multiple locations in the file. Examples:

"a8yy yanks 8 lines, starting at the line with the cursor, into buffer number a
 
"b2dd deletes 2 lines, starting at the line with the cursor, into buffer number b
 
5x Because no named buffer was specified, this delete command uses the default buffer. Five characters, starting at the cursor, are deleted into automatic register 1, forcing its previous contents down into automatic register 2, etc., through automatic register 9.
 
"aP copies the 8 lines from buffer a to 8 new lines above the cursor
 
"bp copies the 2 lines from buffer b to 2 new lines after the cursor
 
"2p copies the contents of the default register number 2 after the current cursor position. This register would contain the deleted or copied text from the second most recent operation that did not explicitly specify one of the named buffers.

Comments or Questions?