Yank, Put, and Delete Commands

 < Day Day Up > 

The vim editor has a General-Purpose buffer and 26 Named buffers that can hold text during an editing session. These buffers are useful if you want to move or copy a portion of text to another location in the Work buffer. A combination of the Delete and Put commands removes text from one location in the Work buffer and places it in another location in the Work buffer. The Yank and Put commands copy text to another location in the Work buffer, without changing the original text.

The General-Purpose Buffer

The vim editor stores the text that you most recently changed, deleted, or yanked in the General-Purpose buffer. The Undo command retrieves text from the General-Purpose buffer when it restores text.

Copying Text to the Buffer
Yank (y/Y)

The Yank command (y) is identical to the Delete (d) command except that it does not delete text from the Work buffer. The vim editor places a copy of the yanked text in the General-Purpose buffer. You can then use a Put command to place another copy of it elsewhere in the Work buffer. Use the Yank command just as you use the Delete command. The uppercase Y command yanks an entire line into the General-Purpose buffer.

tip: Use yy to yank one line

Just as d RETURN deletes two lines, so y RETURN yanks two lines. Use the yy command to yank and dd to delete the current line.

tip: D works differently from Y

The D command (page 160) does not work in the same manner as the Y command. Whereas D deletes to the end of the line, Y yanks the entire line regardless of the cursor position.

Copying Text From the Buffer
Put (p/P)

The Put commands, p and P, copy text from the General-Purpose buffer to the Work buffer. If you delete or yank characters or words into the General-Purpose buffer, p inserts them after the current character, and P inserts them before this character. If you delete or yank lines, sentences, or paragraphs, P inserts the contents of the General-Purpose buffer before the line the cursor is on, and p inserts them after this line.

Put commands do not destroy the contents of the General-Purpose buffer. Thus you can place the same text at several points within the file by using one Delete or Yank command and several Put commands.

Deleting Text Copies It into the Buffer

Any of the Delete commands described earlier in this chapter (page 160) place the deleted text in the General-Purpose buffer. Just as you can use the Undo command to put the deleted text back where it came from, so you can use a Put command to put the deleted text at another location in the Work buffer.

For example, if you delete a word from the middle of a sentence by using the dw command and then move the cursor to a SPACE between two words and give a p command, vim places the word you just deleted at the new location. If you delete a line using the dd command and then move the cursor to the line below the line where you want the deleted line to appear and give a P command, vim places the line at the new location.

optional: Named Buffers

You can use a Named buffer with any of the Delete, Yank, or Put commands. Each of the 26 Named buffers is named by a letter of the alphabet. Each Named buffer can store a different block of text so that you can recall each block as needed. Unlike the General-Purpose buffer, vim does not change the contents of a Named buffer unless you use a command that specifically overwrites that buffer. The vim editor maintains the contents of the Named buffers throughout an editing session.

The vim editor stores text in a Named buffer if you precede a Delete or Yank command with a double quotation mark (") and a buffer name (for example, "kyy yanks a copy of the current line into buffer k). You can use a Named buffer in two ways. First, if you give the name of the buffer as a lowercase letter, vim overwrites the contents of the buffer when it deletes or yanks text into the buffer. Second, if you use an uppercase letter for the buffer name, vim appends the newly deleted or yanked text to the end of the buffer. This feature enables you to collect blocks of text from various sections of a file and deposit them at one place in the file with a single command. Named buffers are also useful when you are moving a section of a file and do not want to use a Put command immediately after the corresponding Delete command, and when you want to insert a paragraph, sentence, or phrase repeatedly in a document.

If you have one sentence that you use throughout a document, you can yank that sentence into a Named buffer and put it wherever you need it by using the following procedure: After entering the first occurrence of the sentence and pressing ESCAPE to return to Command mode, leave the cursor on the line containing the sentence. (The sentence must appear on a line or lines by itself for this procedure to work.) Then yank the sentence into Named buffer a by giving the "ayy command (or "a2yy if the sentence takes up two lines). Now anytime you need the sentence, you can return to Command mode and give the command "ap to put a copy of the sentence below the line the cursor is on.

This technique provides a quick and easy way to insert text that you use frequently in a document. For example, if you were editing a legal document, you might store the phrase The Plaintiff alleges that the Defendant in a Named buffer to save yourself the trouble of typing it every time you want to use it. Similarly, if you were creating a letter that frequently used a long company name, such as National Standards Institute, you might put it into a Named buffer.


In addition to the 26 Named buffers and 1 General-Purpose buffer, 9 Numbered buffers are available. They are, in one sense, readonly buffers. The vim editor fills them with the nine most recently deleted chunks of text that are at least one line long. The most recently deleted pattern is held in "1, the next most recent in "2, and so on. If you delete a block of text and then give other vim commands so that you cannot reclaim the deleted text with Undo, use "1p to paste the most recently deleted chunk of text below the location of the cursor. If you have deleted several blocks of text and want to reclaim a specific one, proceed as follows: Paste the contents of the first buffer with "1p. If the first buffer does not have the text you are looking for, undo the paste with u and then give the period (.) command to repeat the previous command. The Numbered buffers work in a unique way with the period command: Instead of pasting the contents of buffer "1, the period command pastes the contents of the next buffer ("2). Another u and period replace the contents of buffer "2 with that of buffer "3, and so on through the nine buffers.

     < Day Day Up > 

    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net