While I was customizing my status line, I was wondering if I could make certain elements of the status line disappear if the window width is too small. To do this, I would have to know how much space the status line is actually taking up. Is there a way of doing this? If not, is there some other way of hiding/showing certain elements of the status line if the window is squishing its contents?

2 Answers 2


While I'm not a statusline connoisseur, I think the general idea is to do it "the other way around": while computing the statusline, the evaluated code (functions etc.) could check for Vim's windows width and decide if (or how) a certain information will be displayed. To put it in a silly way, something like this:

:set statusline=%{winwidth(0)>80?'looooooooooooooooooooooooong':'short'}

Every time Vim's windows width changes, the status line will be re-evaluated and will display the adjusted information.

In other words, if you want to know the length of the statusline, you will kind of ...have to keep track of it while building the expression to be evaluated (since that's the place where you have the "total" control). Slightly less sillier example:

:set statusline=%{len(expand('%'))>winwidth(0)?expand('%:t'):expand('%')}

(show full filename path if shorter than the window width, or only the actual filename if not). In practice, functions should make such a build-statusline-while-checking-its-length easier.

After some digging - and if I understood it correctly - this seems to be the approach used in the vim-airline plugin:

let s:section_truncate_width = get(g:,
  'airline#extensions#default#section_truncate_width', {
  \ 'b': 79,
  \ 'x': 60,
  \ 'y': 88,
  \ 'z': 45,
  \ })

function! s:get_section(winnr, key, ...)
  if has_key(s:section_truncate_width, a:key)
    if winwidth(a:winnr) < s:section_truncate_width[a:key]
      return ''

  [...] " other code, returns section text

(if a section has 'truncate width' key in the dictionary, and window width is smaller than that width, the section isn't displayed)

  • The part about knowing the length of the status line when building it only works when it doesn't contain things like '%c', since those are calculated by vim afterwards. However, those special status line characters can be emulated by vimscript most of the time, it just removes the convenience of them. Jul 29, 2015 at 22:19
  • Yes, that's inconvenient (and can be circumvented, as you say). That's why I used expand('%') as example, instead of %f. The thing is, Vim doesn't seem to allow peeking at the evaluated statusline anywhere (e.g. to get its length).
    – VanLaser
    Jul 29, 2015 at 22:27

It doesn't answer your question about the length of the status line, but Vim should manage this for you: are you aware that 'statusline' has a potential < item you can include? It indicates where to truncate the status line if it's too long. See :help statusline for more information.

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.