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

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 register a
  • getregtype('a') reads register a type metadata
  • setreg('q', ..., ...) writes both content and type into register q

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 with setreg() while preserving type

Next

How do I run an Ex command on exactly the current visual selection without typing the range manually?