Vim Tips Wiki
No edit summary
 
(I added &diff which fixes the issue with diff mode noted by John Backett.)
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1375
 
|id=1375
  +
|previous=1373
|title=preserve screen *visual* line when switching buffers
 
  +
|next=1376
|created=October 31, 2006 16:42
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Yakov Lerner
 
|author=Yakov Lerner
|version=n/a
+
|version=7.0
 
|rating=97/28
 
|rating=97/28
  +
|category1=Usage
|text=
 
  +
|category2=
When switching buffers (especially with Ctrl-^ and :bp), vim might change the *visual* line position. Even when vim preserves the line number realtive to file, it might reposition the screen view such that, for example, before switching, current line (line 1234 of the file) was top line of the screen, after :bn :bp current line becomes center line of the screen (but still line 1234 relative to file).
 
 
}}
  +
When switching buffers using the <code>Ctrl-^</code> and <code>:bp</code> 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 (<code>:bn :bp</code>), 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:
   
  +
<pre>
 
" 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
  +
</pre>
   
  +
Alternatively a user can <code>:set scrolloff=999</code> which keeps the current line vertically centered.
If your line number is restored correctly but the shift in screen view bothers you, you
 
   
  +
==See also==
can use following autocommands to restore screen view exactly when switching buffers:
 
  +
*{{Help|'nosol'}}
  +
*[[VimTip80]]
  +
*{{Help|'viminfo'}}
  +
*{{help|last-position-jump}}
   
 
==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
   
   
  +
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
" when switching buffers, preserve window view
 
 
if v:version &gt;= 700
 
 
au BufLeave * let b:winview = winsaveview()
 
 
au BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
 
 
endif
 
 
 
 
This requires vim7.
 
 
The other possibility is to use ':set scrolloff=999' (which keeps current line
 
 
vertically centered). Check out also 'set nosol'. (not needed if above autocommands are used).
 
 
 
 
If you have problem with restoration of file-relative line numbers, then
 
 
check out tip [[VimTip80]] , and
 
 
[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:'viminfo'}} :help 'viminfo'], [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:last-position-jump}} :help last-position-jump]
 
 
 
 
Yakov
 
 
 
}}
 
 
== Comments ==
 
<!-- parsed by vimtips.py in 0.518291 seconds-->
 

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