in vim, with

 :buffers

I get the number of all buffers the same with

 :ls

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

link|improve this question

64% accept rate
feedback

3 Answers

up vote 1 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, http://stackoverflow.com/questions/2974192/how-can-i-pare-down-vims-buffer-list-to-only-include-active-buffers/2974600#2974600n etc.

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

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 >= 0
        if buflisted(i)
            let j+=1
        endif
        let i-=1
    endwhile
    return j
endfunction

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
link|improve this answer
feedback

Are you looking perhaps for ?

:echo(bufnr('$'))
link|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. – ldigas 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) – ldigas Oct 12 '11 at 3:55
feedback

Your Answer

 
or
required, but never shown

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