Skip to content

Commit a54fcbc

Browse files
committed
Add support to delete buffers in :Buffers command
By pressing the CTRL-D keybinding
1 parent 02a192e commit a54fcbc

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

autoload/fzf/vim.vim

+22-6
Original file line numberDiff line numberDiff line change
@@ -687,18 +687,30 @@ function! s:bufopen(lines)
687687
if len(a:lines) < 2
688688
return
689689
endif
690-
let b = matchstr(a:lines[1], '\[\zs[0-9]*\ze\]')
691-
if empty(a:lines[0]) && get(g:, 'fzf_buffers_jump')
690+
691+
let key = a:lines[0]
692+
let buffer_numbers = map(a:lines[1:], {_idx, bufline -> matchstr(bufline, '\[\zs[0-9]*\ze\]')})
693+
694+
if key == 'ctrl-d'
695+
execute 'bdelete' join(buffer_numbers, ' ')
696+
return
697+
endif
698+
699+
let b = buffer_numbers[0]
700+
701+
if empty(key) && get(g:, 'fzf_buffers_jump')
692702
let [t, w] = s:find_open_window(b)
693703
if t
694704
call s:jump(t, w)
695705
return
696706
endif
697707
endif
698-
let cmd = s:action_for(a:lines[0])
708+
709+
let cmd = s:action_for(key)
699710
if !empty(cmd)
700711
execute 'silent' cmd
701712
endif
713+
702714
execute 'buffer' b
703715
endfunction
704716

@@ -728,13 +740,17 @@ endfunction
728740
function! fzf#vim#buffers(...)
729741
let [query, args] = (a:0 && type(a:1) == type('')) ?
730742
\ [a:1, a:000[1:]] : ['', a:000]
743+
744+
let expected_keys = 'ctrl-d,' . join(keys(s:default_action), ',')
745+
let ctrl_d_header = ["\t\t:: Press ".s:magenta('CTRL-D', 'Special')." to delete"]
731746
let sorted = fzf#vim#_buflisted_sorted()
732-
let header_lines = '--header-lines=' . (bufnr('') == get(sorted, 0, 0) ? 1 : 0)
747+
let header_lines = '--header-lines=' . (bufnr('') == get(sorted, 0, 0) ? 2 : 1)
733748
let tabstop = len(sorted) >= 1000 ? 9 : 8
749+
734750
return s:fzf('buffers', {
735-
\ 'source': map(sorted, 'fzf#vim#_format_buffer(v:val)'),
751+
\ 'source': ctrl_d_header + map(sorted, 'fzf#vim#_format_buffer(v:val)'),
736752
\ 'sink*': s:function('s:bufopen'),
737-
\ '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]
753+
\ '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, '-m', '--expect', expected_keys]
738754
\}, args)
739755
endfunction
740756

0 commit comments

Comments
 (0)