How do I yank text into a specific named register for later use?
Answer
"{register}y{motion}
Explanation
Vim has 26 named registers (a-z) that act as independent clipboards. By prefixing a yank command with "{register}, you store text in a specific register instead of the default one, letting you accumulate multiple separate snippets and paste any of them on demand.
How it works
"atells Vim to use registerafor the next yank, delete, or paste operationy{motion}yanks text defined by the motion- Together,
"ayiwyanks the inner word into registera "appastes the contents of registera- Each named register is independent — yanking into
"bdoes not affect"a
Example
Given the text:
const first = "alpha";
const second = "beta";
const third = "gamma";
Yank three different values into three registers:
- Cursor on
alpha→"ayi"yanksalphainto registera - Cursor on
beta→"byi"yanksbetainto registerb - Cursor on
gamma→"cyi"yanksgammainto registerc
Now you can paste any of them anywhere:
"appastesalpha"bppastesbeta"cppastesgamma
No matter how many deletes or yanks you do in between, the named registers retain their contents until you explicitly overwrite them.
Tips
- Use uppercase register names to append instead of replace:
"Ayyappends the current line to whatever is already in registera "ayyyanks the entire current line into registera;"ay3jyanks the current line and the next 3 lines- Use
:reg a b cto inspect the contents of specific registers - Named registers persist for the entire Vim session — they survive buffer switches and file edits
- In insert mode, paste from a named register with
<C-r>a(no need for the"prefix) - Registers
a-zare shared with macro recording — recording a macro withqastores keystrokes in registera, so be careful not to accidentally overwrite a register you're using for yanks - Develop a personal convention, like always using
"sfor snippets and"dfor discarded deletes, to keep your registers organized