Let assume that I have multiple files opened as buffers in Vim. The files are
*.md and some
I want to close all the HTML files with
:bd *.html. However, Vim does not allow
this; It throws an error:
E93: More than one match for *.md.
So how can we achieve this?
0. Basic Way
The basic way would be writing all file names and using
:bd file1.html file2.html file3.html
1. Exe Way
You can use buffer numbers:
:ls, which lists all buffers, then pick the buffers that you want to
close. For instance:
deletes buffer range from 2 to 7.
2. Vim Way (Preferred method)
If you exe
:bd *.html and then press
<C-a> it completes all file matches.
3. Vim Script Way
If you usually execute
:bd and don’t want to use Vim Way
then you can write a
function! s:DeleteBufferByExtension(ext) let buffers = filter(range(1, bufnr('$')), 'buflisted(v:val) && bufname(v:val) =~ "\.'.a:ext.'$"') if empty(buffers) |throw "no *.".a:ext." buffer" | endif exe 'bd '.join(buffers, ' ') endfunction command! -nargs=1 DeleteBufferByExtension :call s:DeleteBufferByExtension(<f-args>)
This function takes file extenstion as an arg, find files with this
extension and execute
:bd. It can be run like below:
You can map this function to a key as well.