The vi editor allows you to copy text from your file into temporary buffers for use in other places in the file during your current vi work session. Each buffer acts like temporary memory, or, as some programs call it, a "clipboard" where you can temporarily store information. The simplest case would be using the vi command mode command:
u
The u (undo) command undoes the most recent thing you've done in vi. If you've deleted something by accident, the u command will restore it to your file. Whenever you delete something from a file, vi keeps a copy in a temporary file called the general buffer. You can also delete or copy lines into temporary files called named buffers that will let you reuse those lines during your current vi work session. This feature is particularly useful if you are editing a file that contains a lot of boilerplate text or repetitive code.
When you quit vi, all the buffers are emptied; they are temporary storage that only lasts for your current vi work session.
Copying lines into a buffer
To copy text into a buffer, use the vi yank command. The most common uses are outlined below.
yy
p
P
|
The vi yy command "yanks" the current line into the vi general buffer. That is, it copies the line for you to put into the file immediately.
The vi p and P commands "put" the line back into the file just after (p) or just before (P) the line on which the cursor is resting.
Directions:
- Press the ESC key to be sure you are in vi Command mode.
- Place the cursor on the line you wish to copy.
- Type yy to copy the line.
- Move the cursor to the place you wish to insert the copied line.
- Type p to insert the copied line after the current line on which the cursor is resting or type P to insert the copied line before the current line.
|
nyy
p
P
|
If you precede the yy command with a number, vi will "yank" (copy) that number of lines into the general buffer.
For example, to copy 12 consecutive lines from one part of a file to another, follow these steps:
- Press the ESC key to be sure you are in vi Command mode.
- Place the cursor on the first line of the text you wish to copy.
- Type 12yy to copy the 12 lines.
- Move the cursor to the place where you wish to insert the copied lines. lines after the current line on which the cursor is resting or type P to insert the copied line before the current line.
|
"ayy
"anyy
"ap
"aP
|
On the University's central servers, vi only retains the last deleted or copied item in the general buffer. If you wish to copy some lines for later use, you can precede the yy command with a double quote (") and a letter to copy the line or lines into a buffer that you can reuse during the current work session. Some sample commands are listed below.
"ayy |
Copy the current line into a buffer named a. |
"b7yy |
Copy 7 lines into a buffer named b. |
"bp |
Put the information in the buffer named b after the current cursor position. |
"bP |
Put the information in the buffer named b before the current cursor position. |
To copy 12 consecutive lines into a buffer named f and place those lines in another part of the file later in your work session, follow these steps:
- Press the ESC key to be sure you are in vi Command mode.
- Place the cursor on the first line of the text you wish to copy.
- Type "f12yy to copy the 12 lines into a buffer named f.
- Continue editing your file. You do not need to insert the copied lines immediately.
- When you reach the location for the copied lines, use "f before the p or P commands to insert the lines copied into the buffer named f:
- Type "fp to insert the lines copied into buffer f after the current line on which the cursor is resting.
- Type "fP to insert the lines copied into buffer f before the current line.
- If you wish, you can insert the contents of buffer f in multiple places in your file.
|
Moving lines using vi buffers
If you use the vi dd command (delete a line) instead of the yy command, you can move lines to a new location. Instead of inserting copied lines, you can delete lines from the file, place them in a buffer, and place them where you want them. The format of the commands, similar to those using yy, are summarized below.
dd
p
P
|
The vi dd command deletes the current line in the vi general buffer. If you use the vi p or P commands before making any other changes to the file, the deleted line will be put into the file just after (p) or just before (P) the line on which the cursor is resting. |
ndd
p
P
|
If you precede the dd command with a number, vi will delete that number of lines, retaining them in the vi general buffer. If you use the vi p or P commands before making any other changes to the file, the deleted lines will be put into the file just after (p) or just before (P) the line on which the cursor is resting. |
"add |
Delete the current line and place it in a buffer named a. |
"andd |
Delete n lines, beginning with the current line, and place them in a buffer named a. For example, "g6dd would delete six lines from your file but retain them in the buffer g for the rest of your current vi work session. |
"ap |
Put the contents of buffer a immediately after the current line. |
"aP |
Put the contents of buffer a immediately before the current line. |