Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created July 6, 2011 12:22
Show Gist options
  • Save AndrewRadev/1067094 to your computer and use it in GitHub Desktop.
Save AndrewRadev/1067094 to your computer and use it in GitHub Desktop.
Close all buffers that are not currently opened
command! CloseHiddenBuffers call s:CloseHiddenBuffers()
function! s:CloseHiddenBuffers()
" Get the output of :ls as a string
redir => ls_output
silent ls
redir END
" Go through all buffer entries
for line in split(ls_output, "\n")
if line =~ '\d\+\s*\S\+\s*".*"' " then the listing contains flags
let [bufnr, flags; _] = split(line, '\s\+')
if flags !~ 'a'
" then it's not active, delete:
exe "bdelete ".bufnr
endif
else
" no flags, so not active, delete:
let [bufnr; _] = split(line, '\s\+')
exe "bdelete ".bufnr
endif
endfor
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment