When the number of text lines is less than a window height, tilde (~) lines are displayed until the bottom of the window. I want to decrease the window height just enough to display all the lines and not to show any tilde lines.

One solution is to calculate how many visible lines are required to display all. However, it is difficult because there are a lot of options to take care, such as number, wrap, foldmethod, showbreak. Other solution is to count how many tilde lines are displayed, which would be simpler.

Could you let me know how I can count the tilde lines?

  • &lines-line('$') as a rather hacky solution that ignores the options you mention.
    – D. Ben Knoble
    Dec 19 '18 at 5:05

I think this is not easily to calculate, since you have to consider e.g. wrapped lines and the winbar.

The closest I can think of (without taking care of wrapping and a possible winbar), is something like this:

echo winheight(0) - (line('w$')-line('w0')+1)

This calculates the height of the current window and subtracts the number of visible lines in there (line('w$') is the last visible line in the current window and line('w0') is the first visible line in the current window.) But this will be wrong for wrapped lines, as you would have to add one for each wrapped line to the second half of the equation. But I don't know a good way to do this programmatically. Also if you have installed a WinBar, you would have to add 1 in addition (because this is a line not visible in the current window).


This might work (not tested with all scenarios):

call cursor(line('$'), 99999)
let &lines=winline()+1
normal zb

The +1 is to handle the status line.

Disadvantage: It moves the cursor.

Or are you talking about a split? Then try this:

call cursor(line('$'), 99999)
execute "resize " . winline()
normal zb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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