Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

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

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

5 Answers 5

up vote 11 down vote accepted

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)
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
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

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 In vim is there a way to delete without putting text in the register?

share|improve this answer
1  
+1 for mentioning the "null" register. –  Wayne Werner Dec 23 '11 at 15:07

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.