How do I change whether a register pastes as character, line, or block-wise?
Answer
:call setreg('"', @", 'l')
Explanation
Vim registers carry not just their text content but also a type: charwise (c), linewise (l), or blockwise (b). This type controls how p and P paste the contents. Using setreg(), you can change a register's type after yanking — turning a charwise yank into a linewise paste without re-yanking.
How it works
setreg(register, content, type)sets a register's value and type@"reads the current content of the unnamed register'l'forces linewise mode;'c'for charwise;'b'for blockwise- The third argument can also be
'b42'to specify column width for a block
Example
You yanked a word with yiw (charwise), but want to paste it on its own line:
:call setreg('"', @", 'l')
Now pressing p inserts the text on a new line below the cursor instead of inline.
Conversely, if you yanked a full line with yy (linewise) but want to paste it inline without the newline:
:call setreg('"', @", 'c')
Tips
- Use
getregtype('"')to inspect the current type of a register before modifying it - Works with any named register:
setreg('a', @a, 'b')makes registerablockwise - Combine with a mapping to toggle linewise/charwise on the fly before pasting
- In Neovim,
vim.fn.setreg()exposes the same API from Lua