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

How do I programmatically create a blockwise register with setreg()?

Answer

:call setreg('a', "foo\nbar", 'b')

Explanation

Most register examples focus on interactive yanks, but setreg() lets you construct registers programmatically, including their type. Setting a register to blockwise mode is powerful when you need rectangular pastes that linewise or characterwise yanks cannot reproduce.

How it works

:call setreg('a', "foo\nbar", 'b')
  • setreg() writes content into a register from Vimscript
  • 'a' is the target register name
  • "foo\nbar" creates two lines of stored text
  • 'b' marks the register as blockwise (rectangular) instead of charwise/linewise

After this, pasting register a in blockwise contexts gives predictable column inserts, which is useful for templated edits, aligned prefixes, or synthetic test fixtures.

Example

Create the register:

:call setreg('a', "foo\nbar", 'b')

Then paste with "ap at a target column to insert a two-row rectangular block.

Tips

  • Use 'c' for charwise and 'l' for linewise if blockwise is not needed
  • Combine with getreg('a', 1, 1) when debugging exact stored lines
  • This is ideal inside custom commands or mappings that generate structured text

Next

How do I replay a macro many times without cluttering the jumplist?