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

How do I yank and paste using named registers?

Answer

"ayy ... "ap

Explanation

Named registers let you store multiple pieces of text independently. Use "a before a yank or delete command to store text in register a, then "ap to paste it later.

How it works

  • "a tells Vim to use register a for the next operation
  • yy yanks the current line
  • "ayy yanks the current line into register a
  • "ap pastes the contents of register a

Vim has 26 named registers (a through z), so you can store up to 26 different pieces of text simultaneously.

Example

  1. On line 1, type "ayy to yank line 1 into register a
  2. On line 5, type "byy to yank line 5 into register b
  3. Move to your target location
  4. Type "ap to paste from register a
  5. Type "bp to paste from register b

Tips

  • Use uppercase to append: "Ayy appends to register a instead of overwriting
  • Use :registers or :reg to see the contents of all registers
  • The " (unnamed) register always holds the last yank or delete
  • The 0 register always holds the last yank specifically (not deletes)

Next

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