Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll window halfway between zt and zz in Vim

Tags:

vim

scroll

I often find myself wanting to scroll a Vim window so that the line I'm on is roughly a quarter of the screen down—it provides more room to breathe than zt, but has more code displayed than zz. It's also helpful when my cursor is at the start of a function I want to read, but zt will cut off the Javadoc-like comments.

Is there some magic I can use to do this? I'm afraid I don't understand Vim's scripting at all.

like image 705
Xiong Chiamiov Avatar asked Nov 09 '11 01:11

Xiong Chiamiov


2 Answers

Perhaps set scrolloff=5 (or how many lines above and below the cursor you would like) will do the trick for you. It isn't exactly what you asked for, but close enough?

like image 61
Codie CodeMonkey Avatar answered Nov 10 '22 10:11

Codie CodeMonkey


You could use:

nnoremap <expr> zT 'zt' . winheight(0)/4 . '<c-y>'
nnoremap <expr> zB 'zb' . winheight(0)/4 . '<c-e>'

which will remap zT to zt followed by the quarter of current window height and CTRL-Y (which scrolls one line up, leaving cursor where it is).

like image 32
Benoit Avatar answered Nov 10 '22 12:11

Benoit