Copy, delete, then paste in Vim

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)

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.


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

Tags:

Vim