Last active
September 10, 2024 08:36
-
-
Save AndrewRadev/fe4aaaccf5e0b278b5acdc089e104ab4 to your computer and use it in GitHub Desktop.
Set 'scrolloff' to a fraction based on terminal size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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 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
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):