49

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?

4
  • You're probably already aware, but I think you can always work around the problem with pdd.
    – a3nm
    May 23, 2012 at 16:06
  • @a3nm Don't you mean pyy, that would copy again the current line? May 24, 2012 at 6:35
  • Sorry, I meant pjdd. This being said, I don't understand your suggestion.
    – a3nm
    May 24, 2012 at 8:58
  • @a3nm Well, using pyy, you first paste the content of the register, and then yanks the line you've just pasted into the register to overwrite what has been set when pasting. The problem with pdd is that it would delete what I`ve just pasted. May 24, 2012 at 14:23

2 Answers 2

43

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

3
  • 6
    Also, use :reg to look at the current register contents. This is a pretty good explanation of registers, including the black hole register, and the kill buffer registers 0-9. Finally: :help register, natch.
    – pb2q
    May 23, 2012 at 16:15
  • 3
    This is the better answer. There is no need to send deleted content to the black hole register when there is already a register made specifically to hold onto yanked text
    – rviertel
    Feb 2, 2018 at 19:22
  • @rviertel in which manner is this better? Better to type numerous keyboard keys just to get what you want? And in the end, ask yourself how often you needed replaced content? For me the answer is zero, zero times I needed replaced content
    – bora89
    May 12, 2023 at 8:34
42

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.

2
  • 1
    I went even more extream: vnoremap p "_dP
    – bora89
    May 12, 2023 at 8:40
  • You're a madman!
    – romainl
    May 12, 2023 at 9:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.