Learn Practice Daily golf Reference

How to paste in Vim without losing what you copied

Paste with "0p instead of p: register 0 holds your last yank, and deletes never touch it. Or keep the unnamed register as it is: delete with "_d, or paste over a selection with Visual P (Vim 9.0 and later, and Neovim).

Why the second paste goes wrong

p pastes from the unnamed register, and that register is refilled by every yank and every delete: dd, x, ciw, and pasting over a selection, which deletes the selected text. So you yank a word, select a placeholder and paste over it, and it works; select the next placeholder, paste again, and you get the placeholder you just replaced.

The yank isn't lost. Vim keeps your last yank in register 0 as well, and deletes never write there.

Try it here

Fill both placeholders

Yank DEFAULT_TITLE with yiw, go down to the first TODO (j, fT) and replace it: viw selects it and p pastes over it. The register now holds TODO, so for the second one paste from register 0: j, b, viw, "0p.

1 of 2Yank DEFAULT_TITLE and paste it over the first TODO
1const DEFAULT_TITLE = 'Untitled';
1page.title = page.title || TODO;
2tab.title = tab.title || TODO;
NORMALLoading the editor…1/20.0s1:7

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
"0p / "0PPaste your last yank after / before the cursor, whatever you deleted since.
viw, then "0pReplace a word with your last yank, as many times as you like.
viw, then PThe same in Vim 9.0 and later, and Neovim: Visual P leaves the registers alone.
"_dd / "_diw / "_xDelete without changing any register (the black hole register).
"_ciwChange a word without saving the old one.
Ctrl-r0In Insert mode: type out your last yank, for example after ciw.
"ayy, then "apKeep a copy in register a, which only you write to.
:reg ↵Show what every register holds.

The black hole register

Register _ is a black hole: whatever you delete into it is gone, and no register changes. Put "_ in front of any delete or change, "_dd or "_ciw, when you know you won't want that text back, and p still pastes what you yanked before. (Gone from the registers only: u still undoes the delete.)

Visual P

Since Vim 9.0, and in Neovim, P in Visual mode replaces the selection with the register like p does, but doesn't put the replaced text in the unnamed register. So you can yank once and replace any number of selections with viw and P. In older versions of Vim, Visual P works like Visual p, and "0p is the way.