Add this to your .vimrc:
function! CloseHiddenBuffers() let i = 0 let n = bufnr('$') while i < n let i = i + 1 if bufloaded(i) && bufwinnr(i) < 0 exe 'bd ' . i endif endwhile endfun
Then you can do this to close hidden buffers:
:call CloseHiddenBuffers()
(You probably want to bind a key or command to it.)
Update:
The version for tab support has been updated here. (I myself do not use tabs, so I did not understand that bufwinnr only works for windows on the current page).
function! CloseHiddenBuffers() " figure out which buffers are visible in any tab let visible = {} for t in range(1, tabpagenr('$')) for b in tabpagebuflist(t) let visible[b] = 1 endfor endfor " close any buffer that loaded and not visible for b in range(1, bufnr('$')) if bufloaded(b) && !has_key(visible, b) exe 'bd ' . b endif endfor endfun
Laurence Gonsalves Oct 08 '09 at 0:26 2009-10-08 00:26
source share