# Vim keep last line at bottom of buffer

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:

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

How can this be accomplished?

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

If you try scrolling when you are at the bottom vim assumes you want to move 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.

• 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 Apr 9 '15 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 Apr 9 '15 at 20:45

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).