Vim Tips Wiki
Advertisement
Tip 1375 Printable Monobook Previous Next

created October 31, 2006 · complexity intermediate · author Yakov Lerner · version 7.0


When switching buffers using the Ctrl-^ and :bp commands, Vim will keep the cursor on the line number where it was before switching the buffer but it might change the position of the current line relative to the screen. For example, the cursor may be in line 1234 of the file, with that line at the top of the screen. The user may switch to another buffer, then switch back (:bn :bp), and find that the current line has been repositioned to the middle of the screen.

If the line number is restored correctly, but the shift in screen view is bothersome, add the following autocommands to the .vimrc file to restore the screen view correctly:

" When switching buffers, preserve window view.
if v:version >= 700
  au BufLeave * let b:winview = winsaveview()
  au BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
endif

Alternatively a user can :set scrolloff=999 which keeps the current line vertically centered.

See Also

Comments

Advertisement