How do I copy a register to another register while preserving its character/line/block type?
Answer
:call setreg('q', getreg('a'), getregtype('a'))
Explanation
Simple register copies can silently change behavior when register type is lost. For example, a blockwise register pasted with plain assignment may no longer behave as a block paste. Using setreg() with getreg() and getregtype() copies both the register contents and its type (v, V, or blockwise), so later p/P operations behave exactly as expected.
How it works
getreg('a')reads the text stored in registeragetregtype('a')reads registeratype metadatasetreg('q', ..., ...)writes both content and type into registerq
This is particularly useful when moving complex macros or blockwise yanks between registers without re-recording.
Example
Copy register a into q with full fidelity:
:call setreg('q', getreg('a'), getregtype('a'))
Now @q runs the same macro behavior that @a had, and if a was linewise or blockwise text, "qp retains the same paste semantics.
Tips
- Use this to create safe backups before editing a macro register
- Works with unnamed and numbered registers too (
'"','1', etc.) - For programmatic macro pipelines, build text with
getreg(), then write withsetreg()while preserving type