When I open a file with vim -u NONE
and source this code:
function! F() let l:savePos=getpos('.') silent normal! gg call setpos('.',l:savePos) endfunction autocmd InsertLeave * call F()
I got a strange behavior when I leave the insert mode. The setposition is sometimes changing and there is like a z-
which is executed.
You can see that if the files is greater than your window.
Can you confirm that behavior ?
My vim is 7.4
Advertisement
Answer
Generally, if you want to save and restore cursor position it is better to use winsaveview()
as it has a few other useful features.
Your code could be rewritten to
function! F() let view = winsaveview() silent normal! gg call winrestview(view) endfunction autocmd InsertLeave * call F()
Additionally, variables inside functions are automatically locally scoped so you don’t need to prefix them with l:
.