Copy, delete, then paste in Vim

15

3

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.

tdh87

Posted 2011-12-23T14:53:47.420

Reputation:

Answers

16

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)

Kristo

Posted 2011-12-23T14:53:47.420

Reputation: 614

10

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.

Chirayu Shishodiya

Posted 2011-12-23T14:53:47.420

Reputation:

this is nice because you don't have to think about intermediate registers to yank into. it just works. – Jon Schoning – 2012-02-08T17:14:15.040

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

Eric Bock

Posted 2011-12-23T14:53:47.420

Reputation: 171

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.

Jason Down

Posted 2011-12-23T14:53:47.420

Reputation: 1 641

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.

Michael Krelin - hacker

Posted 2011-12-23T14:53:47.420

Reputation: 626