How do I inspect a register as a list of lines in Vimscript?
Answer
:echo getreg('a', 1, 1)
Explanation
For advanced register debugging and macro tooling, plain getreg('a') is often not enough. It returns a flat string, which hides line structure and makes scripted transforms awkward. Using the extra arguments to getreg() lets you retrieve register data as a List of lines, which is much safer for analysis and programmatic edits.
How it works
:echo getreg('a', 1, 1)
- First argument
'a': target register - Second argument
1: request the expression-friendly form - Third argument
1: return a List instead of a single String
This makes multiline yanks, macros, and blockwise content easier to inspect without manual splitting.
Example
Assume register a contains three yanked lines. Running:
:echo getreg('a', 1, 1)
prints something like:
['first line', 'second line', 'third line']
From there, you can map or filter lines before writing back with setreg(), instead of applying brittle regex substitutions to one long string.
Tips
- Compare with
:echo getreg('a')to see the difference in shape - Pair with
getregtype('a')when preserving char/line/block behavior - Use this pattern when building reusable Vimscript commands that transform register content