6

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?

4

3 回答 3

13

您可以使用该函数作为测试表达式bufnr()来获取最后一个缓冲区的编号,然后创建一个从 1 到该编号的列表并过滤它以删除未列出的缓冲区。buflisted()

" 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)'))
于 2013-07-29T20:14:59.877 回答
3

我会通过调用buflisted()数字范围来做到这一点,直到bufnr("$"). 像这样的东西:

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
于 2013-07-29T20:13:14.263 回答
3

一个简单的解决方案是使用getbufinfo.

在你的 vimscript 中:

len(getbufinfo({'buflisted':1}))

或使用命令对其进行测试:

:echo len(getbufinfo({'buflisted':1}))
于 2019-03-07T04:08:35.600 回答