3

I need to configure Vim in such a way that it has a normal scrolloff of 3 or 5. But in such a way that it keeps the last line at the bottom of the screen. I don't want that as I scroll, the last lines of the buffer to come up to the top of the screen under any circumstance. To make this more precise I want this:

enter image description here

and no matter what key I press, I explicitly do not want this:

enter image description here

How can this be accomplished?

4
  • Can you put some images where this happens?
    – nitishch
    Commented Apr 8, 2015 at 17:11
  • 2
    Press G, followed by ctrl-f. I think this is what he is talking about.
    – Vitor
    Commented Apr 8, 2015 at 17:13
  • Exactly, what @Vitor said
    – carlosdc
    Commented Apr 8, 2015 at 18:35
  • Try if setting the 'scrolloff' option helps you. Commented Apr 8, 2015 at 20:46

2 Answers 2

2

If you try scrolling when you are at the bottom vim assumes you want to move the text up.

You can replace commands like ctrl-f with an additional "move cursor to bottom of screen" using zb. This modification would need to be added to any other commands you wish this to not occur.

:nnoremap <c-f> <c-f>zb

Now when you ctrl-f at the bottom of the screen it stays at the bottom, but so does every instance of ctrl-f.

Update

If you would like to do it conditionally you'll need to write a little function that compares your current position to the end of the file.

function! NoScrollAtEOF()
  let curpos = getpos('.')
  let lnum = get(curpos, 1, -1)
  let len = line('$')
  if lnum + winheight(0) >= len 
    normal! zb
  endif
endfunction

nnoremap <c-f> <c-f>:call NoScrollAtEOF()<cr>

This function pulls the current position and the end of file position and calls zb if the current position plus the height of your window is greater than the end of file...if you can see the end of the file in your window.

2
  • 1
    Thanks! I undestand the approach. How would I conditionally do this only if the cursor in on the last n lines of the buffer?
    – carlosdc
    Commented Apr 9, 2015 at 19:12
  • Using the function I provided in the updated answer should keep the scrolling to operate as normal until you get close to the end. If you wish it to be n lines rather than the window height then replace the function with your value.
    – jecxjo
    Commented Apr 9, 2015 at 20:45
0

another option would be to write a function to toggle scrolloff between your desired amount (3 or 5) and 999 depending on how many lines are left in the document versus your window height. something like, (lines in document) - (line of cursor) <= (lines in window).

Your Answer

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

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