Is there a Vim function or variable that would return 1/3rd (or any dynamic value) of the current window height?

I'm using Neoterm, and it's terminal splits open at half the current window height by default. I want them to open at 1/3rd the current window height.



The winheight function will return the height of window n. From :h winheight():

winheight({nr})                     *winheight()*
        The result is a Number, which is the height of window {nr}.
        {nr} can be the window number or the |window-ID|.
        When {nr} is zero, the height of the current window is
        returned.  When window {nr} doesn't exist, -1 is returned.
        An existing window always has a height of zero or more.
        Examples: >

So winheight(0) will give us the height of the current window. split takes a numerical argument. From :h :split:

:[N]sp[lit] [++opt] [+cmd] [file]           *:sp* *:split*
        Split current window in two.  The result is two viewports on
        the same file.

        Make the new window N high (default is to use half the height
        of the current window).  Reduces the current window height to
        create room (and others, if the 'equalalways' option is set,
        'eadirection' isn't "hor", and one of them is higher than the
        current or the new window).

So, combining these two commands, you could do this with

exec winheight(0)/3."split"


exec winheight(0)/3."new"

if you want a new buffer, rather than a split on the same file. Since this isn't exactly easy to remember, you could wrap it in a function:

function! ThirdSplit()
  exec winheight(0)/3."split"

You could even make this function take an optional denominator:

function! Split(ratio)
  exec winheight(0)/a:ratio."split"

Or of course, you could make a mapping. For example, <leader>s

nnoremap <expr> <leader>s ":".winheight(0)/3."split\<CR>"
| improve this answer | |

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.