Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Replace selection with default buffer without overwriting the buffer

Here is my problem:

I am in visual mode.

I select text and copy it to the buffer. ((y)ank)

I select another text which I want to replace and paste the buffer. ((p)aste)

Now the second selection has been replaced in the buffer, however I want the first one to still sit in there.

Any ideas?

like image 551
Juergen Riemer Avatar asked Oct 01 '10 09:10

Juergen Riemer


2 Answers

Often, this behavior is useful. When you don't want it, you can instead do the usual yank, then paste (officially, 'put') with "0p. You can do this however many times you like.

See :help v_p for more.

If you want to avoid the overwrite, you need to delete first. You can use "_ to select the blackhole buffer, then delete d, then paste before P and you'll avoid the buffer being set.

like image 54
Peter Avatar answered Sep 20 '22 19:09

Peter


While this does not technically answer the question (not using default buffer) it does solve the symptom of the problem so I thought I would still share. I get around this issue with a solution to a different problem.

I have mapped "Copy, paste" (yank, put) from the system clipboard to "Ctrl-Shift-C, Ctrl-Shift-V" (Ctrl-C, Ctrl-V if caps lock is on). This can be used in place of y with the same effect.

If I use the system buffer for copy it does not get overwritten when I paste.

I added this to my .vimrc

vnoremap <C-V> "*p  vnoremap <C-C> "*y 

As a bonus it will give you easy access to the system clipboard.

like image 36
scorch855 Avatar answered Sep 21 '22 19:09

scorch855