Skip to content

Instantly share code, notes, and snippets.

@nelstrom
Created February 11, 2011 07:52
Show Gist options
  • Save nelstrom/822055 to your computer and use it in GitHub Desktop.
Save nelstrom/822055 to your computer and use it in GitHub Desktop.
A command to close all Vim buffers that are not currently visible.
command! -nargs=* Only call CloseHiddenBuffers()
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 are loaded and not visible
let l:tally = 0
for b in range(1, bufnr('$'))
if bufloaded(b) && !has_key(visible, b)
let l:tally += 1
exe 'bw ' . b
endif
endfor
echon "Deleted " . l:tally . " buffers"
endfun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment