Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can vim keep the content of register when pasting over selected text?

Tags:

vim

copy-paste

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?

like image 937
Didier Trosset Avatar asked May 23 '12 15:05

Didier Trosset


People also ask

How do registers work in Vim?

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.

How do I copy and paste in Vim without comments?

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.

How do I paste contents in Vim?

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.


2 Answers

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

like image 92
pb2q Avatar answered Oct 05 '22 03:10

pb2q


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.

like image 22
romainl Avatar answered Oct 05 '22 03:10

romainl