Skip to content

Instantly share code, notes, and snippets.

@AlexMasterov
Last active May 2, 2016 13:42
Show Gist options
  • Save AlexMasterov/e81093a7b4cf14413b2b04abcf83ffe2 to your computer and use it in GitHub Desktop.
Save AlexMasterov/e81093a7b4cf14413b2b04abcf83ffe2 to your computer and use it in GitHub Desktop.
Vim: clean up hidden buffers
" Clean up hidden buffers
command! -bar -bang -nargs=? CleanBuffers call CleanBuffers("<bang>")
function! CleanBuffers(...) abort
let force = a:0 >= 1 && a:1 ==# '!'
redir => bufs
silent! buffers
redir END
let hidden = []
for buf in map(split(bufs, '\n'), 'split(v:val)')
let bufnr = buf[0] + 0
let flags = matchstr(join(buf[1:]), '^.*\ze\s\+"')
let mod = substitute(flags, '\s*', '', 'g')
let hide = mod ==# 'h' || mod ==# 'h+'
\ && (force || input(printf("\n%s not saved.\nDelete anyway? [Y]es, (N)o: ",
\ bufname(bufnr))) =~? '^y\%[es]$')
if hide
call add(hidden, bufnr)
endif
endfor
let saved = get(g:, 'bufcleaner_max_save', 2)
let target = len(hidden) > saved ? join(hidden[0:-saved-1], ' ') : ''
if !empty(target)
silent! execute 'bwipeout!' target
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment