How do you make vim scroll offset closer to the top of the page?

Viewed 1224

:set so=999 causes the cursor to stay on the middle line when possible. Is it possible to move the offset even further, like if I want it 3/4 of the way up the page?

1 Answers

:set so=999 is the extreme; the cursor will stay in the middle of the window as much as possible. So "even further" does not make sense. If you reduce the 'scrolloff' value, the cursor will be allowed "closer" to the top and bottom. If you aim for an area of ¼ on top and bottom that stays clear of the cursor, you can initialize the value to a fraction of Vim's height:

:let &scrolloff = &lines / 4

However, this will be a static value that won't be right when you split the window horizontally. As 'scrolloff' is a global value, you need to adapt it dynamically:

:autocmd VimEnter,WinEnter * let &scrolloff = winheight(0) / 4
Related