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

How do I append text to a register instead of replacing it?

Answer

"Ayy

Explanation

The "Ayy command appends the current line to register a instead of overwriting it. By using an uppercase letter for the register name, Vim appends to the existing register contents rather than replacing them. This is one of Vim's most useful register tricks for collecting text from multiple locations.

How it works

  • "a specifies register a (lowercase) — this replaces the register contents
  • "A specifies register a using uppercase — this appends to whatever is already in register a
  • yy yanks the current line

So "ayy stores the current line in register a, and "Ayy adds the current line to what is already in register a.

Example

Given the text:

first important line
some other text
second important line
more text
third important line
  1. Move to first important line and press "ayy — register a now contains first important line
  2. Move to second important line and press "Ayy — register a now contains both lines
  3. Move to third important line and press "Ayy — register a now contains all three lines
  4. Paste with "ap to insert all three collected lines together

Tips

  • This works with any yank or delete command: "Ad2j appends the next 3 lines to register a via deletion
  • Works with all 26 named registers (az to set, AZ to append)
  • Use :registers a to inspect what is currently stored in register a
  • This technique is perfect for collecting scattered lines from a file into one place
  • Combine with the global command: :g/pattern/yank A appends every line matching a pattern to register a
  • Use qA to append to a recorded macro in register a (same uppercase convention)

Next

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