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
"aspecifies registera(lowercase) — this replaces the register contents"Aspecifies registerausing uppercase — this appends to whatever is already in registerayyyanks 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
- Move to
first important lineand press"ayy— registeranow containsfirst important line - Move to
second important lineand press"Ayy— registeranow contains both lines - Move to
third important lineand press"Ayy— registeranow contains all three lines - Paste with
"apto insert all three collected lines together
Tips
- This works with any yank or delete command:
"Ad2jappends the next 3 lines to registeravia deletion - Works with all 26 named registers (
a–zto set,A–Zto append) - Use
:registers ato inspect what is currently stored in registera - This technique is perfect for collecting scattered lines from a file into one place
- Combine with the global command:
:g/pattern/yank Aappends every line matching a pattern to registera - Use
qAto append to a recorded macro in registera(same uppercase convention)