0

I am trying to display the number of loaded buffers on my statusline using:

...
set statusline += %{len(getbufinfo({"buflisted":1}))}
...

The error I get is :

E540: Unclosed expression sequence: statusline +=%{len(getbufinfo({

What could I be doing wrong ?

2

" starts a comment in VimScript. Please, take your time reading :h :comment and :h :bar and remembering the rules.

You must either escape it with backslash or use another command that accepts quote as part of argument.

UPD. Also, you're not allowed to have nested braces here (thanks to @filbranden for spotting this). And so we have

"WRONG
set statusline+=%{len(getbufinfo({"buflisted":1}))}
"RIGHT
set statusline+=%{ListedBuffers()}
function! ListedBuffers() abort
    return len(getbufinfo({'buflisted': 1}))
endfunction

In practice, it could be easier to compute the whole status line by a function

set statusline=%!MakeStatusLine()
function! MakeStatusLine() abort
    let l:value = ...
    let l:value .= len(getbufinfo({'buflisted': 1}))
    ...
    return l:value
endfunction
5
  • 2
    This is good, but I think there's more... When using :set, you can't have spaces between the option and the operator (= or, in this case, +=), or between the operator and the value. (Actual spaces need to be escaped with `\`.) – filbranden Dec 12 '20 at 15:19
  • thanks. I never considered that buflisted... was bring commented out because I had used double and single quotes quite interchangeably while passsing arguments to functions prior to this (and had never faced this issue). I'll be careful in the future. Thanks! – First User Dec 12 '20 at 15:29
  • @filbranden Ah, yes, I simply copy-pasted from the first code line. But, looking at error message, that was already fixed. – Matt Dec 12 '20 at 15:44
  • 1
    There are more issues though... A %{...} expression can't have a dict (which also uses {...}) inside it. You need to define a separate function to work around that... – filbranden Dec 12 '20 at 15:51
  • 1
    @filbranden Oops. And this one looks buggy. Didn't even remember this one. Have to fix it somehow. – Matt Dec 12 '20 at 16:06

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.