Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vimscript: Number of listed buffers

Tags:

vim

viml

In my vimscript, I need to get a count of all buffers that are considered listed/listable (i.e. all buffers that do not have the unlisted, 'u', attribute).

What's the recommended way of deriving this value?

like image 499
Jonathan Avatar asked Jul 29 '13 18:07

Jonathan


2 Answers

You could use bufnr() to get the number of the last buffer, then create a list from 1 to that number and filter it removing the unlisted buffers, by using the buflisted() function as the test expression.

" All 'possible' buffers that may exist
let b_all = range(1, bufnr('$'))

" Unlisted ones
let b_unl = filter(b_all, 'buflisted(v:val)')

" Number of unlisted ones
let b_num = len(b_unl)

" Or... All at once
let b_num = len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
like image 112
sidyll Avatar answered Nov 01 '22 20:11

sidyll


I would do it by calling buflisted() on the range of numbers up to the largest buffer number given by bufnr("$"). Something like this:

function! CountListedBuffers()
    let num_bufs = 0
    let idx = 1
    while idx <= bufnr("$")
        if buflisted(idx)
            let num_bufs += 1
        endif
        let idx += 1
    endwhile
    return num_bufs
endfunction
like image 23
Prince Goulash Avatar answered Nov 01 '22 21:11

Prince Goulash