How do I copy one register to another while preserving its exact register type?
Answer
:call setreg('b', getreg('a'), getregtype('a'))
Explanation
Plain register copies like :let @b = @a move text, but they can lose important metadata about register type. In Vim, a register can be characterwise, linewise, or blockwise, and that type directly changes how p and P behave. If you want reliable replay behavior, copy both the value and the register type.
setreg() with getregtype() is the safest way to duplicate complex registers, especially blockwise snippets and recorded transformations that depend on shape. This is useful when you keep stable templates in one register and clone them into temporary registers for editing.
How it works
:call setreg('b', getreg('a'), getregtype('a'))
getreg('a')reads the full content of registeragetregtype('a')reads its type (v,V, or blockwise form)setreg('b', ..., ...)writes both content and type into registerb
Example
Before:
Register a: blockwise column edit payload
Register b: empty
Run:
:call setreg('b', getreg('a'), getregtype('a'))
After:
Register b now pastes with the same blockwise shape as register a
Tips
- Use this pattern in mappings to avoid subtle paste-shape bugs
:echo getregtype('a')is useful for debugging strange paste behavior- If you only need raw text and not type,
:let @b = @ais still faster to type