Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all hidden buffers?

Tags:

vim

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?

like image 295
Peeja Avatar asked Dec 09 '11 19:12

Peeja


3 Answers

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
like image 64
ZyX Avatar answered Nov 09 '22 12:11

ZyX


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
like image 9
mogelbrod Avatar answered Nov 09 '22 10:11

mogelbrod


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
like image 5
Herbert Sitz Avatar answered Nov 09 '22 11:11

Herbert Sitz