I often use :line
to jump to a line number. How can I automatically center that line vertically after jumping? Essentially the same as running :150zz
.
1 Answer
It looks like "centering" depends on the length of the jump with the rough ratio (window lines / jump lenght) == 0.625
:
If
- you have 45 window lines and you jump more than 69 lines or
- you have 22 window lines and you jump more than 33 lines or
- you have 15 window lines and you jump more than 23 lines
then cursor would be centered in window, otherwise not.
I am not sure if there is an option that you can use to change "jump center size", but set scrolloff=999
makes your cursor almost always be centered.
Another option would be to map command line <CR>
to zz
after a range:
cnoremap <expr> <CR> (getcmdtype() == ":" && getcmdline() =~ '\d\+') ? "<CR>zz" : "<CR>"
-
Hmm. Made sense to me when I thought it was determined by the target line being visible or not. This ratio driven formula...not so much. Wonder what the rationale is behind it.– B LayerMay 27, 2021 at 1:04
-
@Maxim Kim Shouldn't part of the comparison in your answer be
getcmdline() =~ '^\d\+$'
, i.e. with the^
and$
anchors in the regexp, instead of justgetcmdline() =~ '\d\+'
?– dsimicJan 12 at 14:46
set scrolloff=998
, but that has other effects (I prefer more like 5 or so).-u NONE
or--clean
I get cursor line centered as long as the jump is to a line not currently on screen. You might try that too (vim --clean filename
)...it may indicate that your current config has something preventing the behavior I describe.