Wikia

Vim Tips Wiki

Changes: Keep your cursor centered vertically on the screen

Edit

Back to page

(tweak and finish merge)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
When scrolling or searching through a large file, it can be convenient to keep the cursor line near the middle of the screen (vertically centered within the window). This tip introduces the <tt>scrolloff</tt> option, and shows another possible technique using the <tt>zz</tt> command.
+
When scrolling or searching through a large file, it can be convenient to keep the cursor line near the middle of the screen (vertically centered within the window). This tip introduces the <code>scrolloff</code> option, and shows another possible technique using the <code>zz</code> command.
   
 
==Scrolloff option==
 
==Scrolloff option==
The <tt>'scrolloff'</tt> (scroll offset) option determines the number of context lines you would like to see above and below the cursor. The following command scrolls the text so that (when possible) there are always at least five lines visible above the cursor, and five lines visible below the cursor:
+
The <code>'scrolloff'</code> (scroll offset) option determines the number of context lines you would like to see above and below the cursor. The following command scrolls the text so that (when possible) there are always at least five lines visible above the cursor, and five lines visible below the cursor:
 
<pre>
 
<pre>
 
:set scrolloff=5
 
:set scrolloff=5
 
</pre>
 
</pre>
   
The above command can be abbreviated as <tt>:set&nbsp;so=5</tt>. Entering <tt>:set&nbsp;so=0</tt> restores the default behavior so the cursor can be moved to any line in the window without scrolling.
+
The above command can be abbreviated as <code>:set so=5</code>. Entering <code>:set so=0</code> restores the default behavior so the cursor can be moved to any line in the window without scrolling.
   
 
==Centering with scrolloff==
 
==Centering with scrolloff==
Setting <tt>'scrolloff'</tt> to a large value causes the cursor to stay in the middle line when possible:
+
Setting <code>'scrolloff'</code> to a large value causes the cursor to stay in the middle line when possible:
 
<pre>
 
<pre>
 
:set so=999
 
:set so=999
Line 33: Line 33:
   
   
If you change <tt>'scrolloff'</tt> frequently, you may want to use a mapping. With the following in your [[vimrc]], and assuming the default backslash leader key, you can type <tt>\zz</tt> to toggle the value of <tt>'scrolloff'</tt> between <tt>0</tt> and <tt>999</tt>:
+
If you change <code>'scrolloff'</code> frequently, you may want to use a mapping. With the following in your [[vimrc]], and assuming the default backslash leader key, you can type <code>\zz</code> to toggle the value of <code>'scrolloff'</code> between <code>0</code> and <code>999</code>:
 
<pre>
 
<pre>
 
:nnoremap <Leader>zz :let &scrolloff=999-&scrolloff<CR>
 
:nnoremap <Leader>zz :let &scrolloff=999-&scrolloff<CR>
 
</pre>
 
</pre>
   
In an expression, <tt>&scrolloff</tt> refers to the value of the <tt>'scrolloff'</tt> option. The <tt>:let</tt> command assigns a value to <tt>'scrolloff'</tt>; that value is <tt>999-0</tt> if <tt>'scrolloff'</tt> was <tt>0</tt>, and is <tt>999-999</tt> if <tt>'scrolloff'</tt> was <tt>999</tt>.
+
In an expression, <code>&scrolloff</code> refers to the value of the <code>'scrolloff'</code> option. The <code>:let</code> command assigns a value to <code>'scrolloff'</code>; that value is <code>999-0</code> if <code>'scrolloff'</code> was <code>0</code>, and is <code>999-999</code> if <code>'scrolloff'</code> was <code>999</code>.
   
 
==Mapping wanted keys==
 
==Mapping wanted keys==
An alternative to setting <tt>'scrolloff'</tt> would be to remap some commands so that they vertically center the cursor, for example, when moving down or up with <tt>j</tt> and <tt>k</tt>. Remap the commands like this:
+
An alternative to setting <code>'scrolloff'</code> would be to remap some commands so that they vertically center the cursor, for example, when moving down or up with <code>j</code> and <code>k</code>. Remap the commands like this:
 
<pre>
 
<pre>
 
:nnoremap j jzz
 
:nnoremap j jzz

Latest revision as of 05:17, July 13, 2012

Tip 182 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


When scrolling or searching through a large file, it can be convenient to keep the cursor line near the middle of the screen (vertically centered within the window). This tip introduces the scrolloff option, and shows another possible technique using the zz command.

Scrolloff optionEdit

The 'scrolloff' (scroll offset) option determines the number of context lines you would like to see above and below the cursor. The following command scrolls the text so that (when possible) there are always at least five lines visible above the cursor, and five lines visible below the cursor:

:set scrolloff=5

The above command can be abbreviated as :set so=5. Entering :set so=0 restores the default behavior so the cursor can be moved to any line in the window without scrolling.

Centering with scrolloffEdit

Setting 'scrolloff' to a large value causes the cursor to stay in the middle line when possible:

:set so=999

To restore normal behavior, enter:

:set so=0


If you change 'scrolloff' frequently, you may want to use a mapping. With the following in your vimrc, and assuming the default backslash leader key, you can type \zz to toggle the value of 'scrolloff' between 0 and 999:

:nnoremap <Leader>zz :let &scrolloff=999-&scrolloff<CR>

In an expression, &scrolloff refers to the value of the 'scrolloff' option. The :let command assigns a value to 'scrolloff'; that value is 999-0 if 'scrolloff' was 0, and is 999-999 if 'scrolloff' was 999.

Mapping wanted keysEdit

An alternative to setting 'scrolloff' would be to remap some commands so that they vertically center the cursor, for example, when moving down or up with j and k. Remap the commands like this:

:nnoremap j jzz
:nnoremap k kzz

See alsoEdit

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki