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 ?

1 Answer 1


" 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

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

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
  • 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
    Commented Dec 12, 2020 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
    Commented Dec 12, 2020 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
    Commented Dec 12, 2020 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
    Commented Dec 12, 2020 at 15:51
  • 1
    @filbranden Oops. And this one looks buggy. Didn't even remember this one. Have to fix it somehow.
    – Matt
    Commented Dec 12, 2020 at 16:06

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.