Pursuing a vimscript function or expression that

count how many buffers been modified.

i.e., how many buffers :bm could switch to,

given that option hidden is set.

== Edit ==

In the hope of used in statusline.


Found it.

func! CountModifiedBuffer()
    let mod = map(getbufinfo(), 'v:val.changed')
    return len(filter(mod, 'v:val'))

== Edit ==

I use %{IsBuffersModified()} instead of builtin %m inside my statusline setting, which encode more info.

" [+] if only current modified, [+3] if 3 modified including current buffer.
" [3] if 3 modified and current not, "" if none modified.
func! IsBuffersModified()
    let cnt = CountModifiedBuffer()
    return cnt == 0 ? "" : ( &modified ? "[+". (cnt>1?cnt:"") ."]" : "[".cnt."]" )

== Edit Edit ==

CountModifiedBuffer could be one-liner.

    return len(filter(getbufinfo(), 'v:val.changed == 1'))
| improve this answer | |
  • know getbufinfo from vi.stackexchange.com/a/10478/10254 – qeatzy Nov 23 '17 at 8:04
  • 1
    NB: getbufinfo is deprecated. Use getbufvar instead. Example: getbufvar(0, "&mod") – Braden Best Mar 30 '18 at 23:46
  • 1
    @BradenBest, where have you read about getbufinfo() deprecation? – Luc Hermitte Jun 4 '18 at 13:35
  • @LucHermitte This was over two months ago, so I don't remember the source. At the time, I was modifying a vim script that was using getbufinfo to render the buffers in the tab bar, and either through vim telling me directly, or a source online that said to use getbufvar, I found out that getbufinfo has been deprecated. I'm trying to test getbufinfo in vim right now and not only is it not working at all, but I can't find a single shred of documentation on it. :h getbufinfo -> nothing; :h getbufvar -> help page. So even if the source was wrong, getbufvar is clearly more reliable. – Braden Best Jun 4 '18 at 21:51
  • 1
    I'd check tomorrow on a fresh install of 8.1.02x or so. There is an entry behind :h getbufinfo(). If I remember correctly it's quite recent. Much more recent than getbufvar(). So indeed, getbufvar() is to be preferred for portability reasons. But not because the other one has been deprecated. According to git blame, getbufinfo() appears in v7.4.2204 and getbufvar() in v7.0c13. – Luc Hermitte Jun 4 '18 at 23:15

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.