24

winheight() and winwidth() return the number of columns/lines in the current window. What I'm looking for is something closer to the actual height and width of the editing area, or displayed area if run in a terminal. The problem with winheight() and winwidth() is that aspect ratio calculated from these values is out of touch with the actual aspect ratio.

:echo winwidth('%') winheight('%') winwidth('%') * 1.0 / winheight('%')
104 51 2.039216

A screenshot of the buffer area is sized 936x918 pixels, giving a displayed aspect ratio of 1.019608. I think I can get the actual value by using the size of the font, but how?

This is a follow-up to How can I make Vim open help in a vertical split?. I'm trying to adapt Nobe4's solution.

4
  • 3
    I don't think you can get dimensions in pixels with plain Vim functions. As for opening help in a vertical split, I don't bother with aspect ratio calculations, I just split vertically if the terminal window is wide enough: cnoreabbrev h <C-r>=(&columns >= 160 && getcmdtype() ==# ':' && getcmdpos() == 1 ? 'vertical botright help' : 'h')<CR>. Commented Oct 16, 2015 at 4:36
  • 1
    @SatoKatsura thus following the tradition set by most web developers, who use the document width to determine if it's a mobile. :(
    – muru
    Commented Oct 16, 2015 at 4:39
  • 1
    Not really, since I'm only inflicting that to myself. I played with aspect ratio for a while and I found the results to be unsatisfactory. Then I remembered I have exactly two shortcuts for opening terminals, for "normal" size, and for "wide" terminals. So my terminals almost always have the same sizes. YMMV of course. Commented Oct 16, 2015 at 4:43
  • @SatoKatsura that command is some serious dark magic. I am very surprised that aside from not knowing about the ==# operator which :h helped clear up I actually understood the entire freaking command. Does that make me a Vim expert? Gotta be something like that. Need to get out more.
    – Steven Lu
    Commented Apr 5, 2016 at 2:20

1 Answer 1

1

104 columns / 51 lines = 2.04

Your calculation is skewed because the aspect ratio of each cell is vertical.

For example on Terminal.app, with 23pt font and default character spacing, each terminal cell have a width of 7px and a height of 15px for a total of 728x765px:

(104 * 7) / (51 * 15) = 728 / 765 = 0.95


You cannot retrieve the font size from within Vim without invoking external commands or a way of communicating with the terminal, which can become pretty heavy. Obviously, this is different in Gvim thanks to 'guifont'.

If you manage to do it, still you need a way to deduct the size of a cell which may vary depending on the terminal program if there is no standard. I only checked iTerm 2 which luckily have the same result.


Therefore I would opt for a static solution. I grafted the minimum width check to this snippet you could place in plugin/help.vim:

function! Help(...)
  let l:topic = a:0 ? a:1 : ''
  if winwidth('%') >= 160 " Minimum width
    execute 'vertical botright help' l:topic
    execute 'vertical resize 78'
  else
    execute 'botright help' l:topic
  endif
endfunction

" Use :H to open a vertical or horizontal help split
command! -complete=help -nargs=? H call Help(<f-args>)

If the window is large enough to keep 80 columns of the current buffer, a narrow help split is opened on the right, otherwise the split is opened below. You can customize the split direction with :vertical.

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.