I'm trying to add whitespace on the left side of the flags section, and to hide this whitespace when flags aren't exist.

Here is what I mean:

Statusline with no flags:
:File.txt                              text:

The same, with flags:
:File.ext                   text    [+][RO]:

:File.ext                          text    :

Colons and hyphens are used to show window size.

And here is statusline code:

set laststatus=2
set statusline=
set statusline+=%t
set statusline+=%=
set statusline+=%{strlen(&filetype)?&filetype:'text'}
"set statusline+=%{&modified?'\ \ \ \ ':''}             " Works
set statusline+=%{(&modified||&readonly)?'\ \ \ \ ':''} " Doesn't work
set statusline+=%m%r

As you can see, there is an issue with 7th line.

So, there are two questions:

  • How to check condition against both flags?
  • A better option: is there a way to check against any flag? If we have some arbitrary flag in statusline, then add whitespace; if we don't have flags, then don't add whitespace.

You have to escape the pipe symbols, like:

set statusline+=%{(&modified\|\|&readonly)?'\ \ \ \ ':''} "

If it gets to complicated, you might consider to use a function to create the statusline:

function MyStatusLine()
    let sl = '%t%='
    if &filetype != ''
        let sl .= &filetype
        let sl .= 'text'
    if &modified || &readonly
        let sl .= '    '
    let sl .= '%m%r'
    return sl
set statusline=%!MyStatusLine()

Update: The flags named in :help stl:

  • %m/%M is &modified
  • %r/%R is &readonly
  • %h/%H is &filetype == 'help'
  • %w/%W is &previewwindow
| improve this answer | |
  • As I understand, checking against arbitrary flag will be quite complicated? – jsv Feb 8 '19 at 9:52
  • When I open help with h: stl and then scroll down to "item meaning", it is written that there are only 4 flags: modified (m, M), readonly (r, R), help buffer (h, H), and preview window (w, W). I mean any of them. – jsv Feb 8 '19 at 10:30
  • @jsv See update. – Ralf Feb 8 '19 at 10:56
  • Thanks. By the way, today I discovered that described task could be solved differently: set statusline+=%(\ \ \ \ %m%r%h%) – jsv Feb 8 '19 at 20:48

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.