14

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.

migrated from stackoverflow.com Dec 24 '11 at 1:56

This question came from our site for professional and enthusiast programmers.

15

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)
8

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.

  • this is nice because you don't have to think about intermediate registers to yank into. it just works. – Jon Schoning Feb 8 '12 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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy