Using vi buffers to copy lines
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:
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.
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
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.
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:
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
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:
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.
|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.|
|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.|