Skip to content

Instantly share code, notes, and snippets.

@dahu
Created March 10, 2015 09:26
Show Gist options
  • Select an option

  • Save dahu/10858ec9ccd9a572bf90 to your computer and use it in GitHub Desktop.

Select an option

Save dahu/10858ec9ccd9a572bf90 to your computer and use it in GitHub Desktop.
Restore buffer position when switching between buffers
" Barry Arthur, March 2015
" Restore buffer position when switching between buffers
function! SaveBufPos()
let b:tline = line('w0')
endfunction
function! RestoreBufPosTrigger()
if exists('b:tline')
augroup RestoreBufPos
au!
au CursorHold,CursorMoved <buffer> call RestoreBufPos()
augroup END
endif
endfunction
function! RestoreBufPos()
exe 'normal! ' . (b:tline + &scrolloff) . 'Gzt'
normal! ``
augroup RestoreBufPos
au!
augroup END
endfunction
autocmd BufLeave * call SaveBufPos()
autocmd BufEnter * call RestoreBufPosTrigger()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment