vimtricks.wiki Concise Vim tricks, one at a time.

How do I keep the cursor line always centered on the screen while scrolling?

Answer

:set scrolloff=999

Explanation

By default, Vim only scrolls the viewport when the cursor reaches the very top or bottom of the screen. Setting scrolloff to a very large value like 999 forces Vim to keep the cursor line vertically centered at all times, so the text scrolls around a fixed cursor position.

How it works

The scrolloff option controls the minimum number of screen lines to keep above and below the cursor. When set to 999, Vim will always try to keep 999 lines above and below — which is impossible on any real screen, so it centers the cursor instead.

  • :set scrolloff=999 — always keep the cursor centered
  • :set scrolloff=5 — keep at least 5 lines of context above and below
  • :set scrolloff=0 — default behavior, scroll only at edges

Example

Before (default scrolloff=0):

line 1
line 2
line 3
line 4
█ line 5    <-- cursor at bottom edge, viewport has scrolled

After :set scrolloff=999:

line 3
line 4
█ line 5    <-- cursor stays centered
line 6
line 7

Tips

  • Toggle between centered and default with a mapping: :nnoremap <Leader>zz :let &scrolloff=999-&scrolloff<CR>
  • This affects all vertical cursor movements: j, k, <C-d>, <C-u>, search results, and more
  • Combine with :set sidescrolloff=999 for horizontal centering when nowrap is set

Next

How do I use PCRE-style regex in Vim without escaping every special character?