I also had a need for this feature all the time. This is the solution I have in my vimrc.
function! GetBufferList() return filter(range(1,bufnr('$')), 'buflisted(v:val)') endfunction function! GetMatchingBuffers(pattern) return filter(GetBufferList(), 'bufname(v:val) =~ a:pattern') endfunction function! WipeMatchingBuffers(pattern) let l:matchList = GetMatchingBuffers(a:pattern) let l:count = len(l:matchList) if l:count < 1 echo 'No buffers found matching pattern ' . a:pattern return endif if l:count == 1 let l:suffix = '' else let l:suffix = 's' endif exec 'bw ' . join(l:matchList, ' ') echo 'Wiped ' . l:count . ' buffer' . l:suffix . '.' endfunction command! -nargs=1 BW call WipeMatchingBuffers('<args>')
Now I can just do :BW regex (for example :BW \.cpp$ and erase all the corresponding buffers that match this pattern in their path name.
If you want to delete, not erase, you can, of course, replace exec 'bw ' . join(l:matchList, ' ') exec 'bw ' . join(l:matchList, ' ') to exec 'bd ' . join(l:matchList, ' ') exec 'bd ' . join(l:matchList, ' ')
Kent 01 Feb 2018-11-11T00: 00Z
source share