Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

in vim, with


I get the number of all buffers the same with


, but
how I can get the total number of buffers ?

share|improve this question
up vote 5 down vote accepted

Same idea than Heptite's solution, but as a one liner. Many other things may be done this way: get the name of the buffer (thanks to map), wipeout buffers that match a pattern, etc.

echo len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
share|improve this answer

To my knowledge there is no built-in method in Vim to do this, but you could create a function:

function! NrBufs()
    let i = bufnr('$')
    let j = 0
    while i >= 1
        if buflisted(i)
            let j+=1
        let i-=1
    return j

Put the above in a text file with its name ending in .vim, :source it, then you can do something like:

:let buffer_count = NrBufs()
:echo buffer_count
share|improve this answer
Note for others who find this: I think it should be 'while i > 0', since buffer numbers start from 1, not 0. – Meta Jul 11 '13 at 14:10
@Meta: I fixed my answer. Thanks. Although it's not really going to be an issue since there will always be at least one buffer. – Heptite Jul 11 '13 at 17:44

If you want Heptite's solution as a command, add the following to your .vimrc file:

command BufNum echo len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
share|improve this answer

Are you looking perhaps for ?

share|improve this answer
This will "count" buffers that have been unloaded since Vim never recycles a buffer number, plus it would have to be :echo bufnr('$') – Heptite Oct 12 '11 at 1:00
(Okay, your version will properly echo the number of the last loaded buffer, but the syntax is a little confusing.) – Heptite Oct 12 '11 at 1:07
@Heptite - Yes, that was a mistake. As to the matter of syntax, I see no difference between the two. – Rook Oct 12 '11 at 1:28
They do the same thing, but :echo is a command and not a function, and can never be used like a function (unless you wrap your own user-defined function around an :echo). The difference is important in many contexts of VimL. – Heptite Oct 12 '11 at 3:41
@Heptite - I must admit I've no idea what you just said ... (more of a duct tape engineer here; less of a "code is beautiful" programmer) – Rook Oct 12 '11 at 3:55

protected by JakeGould Jan 9 at 18:38

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.