vimtricks.wiki Concise Vim tricks, one at a time.

How do I delete text without overwriting my yank register?

Answer

"_d

Explanation

The "_d command deletes text using the black hole register ("_), which discards the deleted content instead of storing it. This means your previously yanked text remains intact in the default register, ready to be pasted with p.

How it works

  • "_ specifies the black hole register — anything sent here is permanently discarded
  • d is the delete operator
  • Combine with any motion or text object: "_dw deletes a word, "_dd deletes a line, "_diw deletes the inner word

The problem this solves

By default, Vim stores deleted text in the unnamed register (""), which is the same register used by yy and y. This means if you yank a line with yy, then delete something with dd, your yanked text is overwritten and p pastes the deleted text instead.

Example

Given the text:

good line
bad line
target line

You want to replace target line with good line:

  1. Yank good line with yy
  2. Move to bad line
  3. Delete it with "_dd (black hole register — does not overwrite the yank)
  4. Move to target line and press Vp to replace it with good line

Without "_, step 3 would overwrite the yank register and p would paste bad line instead.

Tips

  • Use "_dd to delete a line without affecting registers
  • Use "_diw to delete a word without affecting registers
  • Use "_x to delete a character without affecting registers
  • An alternative approach: use the yank register "0p to paste, which always holds the last yanked (not deleted) text
  • Some users add a mapping to simplify this: nnoremap <leader>d "_d
  • The black hole register works with c (change) too: "_ciw changes a word without saving the old text

Next

How do I edit multiple lines at once using multiple cursors in Vim?