Search code examples
vimcursor-position

How to keep the cursor's vertical position when page up/down in vim?


I want to keep my cursor's vertical position when page up (Ctl-U) or down (Ctl-D). This is particularly useful when I am in a long python function and want to figure out block structures by indentation.

Current behavior when I page down:

if foo:
    xxx = yyy
    ^ <-- cursor was here before page down
    ... many content in between ...
    while bar:
        xxx = yyy
    ^      ^ <-- cursor jumped here after page down
    ^ <-- I want it to stay here 

Solution

  • I think you want to give a look at 'startofline'/'sol' option.

    By default it is on, but the description reads

    When off the cursor is kept in the same column (if possible). This applies to the commands: CTRL-D, CTRL-U, CTRL-B, CTRL-F, "G", "H", "M", "L", gg, and to the commands "d", "<<" and ">>" with a linewise operator, with "%" with a count and to buffer changing commands (CTRL-^, :bnext, :bNext, etc.).