How do I check whether a register contains characterwise, linewise, or blockwise text?
Answer
:echo getregtype('a')
Explanation
The getregtype() function returns the motion type of a register's content — whether it was yanked characterwise, linewise, or as a visual block. This determines how the text will be pasted: linewise registers insert on a new line, blockwise registers insert as a rectangular block, and characterwise registers insert inline at the cursor.
Understanding register type is essential when writing scripts or mappings that paste registers conditionally, or when you want to diagnose unexpected paste behavior.
How it works
getregtype('{register}') returns a single-character string:
'v'— characterwise (yanked withy,ye,yiw, etc.)'V'— linewise (yanked withyy,Y, orVy)"\x16{width}"— blockwise (yanked with<C-v>); the string starts with<C-V>(character 22) followed by the block width''— register is empty
Example
" Yank a line with yy, then check:
:echo getregtype('"') " → 'V'
" Yank a word with yiw, then check:
:echo getregtype('"') " → 'v'
" Use in a function to paste differently based on type:
function! SmartPaste(reg)
if getregtype(a:reg) ==# 'V'
execute 'normal! o\e"' . a:reg . 'P'
else
execute 'normal! "' . a:reg . 'p'
endif
endfunction
Tips
- Use
setreg('a', @a, 'v')to change a register's type from linewise to characterwise without changing its content - Combine with
getreg('a')to get both content and type for register inspection or manipulation - This is the programmatic way to answer the question: "why did that paste land on a new line?"