How do I set a minimum number of lines to keep visible above and below the cursor?
Answer
:set scrolloff=5
Explanation
The scrolloff option keeps a minimum number of lines visible above and below the cursor when scrolling. Setting it to 5 means Vim will scroll the viewport to always show at least 5 lines of context.
How it works
:set scrolloff=5keeps 5 lines above and below the cursor visible- When the cursor gets within 5 lines of the window edge, Vim scrolls
- Setting it to a very large number (e.g., 999) effectively keeps the cursor centered
Example
With scrolloff=5, scrolling down with j will start scrolling the window when the cursor is 5 lines from the bottom, rather than waiting until it reaches the last visible line.
Tips
:set scrolloff=999keeps the cursor always centered vertically:set sidescrolloff=5does the same for horizontal scrolling- The default is 0 (no offset)
- Abbreviation:
:set so=5