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

How do I programmatically read and write Vim register contents including their type from Vimscript?

Answer

setreg()

Explanation

The setreg(reg, value, type) and getreg(reg, 1, 1) functions give you full programmatic control over registers, including their type (characterwise, linewise, or blockwise) — which :let @a = ... cannot control. This matters when constructing macros or managing yanked text where the type affects how p/P paste behaves.

How it works

" Read register contents as a list of lines
let lines = getreg('a', 1, 1)

" Write a register with explicit type:
" 'c' = characterwise, 'l' = linewise, 'b' = blockwise
call setreg('a', ['line1', 'line2'], 'l')

" Get the type of an existing register
let type = getregtype('a')  " Returns 'v', 'V', or <C-v>N

Example

Duplicate a register and transform its contents — preserving type:

function! UpperCaseRegister(reg)
  let lines = getreg(a:reg, 1, 1)
  let type  = getregtype(a:reg)
  call setreg(a:reg, map(lines, 'toupper(v:val)'), type)
endfunction

call UpperCaseRegister('a')

Now "ap pastes the register contents in uppercase, with the same linewise or characterwise behavior as the original.

Tips

  • getreg(reg, 1, 1) — the third argument 1 returns the register as a list; without it you get a flat string with \n separators, which loses linecount information for empty trailing lines
  • getregtype(reg) returns 'v' (char), 'V' (line), or '\<C-v>N' (block with width N)
  • When modifying macros, use getreg('q', 1, 1) to get raw key sequences as a list, modify, then setreg('q', modified, 'c') to write back

Next

How do I run Lua code in the context of a specific buffer without switching to it in Neovim?