Vim Tips Wiki
(Adjust previous/next navigation)
(rewrite and rough merge in from tip 281)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=182
 
|id=182
 
|previous=181
 
|previous=181
 
|next=184
 
|next=184
|created=December 17, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Demian L. Neidetcher
+
|author=
|version=5.7
+
|version=6.0
 
|rating=24/18
 
|rating=24/18
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
  +
When jumping around in a large file, you may like to keep the cursor line in the middle of the window (vertically centered). To do this, enter the command:
I hope i don't hear a collective 'DUH!' from around the world but i just did this and i think it's kinda cool.
 
  +
<pre>
 
 
:set so=999
In your [[vimrc]] add:
 
  +
</pre>
   
  +
To restore normal behavior, enter:
 
<pre>
 
<pre>
  +
:set so=0
map j jzz
 
map k kzz
 
 
</pre>
 
</pre>
   
 
The <tt>'scrolloff'</tt> option (abbreviated as <tt>'so'</tt>) sets how many lines of context are maintained above and below the cursor. If you set this to a large value, the cursor will stay in the center of the screen when possible.
So whenever you go up or down, Vim does that and then re-centers. Obviously it doesn't work when you page up/ down.
 
   
  +
Alternatively, you might want vertical centering only following some commands, such as moving the cursor down/up with <tt>j</tt> and <tt>k</tt>. To achieve that, you could remap the keys:
==Comments==
 
  +
<pre>
This might work, but it isn't necessary to remap your keys to get this effect. Vim has a built-in option for this called 'scrolloff' (or 'so' for short). This sets how many lines of context are maintained above and below the cursor. If you set this to a very big number, your cursor will stay in the center of the screen when it can.
 
  +
:nnoremap j jzz
  +
:nnoremap k kzz
  +
</pre>
   
  +
==Rough merge in from tip 281==
set so=9999
 
  +
Do you find yourself hitting 'zz' all the time in order to see some context of what you're currently working on? If so, then this tip might be for you. If you add the following line in your vimrc, you can toggle zz mode by pressing <Leader>zz.
  +
<pre>
  +
" maintain a constant zz state, second call will toggle it back off
  +
map <Leader>zz :let &scrolloff=999-&scrolloff<CR>
  +
</pre>
   
  +
==See also==
Try it and see what happens. It's also compatible with PgUp/Down, arrow keys, ^B/^F, and other scrolling commands.
 
  +
*[[Make search results appear in the middle of the screen]]
   
  +
==References==
----
 
  +
*{{help|'scrolloff'}}
The "set so=9999" setting works better, but it renders "H", "M", "L" useless which is a pain
 
  +
*{{help|zz}}
   
 
==Comments==
----
 

Revision as of 09:35, 25 February 2010

Tip 182 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


When jumping around in a large file, you may like to keep the cursor line in the middle of the window (vertically centered). To do this, enter the command:

:set so=999

To restore normal behavior, enter:

:set so=0

The 'scrolloff' option (abbreviated as 'so') sets how many lines of context are maintained above and below the cursor. If you set this to a large value, the cursor will stay in the center of the screen when possible.

Alternatively, you might want vertical centering only following some commands, such as moving the cursor down/up with j and k. To achieve that, you could remap the keys:

:nnoremap j jzz
:nnoremap k kzz

Rough merge in from tip 281

Do you find yourself hitting 'zz' all the time in order to see some context of what you're currently working on? If so, then this tip might be for you. If you add the following line in your vimrc, you can toggle zz mode by pressing <Leader>zz.

" maintain a constant zz state, second call will toggle it back off
map <Leader>zz :let &scrolloff=999-&scrolloff<CR>

See also

References

Comments