How to Copy or Delete in Vim Without Overwriting the Buffer

2022-05-05
By: O. Wolfson

How to Copy and Replace in Vim

When you need to copy and replace text in Vim, the copied text will typically overwrite the buffer, making it frustratingly unavailable for pasting. However, there are a couple of easy solutions that can help you avoid this problem.

First, it's important to note that the copy command in Vim is called "yank," and is assigned to the y key. The delete command is assigned to the d key.

Here are two ways to copy and delete text without overwriting the buffer:

  1. Copy into a named register. In this method, you can copy text into a register (designated by a letter of your choice), and then paste the copied text from that register. To do this, enter the following command to copy text to register "a":

    bash
    "ay
    

    To paste the copied text from register "a," use this command:

    bash
    "ap
    
  2. Write to the "black hole" register when deleting. This method involves writing the deleted text to a register that is not used for pasting. To do this, use the following command to delete text to the "black hole" register:

    bash
    "_dw
    

Using one of these solutions can make copying and replacing text in Vim much more efficient.