vimtricks.wiki Concise Vim tricks, one at a time.

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

  • "a tells Vim to use register a for the next yank, delete, or paste operation
  • y{motion} yanks text defined by the motion
  • Together, "ayiw yanks the inner word into register a
  • "ap pastes the contents of register a
  • Each named register is independent — yanking into "b does not affect "a

Example

Given the text:

const first = "alpha";
const second = "beta";
const third = "gamma";

Yank three different values into three registers:

  1. Cursor on alpha"ayi" yanks alpha into register a
  2. Cursor on beta"byi" yanks beta into register b
  3. Cursor on gamma"cyi" yanks gamma into register c

Now you can paste any of them anywhere:

  • "ap pastes alpha
  • "bp pastes beta
  • "cp pastes gamma

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: "Ayy appends the current line to whatever is already in register a
  • "ayy yanks the entire current line into register a; "ay3j yanks the current line and the next 3 lines
  • Use :reg a b c to 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-z are shared with macro recording — recording a macro with qa stores keystrokes in register a, so be careful not to accidentally overwrite a register you're using for yanks
  • Develop a personal convention, like always using "s for snippets and "d for discarded deletes, to keep your registers organized

Next

How do I edit multiple lines at once using multiple cursors in Vim?