0

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]:
:------------------------------------------:

Incorrect:
: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.

1 Answer 1

1

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
    else
        let sl .= 'text'
    endif
    if &modified || &readonly
        let sl .= '    '
    endif
    let sl .= '%m%r'
    return sl
endfunc
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
4
  • As I understand, checking against arbitrary flag will be quite complicated?
    – user90726
    Feb 8, 2019 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.
    – user90726
    Feb 8, 2019 at 10:30
  • @jsv See update.
    – Ralf
    Feb 8, 2019 at 10:56
  • Thanks. By the way, today I discovered that described task could be solved differently: set statusline+=%(\ \ \ \ %m%r%h%)
    – user90726
    Feb 8, 2019 at 20:48

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.