From d8e22d0f7847333ecbb424eddba9c134721f9e77 Mon Sep 17 00:00:00 2001 From: Oliver Ruben Albertini Date: Thu, 8 Apr 2021 16:02:11 -0700 Subject: [PATCH] Add :DBuffers command This works like :Buffers, but allows you to select multiple buffers for deletion. --- autoload/fzf/vim.vim | 21 +++++++++++++++++++-- plugin/fzf.vim | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 9e14f8540..444b767ee 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -702,6 +702,16 @@ function! s:bufopen(lines) execute 'buffer' b endfunction +function! s:bufdelete(lines) + if len(a:lines) < 2 + return + endif + for line in a:lines[1:] + let b = matchstr(line, '\[\zs[0-9]*\ze\]') + execute 'bdelete' b + endfor +endfunction + function! fzf#vim#_format_buffer(b) let name = bufname(a:b) let line = exists('*getbufinfo') ? getbufinfo(a:b)[0]['lnum'] : 0 @@ -728,13 +738,20 @@ endfunction function! fzf#vim#buffers(...) let [query, args] = (a:0 && type(a:1) == type('')) ? \ [a:1, a:000[1:]] : ['', a:000] + let sink = 's:bufopen' + let multi = '+m' + if type(args[-1]) == type('') && args[-1] ==# 'delete' + let args = args[:-2] + let sink = 's:bufdelete' + let multi = '-m' + endif let sorted = fzf#vim#_buflisted_sorted() let header_lines = '--header-lines=' . (bufnr('') == get(sorted, 0, 0) ? 1 : 0) let tabstop = len(max(sorted)) >= 4 ? 9 : 8 return s:fzf('buffers', { \ 'source': map(sorted, 'fzf#vim#_format_buffer(v:val)'), - \ 'sink*': s:function('s:bufopen'), - \ 'options': ['+m', '-x', '--tiebreak=index', header_lines, '--ansi', '-d', '\t', '--with-nth', '3..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query, '--preview-window', '+{2}-/2', '--tabstop', tabstop] + \ 'sink*': s:function(sink), + \ 'options': [multi, '-x', '--tiebreak=index', header_lines, '--ansi', '-d', '\t', '--with-nth', '3..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query, '--preview-window', '+{2}-/2', '--tabstop', tabstop] \}, args) endfunction diff --git a/plugin/fzf.vim b/plugin/fzf.vim index 9719538ac..6f3cc6147 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -49,6 +49,7 @@ call s:defs([ \'command! -bang -nargs=? GitFiles call fzf#vim#gitfiles(, fzf#vim#with_preview( == "?" ? { "placeholder": "" } : {}), 0)', \'command! -bang -nargs=? GFiles call fzf#vim#gitfiles(, fzf#vim#with_preview( == "?" ? { "placeholder": "" } : {}), 0)', \'command! -bar -bang -nargs=? -complete=buffer Buffers call fzf#vim#buffers(, fzf#vim#with_preview({ "placeholder": "{1}" }), 0)', +\'command! -bar -bang -nargs=? -complete=buffer DBuffers call fzf#vim#buffers(, fzf#vim#with_preview({ "placeholder": "{1}" }), 0, "delete")', \'command! -bang -nargs=* Lines call fzf#vim#lines(, 0)', \'command! -bang -nargs=* BLines call fzf#vim#buffer_lines(, 0)', \'command! -bar -bang Colors call fzf#vim#colors(0)',