There is a popular mapping that will show the :ls
result above a prompt:
:nnoremap <leader>ls :ls<cr>:b<space>
Instead of showing :b
, as in that mapping, we'll show :bd
instead:
:nnoremap <leader>lsd :ls<cr>:bd<space>
(If you prefer a different mapping than lsd
, heh, feel free to change it.)
Append whatever buffer numbers you want to delete, space separated, and hit Enter.
If you'd rather enter ranges of buffer numbers, like :2,5bd
this variation will put the cursor at the beginning, before bd
:
:nnoremap <leader>lsd :ls<cr>:bd<home>
But you have to pick one or the other or use two mappings. Personally I use the first mapping and hit Home if I want to enter a range.
Bonus Update: Wanted to stretch my vimscript muscles so came up with this...
" Displays buffer list, prompts for buffer numbers and ranges and deletes
" associated buffers. Example input: 2 5,9 12
" Hit Enter alone to exit.
function! InteractiveBufDelete()
let l:prompt = "Specify buffers to delete: "
ls | let bufnums = input(l:prompt)
while strlen(bufnums)
echo "\n"
let buflist = split(bufnums)
for bufitem in buflist
if match(bufitem, '^\d\+,\d\+$') >= 0
exec ':' . bufitem . 'bd'
elseif match(bufitem, '^\d\+$') >= 0
exec ':bd ' . bufitem
else
echohl ErrorMsg | echo 'Not a number or range: ' . bufitem | echohl None
endif
endfor
ls | let bufnums = input(l:prompt)
endwhile
endfunction
Put in your vimrc file along with a mapping like this...
nnoremap <silent> <leader>bd :call InteractiveBufDelete()<CR>
Now if you hit \bd
in Normal mode you'll see the buffer listing and a prompt. Enter space-separated numbers or ranges, such as 2 5,8 11
, and hit Enter and all associated buffers will be deleted. The process will repeat until you hit Enter alone.
(I'll leave it to you to add any additional validation or error checking beyond what I have.)