19

I just started with Vim yesterday and am having trouble understanding copy & paste. I understand to copy I enter visual mode, highlight and then yank. I then move to the line I wish to paste onto, but more often then not I need to delete some text from this line. I do this using x or dw. This then results in my original copy being overwritten (as x and dw seem to cut). How do I get around this?

To put it into an example

$foo = $this->foo->property;
thislineuses($foo);

So with the above code I copy $this->foo->property, I then would want to go onto line 2, delete $foo and paste. I problem is when I delete $foo from line 2 this is then what gets pasted instead of $this->foo->property.

6 Answers 6

19

Take a look at :h copy-move. The default yank or delete goes into a place called a register (a register named "). If you need to delete some text before you paste, you need to avoid overwriting register x, as you've discovered. Fortunately, you can use any other letter or number to name a different register.

  • "ayy (yank a line into register a)
  • x, dd, etc. (delete some text into the unnamed register, ")
  • "ap (paste the text from register a)
0
13

If the problem is that you merely need to replace some text with some text2, then, just highlight and yank text2. Then highlight text and press p or P to paste text2 in place of text.

A quick further guide for some common vim commands is http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know

Enjoy!

Edit: note that p pastes text after the cursor and P before the cursor.

1
  • 1
    this is nice because you don't have to think about intermediate registers to yank into. it just works.
    – 9b5b
    Feb 8, 2012 at 17:14
7

You could copy into a named register "ayw, do your delete and then paste from the named register "ap.

However, it's usually easier to just change the order you do things in. Do the paste and then do the delete, or delete and then do the copy/paste.

Alternatively, you could delete into the black hole register "_d. See https://stackoverflow.com/q/54255/70863

0
3

You have various registers you can use to hold different text values. If you'd like to save text to a different register than where the deleted text will go (i.e. from using x or d w ), than use the following:

To save text in register a:

  • press " a [yank and movement command] (saves text into register a)
  • go to where you want to paste the text
  • press " a p (paste text from register a)

You can use a register for each of the letters on the keyboard and each number. Deleting text will not use those registers, so the text will remain in the register until you end your session or manually put someting else in there.

0

You don't have to highlight the whole line if you use v (as opposed to V) visual mode. If that's what you're asking for.

If you're talking about modifying copied text, then you first copy and the erase parts.

And if you want to copy, then modify the original and then paste, Then you can yank into other register. Like "2y, then modify, then go elsewhere and "2p.

0

The register "0 is reserved for the last yanked text. Because of that, there is no need to yank into a named register:

yy would yank a line and place it into the unnamed register " as well as yank register "0

dd would delete a line and place it into " register, but keep "0 intact

"0p would paste last yanked line.

The same is true for any yank/delete commands

In insert mode you can also use Ctrl-r 0. It will also paste the content of the "0 register.

You must log in to answer this question.