Auto-jump to last known cursor position when buffer is re-opened

2 Points

Anton Backer Anton Backer

10 years ago

Jumps to the last known position, but only when that position is deemed "sane". For instance, ignores git buffers, since they generally represent transient files such as commit messages and rebase scripts.

function! s:JumpToLastKnownCursorPosition()
    if line("'\"") <= 1 | return | endif
    if line("'\"") > line("$") | return | endif
    " Ignore git commit messages and git rebase scripts
    if expand("%") =~# '\(^\|/\)\.git/' | return | endif
    execute "normal! g`\"" |
endfunction

autocmd BufReadPost * call s:JumpToLastKnownCursorPosition()

Martínez Ortiz Saúl Axel

Martínez Ortiz Saúl Axel 6 years ago

Someone explain how can the line be less than one or more than the last line.