neumachen
6/26/2019 - 12:33 AM

A simple yankring. A much needed improvement on the numbered registers.

A simple yankring. A much needed improvement on the numbered registers.

function! yankring#reset() abort
  let s:yankring = []
  for l:i in range(1, 9)
    execute 'let l:contents = @' . l:i
    call add(s:yankring, l:contents)
  endfor
endfunction
call yankring#reset()

function! yankring#yank(contents) abort
  call insert(s:yankring, join(a:contents, "\<C-j>"))
  while len(s:yankring) > 9
    call remove(s:yankring, -1)
  endwhile
  call yankring#sync()
endfunction

function! yankring#sync() abort
  messages clear
  for l:n in range(len(s:yankring))
    execute 'let @' . l:n . ' = "' . escape(s:yankring[l:n], '"') . '"'
  endfor
endfunction

augroup yankring
  autocmd!
  autocmd TextYankPost * if empty(v:event['regname']) | call yankring#yank(v:event['regcontents']) | endif
augroup END