1

In vim's command mode, when I type 'H' the cursor usually doesn't go all the way to the top of the window, and 'L' doesn't go all the way to the bottom.

To investigate the problem, I created a 60-line buffer. Each line has a number on it, from 1 to 60. If line 1 is visible at the top of the window, then the last line in the window is 58. (If I type :echo winheight('%'), the number 58 appears in the bottom line.)

If I position the cursor at the first line (1G) and then type 'L', the cursor moves to line 53. Then typing 'H' gets me back to line 1. If I go to line 58 by typing '58G', the window scrolls and I see lines 3-60. If I then type 'H', I go to line 8 instead of 3.

How can I tweak vim to get 'H' and 'L' working properly?

I am running vim in a konsole window under Fedora Linux (35). The konsole version is 21.08.3. Vim is 8.2 that includes patches 1-3755.

2
  • 1
    Yes, that works. I set scrolloff to zero in my .vimrc. Thanks! Dec 23, 2021 at 20:06
  • Once you've received a solution to your question, you should upvote and accept that answer (by hitting the green tick and the up arrow)so future people know that it indeed worked.
    – mattb
    Dec 24, 2021 at 15:35

1 Answer 1

1

The behavior you're seeing is due to the 'scrolloff' option.

When this option is set to a positive number, that indicates the minimal number of screen lines to keep above and below the cursor. This helps make some context visible around the line of the cursor, even when you scroll up or down past the current screen.

As a consequence of 'scrolloff' being set, the cursor will typically not be able to reach the top and bottom lines on screen (in fact, they won't be able to reach the first and last "N" lines, where N is what 'scrolloff' is set to.)

As such, the H and L commands will have their behavior adjusted to jump to the highest or lowest line visible in screen without violating the 'scrolloff' restrictions. (They preserve the behavior of not scrolling the current screen.)

An exception exists at the very top and at the very bottom of the file opened in a buffer. Since in that case there's no context to show above or below those lines, the cursor is then able to move all the way to the first or last line, and the H and L commands will also end up being able to jump to those lines.

The default for 'scrolloff' is zero (unset), but the defaults.vim that is shipped with Vim will set it to 5. The configuration from defaults.vim is used whenever the user has no vimrc file present, which might explain why you're seeing this behavior despite not having configured this option explicitly.

2
  • You can use :verbose set scrolloff? (or :verb se so for short) to figure out where that setting is coming from.
    – filbranden
    Dec 24, 2021 at 19:58
  • 1
    I have a minimal .vimrc file in my home directory. It has one line that is not a comment: ":source ~/programs/vim/vimrc.vim". The sourced file (which gets backed up through my normal processes) contains all my vim settings. For me, the fix was to set 'scrolloff=0' in the sourced vimrc.vim file. On my computer, I found "scrolloff=5" in /etc/vimrc which is the first script that my copy of vim sources. Dec 24, 2021 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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