How to Copy or Delete in Vim Without Overwriting the Buffer

May 05, 2022 | O. Wolfson
vim

Copying text in Vim will write the text to buffer. Deleting text also copies. It overwrites the buffer as well. This can be frustrating when you need to do the very common task of copy + replace. The copied text is not preserved in the buffer after the delete command, therefore you cannot paste it. Find a solution to copy and delete in vim without overwriting the buffer below.

Note that the copy command is called yank in Vim, hence the key y. The delete command is d.

To copy or delete in vim without overwriting the buffer try one of the following solutions.

  1. Copy into a “named” register. Here we will name the register “a”, you can use “b” or whatever name you want. Use the following command (double quotes+a+y).
"ay

To paste from the named register you should use the following: (double quotes+a+p).

“ap
  1. Write to the “black hole” register when deleting: (double quotes+underscore+d+w).
"_dw

Using one of these solutions should make your vim copy and replace actions much more efficient.