2

I have autoread working via my .vimrc:

set autoread
au CursorHold,FocusGained,BufEnter * checktime

When vim reloads a buffer due to it having changed, it both scrolls the buffer and changes the current cursor position to a seemingly random point in the file, although occasionally it stays in the same place. I have experimented with it, but no pattern is apparent.

Is there a way to prevent this, or to save and restore the cursor position when reloading?

Alcamtar
  • 1,259
  • 9
  • 17

2 Answers2

4

You could try to save the view and then restore it:

function! Checktime() abort
    let l:winview = winsaveview()
    checktime
    call winrestview(l:winview)
endfunction

augroup Autoread
    autocmd!
    autocmd CursorHold,FocusGained,BufEnter * call Checktime()
augroup END

(untested)

romainl
  • 172,579
  • 20
  • 259
  • 291
1

Refering to this answer: to prevent the cursor moving away from its current position you can call feedkeys["lh"] (it moves the cursor to the right and back to the left, which normaly doesn't do harm when viewing a file)

:set autoread | au CursorHold * checktime | call feedkeys("lh")

respectively

:set autoread | au CursorHold,FocusGained,BufEnter * checktime | call feedkeys("lh")

(it works in my environment)

MacMartin
  • 1,936
  • 1
  • 21
  • 25