Let's say I have the word "the" in my vim clipboard. If I enter visual block, select a word (let's say this word is "a") and paste. The word "a" is replaced by the word "the" by my clipboard has changed : it now contains the word "a". How can I change this behavior to not replace my clipboard when pasting in visual block?
-
Hi @bob - I suggest reading up a bit on vim registers; This might clear up some of the confusion. Vim maintains a bunch of its own "clipboards" which it calls registers. One of these registers will always contain the last text you deleted - I believe this is the behaviour you are seeing. – Lix Jan 16 '20 at 14:10
-
There are some good details and references in [this post](https://stackoverflow.com/questions/54255/in-vim-is-there-a-way-to-delete-without-putting-text-in-the-register) – Lix Jan 16 '20 at 14:11
-
2Add the value of `:verbose set clipboard?` – Matt Jan 16 '20 at 17:18
2 Answers
The documentation is quite explicit about visual mode paste. The deleted contents will always go to the unnamed register (default register.)
If you want to paste the same text repeatedly, the recommendation is to use a named register instead. Yank "the" using "xyw
to yank it into register "x", then paste it using "xp
in visual mode. Register "x
won't be touched by the operation.
Note that the numbered register "0
can be really useful here, since a default yank also goes to the "0
register, but the text replaced with a put in visual mode only goes to the unnamed register and doesn't modify "0
.
See :help v_p
:
The previously selected text is put in the unnamed register. If you want to put the same text into a Visual selection several times you need to use another register. E.g., yank the text to copy, Visually select the text to replace and use
"0p
. You can repeat this as many times as you like, the unnamed register will be changed each time.
Another option is to override that behavior by creating custom mappings.
You can create mappings for the p
and P
operations in Visual mode that will save and restore the unnamed register before and after the operation.
These functions and mappings will do that:
function! SaveReg(cmd) abort
let b:saved_reg = getreg('"')
let b:saved_type = getregtype('"')
return a:cmd.":\<C-U>call RestxyzoreReg()\r"
endfunction
function! RestoreReg() abort
call setreg('"', b:saved_reg, b:saved_type)
unlet b:saved_reg
unlet b:saved_type
endfunction
xnoremap <silent> <expr> p SaveReg('p')
xnoremap <silent> <expr> P SaveReg('P')

- 8,522
- 2
- 16
- 32
-
1This (ve"0p) does work but I can not use dot (.) to repeat the command. It just removes the selected text but does not paste again from the yank register. Do you know how to make that work? – eclipse Feb 08 '21 at 21:58
-
1@eclipse Yeah, `.` will not work because it's two separate commands, first `ve` to do a visual selection, then `"0p` to put from register 0. To make it repeatable, you could, for example, record a macro with it. – filbranden Feb 08 '21 at 22:44
See :help registers
. You can yank/put text to specified registers.
Case in point, just yank the word "the" with "by
, which puts it in the register b
. Then, you can :put
it with "bp
.

- 442
- 2
- 7