How do I paste the contents of a register into the command line or search prompt?
Answer
:<C-r>"
Explanation
When typing an Ex command or search pattern, you often need to insert text you've already yanked or deleted. Instead of retyping it, press <C-r> followed by a register name to paste that register's contents directly into the command line. This works in both the : command prompt and the / or ? search prompt.
How it works
<C-r>"— pastes the unnamed (default) register, which holds the last yanked or deleted text<C-r>a— pastes registera<C-r>0— pastes register0(last yank)<C-r>/— pastes the last search pattern<C-r>%— pastes the current filename<C-r>*— pastes from the system clipboard
Example
Suppose you yank a variable name myVariable with yiw, then want to search and replace it:
:%s/<C-r>0/newVariable/g
This inserts myVariable directly into the substitute command without retyping it.
Tips
<C-r><C-w>inserts the word under the cursor into the command line — this does not use a register but is equally powerful- Use
<C-r><C-a>to insert the WORD (including special characters) under the cursor - This also works in insert mode for pasting registers without leaving insert mode