How do I force a register to be linewise before I put it in Vim?
Answer
:call setreg('a', @a, 'V')
Explanation
When you paste from a register, Vim uses that register's stored type: characterwise, linewise, or blockwise. If a macro, yank, or scripted update leaves a register in the wrong type, your put operations can land in the wrong place. setreg() lets you rewrite the same text with an explicit register type so later pastes behave predictably.
How it works
setreg()writes directly to a register'a'chooses registera@areuses the current register text without changing its content'V'forces linewise type for that register- After that, commands like
"apand"aPpaste as full lines
This is especially useful after programmatic edits, macro construction, or when you've mixed charwise and linewise yanks and want stable paste behavior before repeating changes across files.
Example
Register a currently contains: "foo\nbar"
Type is characterwise, so putting it inside code can splice text mid-line.
Run:
:call setreg('a', @a, 'V')
Now a put from register a is linewise:
Current line
foo
bar
Next line
Tips
- Use
'v'to force characterwise type instead - For blockwise registers, pass a block type like
"\<C-v>4" :echo getregtype('a')is a quick way to inspect a register before and after conversion