I like to run Vim with 'hidden'
on. Sometimes, though, I've got a lot of hidden buffers hanging around and I'd like to kill them all. What command can I use to :bdelete
every hidden buffer in the buffer list?
Try the following function:
function DeleteHiddenBuffers()
let tpbl=[]
call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)==-1')
silent execute 'bwipeout' buf
endfor
endfunction
Extendend version of @ZyX answer which skips modified buffers and outputs the number of buffers that was closed.
function! DeleteHiddenBuffers()
let tpbl=[]
let closed = 0
call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)==-1')
if getbufvar(buf, '&mod') == 0
silent execute 'bwipeout' buf
let closed += 1
endif
endfor
echo "Closed ".closed." hidden buffers"
endfunction
Here is slightly different way from previously posted function by Prince Goulash. Code is untested. It uses a function to parse the output of the :buffers
command, which includes marker of 'h' for hidden buffers. Something like below:
function! DeleteHiddenBuffers()
redir => buffersoutput
buffers
redir END
let buflist = split(buffersoutput,"\n")
for item in filter(buflist,"v:val[5] == 'h'")
exec 'bdelete ' . item[:2]
endfor
endfunction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With