12

I often find scrolling a full page too disorienting, half a page too much, but a quarter page is just right. I currently do it just by holding down the arrow keys.

How do I scroll 25% of the page down and up easily?

17

Maybe ctrld and ctrlu could be what you are looking for. By default they move half of the screen.

From :h CTRL-D:

Scroll window Downwards in the buffer. The number of lines comes from the 'scroll' option (default: half a screen).

If [count] given, first set 'scroll' option to [count].

Which means that the first time you want to scroll in a window you can do XXctrld where XX is the 25% of the number of lines in your window. As it sets scroll to the XX value you can then use ctrld and ctrlu to move 25% of the screen.

Also, I think that :h scrolling might be interesting for you.


Edit

And here is another solution with a function and some mappings to add to your .vimrc:

function! ScrollQuarter(move)
    let height=winheight(0)

    if a:move == 'up'
        let key="\<C-Y>"
    else
        let key="\<C-E>"
    endif

    execute 'normal! ' . height/4 . key
endfunction

nnoremap <silent> <up> :call ScrollQuarter('up')<CR>
nnoremap <silent> <down> :call ScrollQuarter('down')<CR>

The function will get the height of the current window, and accordingly to its parameter will scroll the screen up or down of a quarter of the height.

The mappings will call the function, the first one to go up and the second one to go down.

Of course, you can change <up> and <down> to some other keys if you want to keep the default behavior of your arrow keys.

  • 2
    Seems like it might make more sense to use ctrl-E and ctrl-Y in the mappings, because a). you then have separate shortcuts for both 1/2 page and 1/4 page scrolling. b). the behaviour of ctrl-U/ctrl-D is consistent and not dependent on whether you have previously used the mapping. – Rich Oct 25 '16 at 12:36
  • I don't understand your important note. What do you mean enter manually? Currently using this deletes a line instead of moving the page. – Louis Hong Oct 25 '16 at 18:00
  • @LouisHong See the line let key="^U" the part between the double quote is meant to represent the key code of ctrl+u. So if you copied the function in vim put your cursor on the first double quote hit ci" to modify the content of the quotes and then hit ctrl+v followed by ctrl+u to the same on the line let key="^D". – statox Oct 25 '16 at 18:11
  • @Rich: Yup that's a good point. I used the arrow keys because that's what OP said he used but your point is relevant. – statox Oct 26 '16 at 9:06
  • @statox You misunderstood me! (I wasn't clear). I meant use let key="^Y" instead of let key="^U". It will create the same behaviour but without affecting the 'scroll' setting. – Rich Oct 26 '16 at 9:31
2

You can use ctrle and ctrly to move the page by one line up and down.

Then if you want for +/- 25% to be moved you can use 20ctrle and that will move you 20 lines down.

Here is also a good explained question that is similiar to yours: https://stackoverflow.com/questions/3458689/how-to-move-screen-without-moving-cursor-in-vim

2

I variated statox approach so it scrolls more consistent over the linewraps. If you want a quarter step, change 0.618 back to 0.25 or whatever.

function! ScrollGolden(move)
    let height=winheight(0)
    if a:move == 'up'
        let prep='L'
        " let key="^Y"
        let key='gk'
        let post='zb'
    elseif a:move == 'down'
        let prep='H'
        " let key="^E"
        let key='gj'
        let post='zt'
    endif
    execute 'normal! ' . prep . float2nr(round(height*0.618)) . key . post
endfunction
nnoremap <silent> <BS>    :call ScrollGolden('up')<CR>
nnoremap <silent> <Space> :call ScrollGolden('down')<CR>
0

If your wanting a good general answer and not an exact 25% you could factor the normal line count on a page by divide by 25 and set some system mappings. For instance if you normally have about 100 line on a screen at a time then 25% of that is 25 lines so you could set a key mapping on something like your capital J and K for quickly moving up and down 25 lines

noremap J 25j
noremap K 25k

If your wanting to just jump to the quarter mark of the entire file then in normal mode just type:

25%
  • 1
    I think there's two drawback with your solution: noremap J 25j hardcodes the value 25 thus when for example you horizontally split a 100 lines window your mapping will actually navigate 50% of the new windows. About your second option :25% I didn't knew it existed but from my tests it seems that it puts the cursor on a position based on the first line: if I have a 100 lines buffer and I want to move 25% forward typing :25% will bring me on line 25 no matter where my cursor was. Also could you please put the help topic describing this feature I can't find it in the doc? – statox Oct 27 '16 at 8:09
  • 1
    I found the help topic at :h N% :-) – statox Oct 27 '16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.