Any way to delete in vim without overwriting your last yank?

VimVi

Vim Problem Overview


I love vim, but one common gotcha is:

  • yank a line
  • go to where you would like to paste it
  • delete what's there
  • paste your yank, only to discover that it pastes what you just deleted

Obviously the workflow is delete first, yank second. But it would be reeeeeaaaaaalllly nice if I didn't have to. Anyone have a trick for this? Does vim have a paste buffer that works well, or is there a .vimrc setting I can change?

Vim Solutions


Solution 1 - Vim

Pass to the _ register, the black hole.

To delete a line without sticking it in the registers:

"_dd

See also :help registers.

It's probably safest, if you want to paste something over and over again, to yank it into a "named" register.

"aY

Yanks a line into the a register. Paste it with "ap.

Solution 2 - Vim

Your yanked line should still be in the register 0. So do

"0p

to paste the line (and delete whenever you want)

Solution 3 - Vim

All yank and delete operations write to the unnamed register by default. However, the most recent yank and most recent delete are always stored (separately) in the numbered registers. The register 0 holds the most recent yank. The registers 1-9 hold the 9 most recent deletes (with 1 being the most recent).

In other words, a delete overwrites the most recent yank in the unnamed register, but it's still there in the 0 register. The blackhole-register trick ("_dd) mentioned in the other answers works because it prevents overwriting the unnamed register, but it's not necessary.

You reference a register using double quotes, so pasting the most recently yanked text can be done like this:

"0p

This is an excellent reference:

Solution 4 - Vim

another possibility is:

yank your lines like you would do normally

go to where you want to paste them, enter visual line mode (V)

select the lines you want to replace

hit p to paste your lines.

this also has the added benefit, that the buffer is "swapped" with the replaced contents

Solution 5 - Vim

I use the following mapping to make deleting to the black hole register a bit easier:

nnoremap R "_d

This way, dd becomes Rd and d$ becomes R$. Note that R is normally bound to enter replace mode, but I found that I never used that, so it was the easisest to remember key for a "really remove" feature.

Solution 6 - Vim

" map paste, yank and delete to named register so the content
" will not be overwritten
nnoremap d "xd
vnoremap d "xd
nnoremap y "xy
vnoremap y "xy
nnoremap p "xp
vnoremap p "xp

Solution 7 - Vim

I wrote this plugin (yankstack.vim) to solve this problem. It gives you something like Emacs's kill ring for vim. You can yank or delete multiple things, do a paste, and then cycle back and forth through your history of yanked/killed text. I find its easier than having to remember what register I yanked something into.

In my .vimrc, I have these mappings:

nmap <M-p> <Plug>yankstack_substitute_older_paste
nmap <M-P> <Plug>yankstack_substitute_newer_paste

which let me hit ALT-p or ALT-SHIFT-p to cycle back and forth through my paste history.

Solution 8 - Vim

For your specific question, as asked, couldn't you just swap the order of the last 2 steps?

  • yank line (same)
  • move to new location (same)
  • paste yanked line (was step 4)
  • delete line you don't want (was step 3)

Granted, I usually use a named register for this type of thing, but sometimes the solution is simpler than what first comes to mind.

Solution 9 - Vim

You could use registers: "<register><command>

E.g.:

This yanks a line into register a, deletes a different line, and then pastes register a

"ayy` (move) `dd"aP

Solution 10 - Vim

You can also try out the following script: ReplaceWithRegister at vim.org/scripts/

Solution 11 - Vim

I find all these key combos cumbersome. Instead, I wrote a function to toggle on and off vim's "side effect" behavior of overwriting buffers from delete operations. That way you can just toggle it off, delete freely, then toggle back on when you're done.

See my answer here: https://stackoverflow.com/a/12649560/778118

To use it just put it in your .vimrc

Solution 12 - Vim

If you are an evil user, you may consider remapping X to do the equivalent of "_d. However, perfecting the implementation was a little tricky for me. Nonetheless, I found that

(define-key evil-normal-state-map "X" 'evil-destroy)
(define-key evil-visual-state-map "X" 'evil-destroy)

(evil-define-operator evil-destroy (beg end type register yank-handler)
  "delete without yanking text"
  (evil-delete beg end type 95 yank-handler)
)

integrates very nicely. For example, typing XX will function analogously to dd, as will X$ to d$, X0 to d0, etc...

If you are curious as to how it works, "95" represents the "_ register, so it simply reroutes your call to delete as if "_ had been the register pressed.

Solution 13 - Vim

The trick is that you know you want to grab something and move, and you are using the 'lazy' first register (which gets replaced by whatever you just deleted).

You need to learn to "cut" in vim.

Before deleting, specify any register different than the " one. Tip: check out your registers with :reg

now, you select a new register by pressing " before any command (in command mode, obviously)

  1. select what you want to "cut" (or at step 2 specify a range)
  2. Change register to anything (1 here) and delete: "1d or "1x or even "1c
  3. go to new place, delete some more
  4. now you are ready to paste what you cut and stored in register 1: "1p or "1P

done. this also has the advantage of solving the usecase: delete 5 different things from one place, and each piece goes to a different destination... just put one in "1 another in "2 and so on... go to each destination and paste.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontedsuoView Question on Stackoverflow
Solution 1 - Vimdash-tom-bangView Answer on Stackoverflow
Solution 2 - Vimmb14View Answer on Stackoverflow
Solution 3 - VimWayneView Answer on Stackoverflow
Solution 4 - VimknittlView Answer on Stackoverflow
Solution 5 - VimJörn HorstmannView Answer on Stackoverflow
Solution 6 - VimPencilcheckView Answer on Stackoverflow
Solution 7 - VimmaxbrunsfeldView Answer on Stackoverflow
Solution 8 - VimDanView Answer on Stackoverflow
Solution 9 - VimDarkDustView Answer on Stackoverflow
Solution 10 - VimdmedvinskyView Answer on Stackoverflow
Solution 11 - VimMagnusView Answer on Stackoverflow
Solution 12 - VimRussellStewartView Answer on Stackoverflow
Solution 13 - VimgcbView Answer on Stackoverflow