Vim Tips Wiki
No edit summary
 
(Some clean up)
Line 6: Line 6:
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Yakov Lerner
 
|author=Yakov Lerner
|version=n/a
+
|version=7.0
 
|rating=97/28
 
|rating=97/28
 
|text=
 
|text=
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 (especially with <tt>Ctrl-^</tt> and <tt>:bp</tt>), vim might change the *visual* line position. Even when vim preserves the line number relative 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 <tt>:bn :bp</tt> current line becomes center line of the screen (but still line 1234 relative to file).
   
 
If your line number is restored correctly but the shift in screen view bothers you, you can use following autocommands to restore screen view exactly when switching buffers:
   
  +
<pre>
 
" 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
  +
</pre>
   
 
This requires vim7.
If your line number is restored correctly but the shift in screen view bothers you, you
 
   
 
Another possibility is to use <tt>:set scrolloff=999</tt> (which keeps current line vertically centered).
can use following autocommands to restore screen view exactly when switching buffers:
 
 
Check out also {{Help|id='nosol'}} (not needed if the above autocommands are used).
 
 
 
" 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
 
   
 
If you have problem with restoration of file-relative line numbers, then check out tip [[VimTip80]], {{Help|id='viminfo'}}, and {{Help|id=last-position-jump}}
   
 
}}
 
}}

Revision as of 10:45, 15 August 2007

Previous TipNext Tip

Tip: #1375 - Avoid scrolling when switch buffers

Created: October 31, 2006 16:42 Complexity: intermediate Author: Yakov Lerner Version: 7.0 Karma: 97/28 Imported from: Tip#1375

When switching buffers (especially with Ctrl-^ and :bp), vim might change the *visual* line position. Even when vim preserves the line number relative 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).

If your line number is restored correctly but the shift in screen view bothers you, you can use following autocommands to restore screen view exactly when switching buffers:

" 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

This requires vim7.

Another possibility is to use :set scrolloff=999 (which keeps current line vertically centered). Check out also :help 'nosol' (not needed if the above autocommands are used).

If you have problem with restoration of file-relative line numbers, then check out tip VimTip80, :help 'viminfo', and :help last-position-jump

Comments