Skip to content

Instantly share code, notes, and snippets.

@marcotrosi
Last active November 15, 2019 01:38
Show Gist options
  • Save marcotrosi/7e14cfbf290d83a92a6d50c01f29d687 to your computer and use it in GitHub Desktop.
Save marcotrosi/7e14cfbf290d83a92a6d50c01f29d687 to your computer and use it in GitHub Desktop.
function and command to edit registers.
function! EditReg(reg)
let l:EditRegWinNum = bufwinnr("__EDITREG__")
if l:EditRegWinNum == -1
silent! belowright new __EDITREG__
setlocal modifiable
put! =getreg(a:reg)
$d_
setlocal noshowcmd
setlocal buftype=nofile
setlocal bufhidden=wipe
setlocal noswapfile
setlocal nowrap
setlocal nobuflisted
setlocal nonumber
setlocal nospell
execute 'setlocal statusline=editing\ register\ \"'.a:reg.'\ \|\ <CR>\ to\ confirm\ changes\ \|\ <ESC>\ or\ q\ to\ discard\ changes'
setlocal noruler
execute 'normal! z'.line('$').'ggG'
startinsert!
endif
execute 'nnoremap <buffer> <silent> <CR> :call setreg("'.a:reg.'", getline(1,"$")) <BAR> silent! bwipeout! __EDITREG__<CR>'
execute 'inoremap <buffer> <silent> <CR> <ESC>:call setreg("'.a:reg.'", getline(1,"$")) <BAR> silent! bwipeout! __EDITREG__<CR>'
nnoremap <buffer> <silent> <ESC> :silent! bwipeout! __EDITREG__<CR>
nnoremap <buffer> <silent> q :silent! bwipeout! __EDITREG__<CR>
autocmd BufLeave <buffer> :silent! bwipeout! __EDITREG__
endfunction
command! -nargs=1 EditReg call EditReg(<f-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment