Copy, delete, then paste in Vim

vim

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.

Best Answer

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)
Related Question