2

I usually want to replace some content in vim. After copy, I select the content to be replaced, then paste content I copied. But then the content to be replaced will be copied to clipboard, and I can't replace next place. I hope content in clipboard don't change when I paste on some selected place.

K F
  • 645
  • 1
  • 6
  • 16
  • Does this answer your question? [How can vim keep the content of register when pasting over selected text?](https://stackoverflow.com/questions/10723700/how-can-vim-keep-the-content-of-register-when-pasting-over-selected-text) – jdhao Oct 12 '20 at 13:07
  • Does this answer your question? [How to paste over without overwriting register](https://stackoverflow.com/questions/290465/how-to-paste-over-without-overwriting-register) – Cyrus Yip Aug 07 '22 at 16:37

2 Answers2

8

Short answer: "0p (instead of p)

This question sort of has an answer here, but I will add more details.

The :registers, command will show you what the registers are currently storing.
(use :help registers for more information)

Explanation:

Using the double quote (") before a yank or put command tells vim you want to use a specific register. The command "xp (where x is a number (0-9), or a letter (a-z)) says you want to put the data that was stored in register x. In your case, you want to retrieve from register 0, because that was what you most recently yanked. (Every yank you do automatically fills register 0 with the text, as well as the unnamed register that is used by default with each yank and put)

You could also choose a named register to store your data in. I will choose letter 'h', for example. To yank: "hy. To put: "hp.

Now if you use the :registers (or :reg) command, you will see that you have text stored in your "h register.

(Here is a question that explains registers very well)

Community
  • 1
  • 1
Thomas F
  • 1,869
  • 3
  • 24
  • 25
-1

I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324