Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active September 10, 2024 08:36
Show Gist options
  • Save AndrewRadev/fe4aaaccf5e0b278b5acdc089e104ab4 to your computer and use it in GitHub Desktop.
Save AndrewRadev/fe4aaaccf5e0b278b5acdc089e104ab4 to your computer and use it in GitHub Desktop.
Set 'scrolloff' to a fraction based on terminal size
" Reimplementation of https://github.com/tonymajestro/smart-scrolloff.nvim
" Place in `~/.vim/plugin/`
"
" Note that the value is based on `&lines`, which is the global number of
" lines available to Vim. If you'd like to scale scrolloff based on window
" size, you could use `line('w$') - line('w0')`. In that case, you'd update
" `&l:scrolloff`, the window-local option.
"
" See `:help 'scrolloff'`, `:help 'lines'`, and `:help getpos()` for more
" details.
if !exists('g:scrolloff_fraction')
" Set to 0 to disable. Change value and resize any window to update the
" global `scrolloff` value.
let g:scrolloff_fraction = 0.2
endif
augroup smart_scrolloff
autocmd!
autocmd BufRead,BufNewFile,WinResized *
\ if g:scrolloff_fraction > 0 |
\ let &scrolloff = float2nr(floor(&lines * g:scrolloff_fraction)) |
\ endif
augroup END
@vladimiroff
Copy link

This works great. I've only added BufRead and BufNewFile in the list of events so that it kicks-in without having to resize first (e.g. toggle really quick a filelist, quickfix or something):

-  autocmd WinResized *
+  autocmd BufRead,BufNewFile,WinResized *

@AndrewRadev
Copy link
Author

@vladimiroff Yeah, I think I did the same while testing, I kept this event, because that's the one used in the original project, but I'll update it for usefulness' sake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment