How do I programmatically set a register with a specific type (characterwise, linewise, blockwise)?
Answer
setreg('{reg}', {val}, '{type}')
Explanation
The setreg() function sets a register's content AND its type in one call. Register type matters when pasting — linewise content pastes on its own line, characterwise pastes inline, and blockwise pastes as a rectangular block. Without setting the type correctly, a paste may behave unexpectedly.
Register types
| Type flag | Type | Pasting behavior |
|---|---|---|
'v' or 'c' |
characterwise | Pastes inline at cursor |
'V' or 'l' |
linewise | Pastes on a new line |
<C-v> or 'b' |
blockwise | Pastes as a rectangle |
Syntax
call setreg('{register}', {value}, '{type}')
Examples
Set register a to a linewise value:
:call setreg('a', 'line of text', 'l')
:put a " pastes on its own line
Set register a to characterwise:
:call setreg('a', 'inline text', 'c')
:normal "ap " pastes inline
Convert a register from characterwise to linewise:
:call setreg('a', @a, 'l')
Now "ap pastes @a on its own line, even if it was originally copied inline.
Set the clipboard register with a linewise value:
:call setreg('+', join(['line1', 'line2'], "\n"), 'l')
Tips
getreg('a')retrieves the content;getregtype('a')retrieves the type ('v','V', or"\x16"for block)- Type conversion is the main use case: if
"appastes at the wrong level, fix it withsetreg() setreg('/', @a)sets the search register — same as:let @/ = @abut can set type:let @a = 'text'always creates a characterwise register;setreg()is the only way to set other types