0

I find myself unintentionally hitting j (or some other downward cursor movement key) when I'm at the bottom of a file. Then the next thing I do is to hit Ctrl-E to scroll the current line up - I may even just hit zt to move the current line to the top of the window.

My question is: Is there a way I could configure vim to automatically scroll for me (I am ok with just using Ctrl-W in this case) when I hit j at the last line of a file?

I know there's already a similar question, but the answer is to persist the cursor in the middle of the screen, which is not what I'm looking to do.

1

1 Answer 1

0

You can use an "expr" mapping to check whether you're on the last line of the buffer and map j to <C-e> in that case.

nnoremap <expr> j line('.') ==# line('$') ? '<C-e>' : 'j'

If you want this to also work the same way on Visual mode, then repeat the command with xnoremap as well.

xnoremap <expr> j line('.') ==# line('$') ? '<C-e>' : 'j'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .