Search code examples
vimcopy-paste

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


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?


Solution

  • 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.