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 discardeddis the delete operator- Combine with any motion or text object:
"_dwdeletes a word,"_dddeletes a line,"_diwdeletes 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:
- Yank
good linewithyy - Move to
bad line - Delete it with
"_dd(black hole register — does not overwrite the yank) - Move to
target lineand pressVpto replace it withgood line
Without "_, step 3 would overwrite the yank register and p would paste bad line instead.
Tips
- Use
"_ddto delete a line without affecting registers - Use
"_diwto delete a word without affecting registers - Use
"_xto delete a character without affecting registers - An alternative approach: use the yank register
"0pto 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:"_ciwchanges a word without saving the old text