Vim Tips Wiki
Register
No edit summary
(I added &diff which fixes the issue with diff mode noted by John Backett.)
Line 18: Line 18:
 
" When switching buffers, preserve window view.
 
" When switching buffers, preserve window view.
 
if v:version >= 700
 
if v:version >= 700
au BufLeave * let b:winview = winsaveview()
+
au BufLeave * if !&diff | let b:winview = winsaveview() | endif
au BufEnter * if exists('b:winview') | call winrestview(b:winview) | endif
+
au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) | endif
 
endif
 
endif
 
</pre>
 
</pre>
Line 32: Line 32:
   
 
==Comments==
 
==Comments==
  +
As noted by [[User:JohnBackett|JohnBackett]] the autocommands should not be run in diff mode, this is accomplished with the "&diff" if test - Sep 21, 2012
Need a way to turn this off when viewing a diff. When scrolling through a diff showing two similar files side-by-side, it is convenient to jump to the other window by typing Ctrl-W w (or by clicking with the mouse). When this tip is in effect, both of these techniques result in a very irritating vertical scroll of the two windows. [[User:JohnBeckett|JohnBeckett]] 23:35, May 30, 2011 (UTC)
 
  +
   
 
This tip results in a [http://vim.1045645.n5.nabble.com/Cursor-anomoly-apparent-and-actual-positions-differ-td5673900.html display bug] that hasn't been patched yet, and there's no mention of a vimscript workaround. - Sep 21, 2012
 
This tip results in a [http://vim.1045645.n5.nabble.com/Cursor-anomoly-apparent-and-actual-positions-differ-td5673900.html display bug] that hasn't been patched yet, and there's no mention of a vimscript workaround. - Sep 21, 2012

Revision as of 21:24, 21 September 2012

Tip 1375 Printable Monobook Previous Next

created 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 * if !&diff | let b:winview = winsaveview() | endif
  au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) | endif
endif

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

See also

Comments

As noted by JohnBackett the autocommands should not be run in diff mode, this is accomplished with the "&diff" if test - Sep 21, 2012


This tip results in a display bug that hasn't been patched yet, and there's no mention of a vimscript workaround. - Sep 21, 2012