46

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?

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • You're probably already aware, but I think you can always work around the problem with `pdd`. – a3nm May 23 '12 at 16:06
  • @a3nm Don't you mean `pyy`, that would copy again the current line? – Didier Trosset May 24 '12 at 06:35
  • Sorry, I meant `pjdd`. This being said, I don't understand your suggestion. – a3nm May 24 '12 at 08: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. – Didier Trosset May 24 '12 at 14:23
  • Possible duplicate of [How to paste over without overwriting register](https://stackoverflow.com/questions/290465/how-to-paste-over-without-overwriting-register) – Kevin Ji Dec 08 '18 at 20:01

2 Answers2

42

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

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 6
    Also, use `:reg` to look at the current register contents. [This](http://blog.dreasgrech.com/2010/06/vims-black-hole-register.html) 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 '12 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 02 '18 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 '23 at 08:34
40

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.

romainl
  • 186,200
  • 21
  • 280
  • 313