Vimscript: number of buffers listed

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

What is the recommended way to get this value?

+4
source share
2 answers

You can use bufnr() to get the number of the last buffer, then create a list from 1 to this number and filter it by deleting unregistered buffers using the buflisted() function as a 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)')) 
+11
source

I would do this by typing buflisted() in a range of numbers up to the largest buffer number given by bufnr("$") . Something like that:

 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 
+3
source

Source: https://habr.com/ru/post/1494118/


All Articles