Vim. Copy & Paste

Copy & Paste are two of the most important operations when writing a text. It is easy to know why: you save a lot of time.
Vim handles copy & paste powerfully and gracefully. In Vim you can yank (copy), cut and paste text objects. There are two basic ways for selecting text: either using the normal vim commands or going into visual mode.
Let us see all of this with some examples:

This is a sample text

(Bold letters indicate the cursor position as in the previous issues.)

Now type: yw followed by ESC o
As you can expect, you have just yanked a word and opened a new line below the current one. Now, paste the copied text: in command mode, type p

This is a sample text

Easy, right?

You can do the same thing going into Visual mode (pressing v or V). Once you are in this mode, use h, j, k, l to move around and mark the selection area. When you are done, end with y.

The same procedure is used for cutting. Just replace the y command with the d command. What if you want to paste the selection above the current line? Use P when pasting the selection instead of p.
Another shortcut: use yy to yank a whole line (remember you can always use modifiers, so you can do something like 2yy to yank two lines).

Using registers
This has been easy so far. Let us be more efficient. In vim, you can use several registers to put text into.
It is as easy as doing this:"{register}{operation}. For instance, assuming we have already selected some text, we can do: "ry
And the selected text will be put into the r register.

In the following example, the word “bluetooth” is selected.

Everything is better with bluetooth.

Now, type: "ry to copy the text into “r” register.

Everything is better with bluetooth.        ESC o

And now, we paste the text with: "rp

Everything is better with bluetooth.

You can inspect the contents of these registers going into command line mode and typing registers. E.g:


--- Registers ---
""   bluetooth
"0   This is a test^JThis is another line^J
"1   Everything is better with bluetooth^Jbluetooth^J
"2   ^JThis is another line^JThis is a test^JThis is another line^J^J^J^J^JThis is a test^JThis is another line^J
"3   ^J
"4   ^J
"5   ^J
"6   ^J
"7   ^J
"8   ^J
"9   biscuit^J
"b   cookie
"f   bluetooth
"g   I'm blind^JS
"r   bluetooth
"-   test
"/   throw

Obviously, when you use the same register to cut or copy new text, the previous content is replaced with the new one. But what happens when you use the same register name but using the uppercase letter? Vim appends the new text to the content of the register. As in the example below:

This line is interesting.
This is not.
This is another interesting line.
This is completely useless.

If we want to copy only the interesting lines, we can do the following:V “fy 2j V “Fy j o ESC p.
Let us analyze that sequence: First we go into visual line mode and copy the text into the f register. Then we move to the next interesting line and do the same, but this time we use the F register character, so that we append the second line to the f buffer preserving the previous content. Then, we open a new line below the last one and paste on it the content of the f register.

I know the sequence can seem overwhelming, but you rest assured that it will become second-nature to your fingers once you have used those commands for few days. As an exercise it could be interesting to inspect the content of the registers before and after appending some text (using the :registers command).

One very special register is *. It is used to copy from/to the system’s clipboard. It can be used to paste in Vim some text copied from gEdit. It is used as the other registers.

As always, use these features as much as possible to get used to them.


Vim Sheet (III)

  • y: copy text. (yw copies a word, for example)
  • yy: copy this line
  • d: cuts the content and puts it into the clipboard
  • p: paste the content of the clipboard
  • ” register y: yanks the selected text into register. Example: “ty
  • ” register p : pastes the content of the register.
  • ” REGISTER y: adds the selection to the contents of the register (it does not delete the previous content)

1 Comment

  • On 10.25.11 stepan said:

    Useful article

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields