How can I delete all hidden buffers?

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

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

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

bufexplorer.vim can manage your vim buffers. Here to download . you can use :BufExplorer to show all your vim buffers in a window. And press 'd' to delete it.

Tags:

Vim