I have a line of text I have yanked yy
. Now I want to use this text to replace lines at several other places. The trouble is that when I select V
the line to be replaced, and paste p
, the text that was selected is automatically yanked! That's what I don't want.
Changing the register does not work, because both the paste and the yank are done with the newly selected register.
What is the command to keep the content of the register when pasting over selected text?
Vim registers are spaces in memory that Vim uses to store some text or operation details. Each of these spaces has an identifier so that it can be accessed later. When we want to reference a Vim register, we use a double quote followed by the register's name.
In Vim, the primary commands for yanking (copying) and putting (pasting) are y and p . Those commands are prefered to "Right-click/Paste or Middle Click or CTRL+SHFT+V" because the text is "put" into the buffer without any special treatment.
You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.
Your original selection should remain in register 0
. So you can move through the file and paste your yanked line over other lines using: V"0p
Each time you p
over something it goes into the default register.
To work around this feature you have to use "_
, "the black hole register", before you p
. Here is a custom mapping I have in my ~/.vimrc
:
vnoremap <leader>p "_dP
It deletes the selected content and drops it in the black hole register (this means that the selected text disappears forever) and puts the content of the default register in place of the previously selected text while leaving the default register intact.
I use it often when I need to replace a loooooooong url in a few places with another looooooong url and crafting a s//
would be too cumbersome.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With