tags:

views:

76

answers:

4

So I have read :help scroll-cursor and really like zz, which puts the line your cursor is on in the middle of your window.

I'm looking for help to make a mapping that would perform similar to zz but puts the line my cursor is on at 20% of the window height (or 25%, 30% etc).


Edit:

Thanks to ZyX and Drasill, I was able to modify his function to get the desired functionality:

function ScrollToPercent(percent)
    let movelines=winheight(0)*a:percent/100

    if has("float") && type(movelines)==type(0.0)
        let movelines=float2nr(movelines)
    endif

    let oldso=&so
    execute ":set so=" . movelines
    execute "normal! zt"
    execute ":set so=" . oldso
endfunction
+2  A: 
ZyX
Thanks for the example, but this function only moves the cursor, not the cursor and the line under the cursor. See my above edit for my end result.
Nick Canzoneri
@Nick Canzoneri Just was updating my function while you was writing this comment.
ZyX
A: 

I take it you know about zb and zt which scrolls the current line to the bottom or top, respectively?

Walter
Yep I did know about them. I was looking to make something between zz and zt.
Nick Canzoneri
+5  A: 

This is not specifically an answer to your question, but you can like the scrolloff option.

For exemple : :set scrolloff=5 will always leave 5 visible lines at start and end of you window.

So, when you use zt or zb, your cursor will go 5 lines under top (or 5 lines above bottom, respectively), which can be about your desired 20%.

I personnaly love this setting.

:help scrolloff
Drasill
I have been looking for this for a long time ! Many thanks.
ereOn
A: 

Somewhat related, I've got ^J mapped to move the cursor down one line & then recenter the screen:

map <C-J> jzz
map <C-K> kzz

You could subsitute in your scroll-to-percent mapping in place of zz.

Anyway, this has the effect of leaving the cursor in the middle of the screen while text scrolls by behind it - I often use this instead of normal j/k.

John Hart