Tuesday 26 November 2019

How do I replace-paste yanked text in vim without yanking the deleted lines?


So I usually find myself copying text from one point to another while overwriting old text where the new is pasted:


blah1
newtext
blah2
wrong1
blah3
wrong2
blah4

Suppose I visual-mark newtext and yank it. Now I select wrong1 (which could be anything, not necessarily just a word) and paste the newtext. However, if I now do the same with wrong2 it will be replaced with wrong1 instead of newtext.


So how do I keep the text that is in the buffer from being swapped with the text that I am currently overwriting?


Edit 1


Although I quite like the reigister suggestions (I think I will start using registers more, now that I discovered the :dis command), I am going with a modification of jinfield's answer, because I do not use the swapping mode.


vnoremap p "0p
vnoremap P "0P
vnoremap y "0y
vnoremap d "0d

does the trick perfectly.


Edit 2


I was too fast; romainl's solution is precisely what I was looking for, without the hack in Edit 1.
Actually, vnoremap p "_dP is enough!
So, changing accepted answer.



Answer



I have these mappings in my .vimrc:


" delete without yanking
nnoremap d "_d
vnoremap d "_d

" replace currently selected text with default register
" without yanking it
vnoremap p "_dP

"_ is the "blackhole register", according to :help "_:


"When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned. {not in Vi}"


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...