7

I know Vim provides functions to display window's dimensions. But does it also provide ways for me to know current buffer/split's dimensions? Like how many lines and columns in displable area?

Currently all I can find online is the information on how to change the split width and height rather than obtaining the current dimension. But I don't need to change the split size, only need to know its size information to organize some material in the buffer.


Edit: As indicated by @Tommy A in his comment, the reason I have this question is that I misunderstood of the terminology. To quote him:

A window is a viewport for a buffer. "Split" is just a verb in Vim. I think you might be mixing up the terminology for windows and buffers. Soft wrapping occurs at winwidth(0) and it's also the the total number of characters that can be displayed horizontally. Hard wrapping occurs at &textwidth.

Hence what I want to do is to show the window dimension, by winwidth(0) and winheight(0).

Also, as mentioned in Question Get Usable Window Width in Vim Script, winwidth(0) is usually larger than the usable width of the window for the content. To obtain the correct size information, please refer to the answer in that question.

2

1 Answer 1

12

These are the different ways (that I know of) to get dimensional information from Vim.

Vim

&columns and &lines will give you the total width and height of the screen Vim is displayed in. These can be changed at runtime, but it's not recommended.

&cmdheight will give you the height of the command line.

Windows

winwidth(0) and winheight(0) will give you the dimensions for the current window. It includes the width of the gutter columns, but not the window separator.

If you wanted to get the size as a ratio, you would need to turn one of the values into a float:

let width_ratio = winwidth(0) / str2float(&columns)
let height_ratio = winheight(0) / str2float(&lines)

Buffers

If you want the total number of lines in a buffer:

let total_lines = line('$')

If you want the number of visible lines in a buffer:

let visible_lines = line('w$') - line('w0')

w$ is the bottom line and w0 is the top line.

To get the maximum column from the buffer, you could use:

function! BufferWidth() abort
  let view = winsaveview()
  let max_col = 0
  g/^/let max_col=max([max_col, col('$') - 1])
  call histdel('search', -1)
  let @/ = histget('search', -1)
  call winrestview(view)
  return max_col
endfunction

The :global command is used to get the last column in every line and keep the highest value. The rest of the function saves/restores the state since :global moves the cursor and changes the search history.

7
  • I see. So maybe there is no direct way to get it. Not that I can't find one... Thanks for your complete answer. I'll wait a while to see if there really no one know of any direct method or if it really doesn't exist before accepting your answer. :)
    – Chong
    Jul 23, 2016 at 6:10
  • 1
    What exactly is it that you want to do? From your description, it sounds like winwidth() and winheight() are what you're looking for.
    – Tommy A
    Jul 23, 2016 at 6:14
  • I want to know how many character a split/buffer can display before soft wrapping or getting beyong visible range. Also how many lines it can display in its display range. Now that taking a closer look, even your BufferWidth doesn't really do the first part...
    – Chong
    Jul 23, 2016 at 6:19
  • 2
    A window is a viewport for a buffer. "Split" is just a verb in Vim. I think you might be mixing up the terminology for windows and buffers. Soft wrapping occurs at winwidth(0) and it's also the the total number of characters that can be displayed horizontally. Hard wrapping occurs at &textwidth.
    – Tommy A
    Jul 23, 2016 at 6:26
  • I see, winwidth(0) appears to be what I want... Strange it's value is a few character longer than the number of columns able to contain in the line... Does line number occupy the winwidth(0)?
    – Chong
    Jul 23, 2016 at 6:38

Your Answer

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

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