VIM Editor

The vi and vim editors allow 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:
1) Press the ESC key to ensure you are in vi Command mode.
2) Place the cursor on the line you wish to copy.
3) Type yy to copy the line.
4) Move the cursor to where you wish to insert the copied line.
5) 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:

1) Press the ESC key to ensure you are in vi Command mode.
2) Place the cursor on the first line of the text you wish to copy
3) Type 12yy to copy the 12 lines.
4) 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
“any
“ap
“aP
Vi only retains the last deleted or copied item in the general buffer on most Linux installations. 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:

1) Press the ESC key to ensure you are in vi Command mode.
2) Place the cursor on the first line of the text you wish to copy.
3) Type “f12yy to copy the 12 lines into a buffer named f.
4) Continue editing your file. You do not need to insert the copied lines immediately.
5)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:
a) Type “fp to insert the lines copied into buffer f after the current line on which the cursor is resting.
b) 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

Using 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, is 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.
“addDelete the current line and place it in a buffer named a.
“and
d
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.
“apPut the contents of the buffer immediately after the current line.
“aPPut the contents of the buffer immediately before the current line.