How to remove multiple buffers in Vim?

Assuming I have several files open as buffers in Vim. Files have *.cpp , *.h , and some have *.xml . I want to close all XML files with :bd *.xml . However, Vim does not allow this (E93: more than one match ...).

Is there any way to do this?

PS I know what works :bd file1 file2 file3 . Can I somehow evaluate *.xml on file1.xml file2.xml file3.xml ?

+80
vim buffer
Jul 01 '10 at 6:16
source share
8 answers

You can use <Ca> to make all matches. Therefore, if you type :bd *.xml and then press <Ca> , vim will end the command before :bd file1.xml file2.xml file3.xml .

+127
Feb 29 2018-12-12T00:
source share

You can also use alternatively:

  :.,$-bd[elete] " to delete buffers from the current one to last but one :%bd[elete] " to delete all buffers 
+8
Jan 19 '16 at 6:40
source share
 :3,5bd[elete] 

Deletes a buffer range from 3 to 5.

+7
Jan 04 '17 at 7:24
source share

You can use this.

 :exe 'bd '. join(filter(map(copy(range(1, bufnr('$'))), 'bufname(v:val)'), 'v:val =~ "\.xml$"'), ' ') 

This should be fairly easy to add to the team.

 function! s:BDExt(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 BDExt :call s:BDExt(<f-args>) 
+4
Jul 01 '10 at 9:30
source share

Try the script below. Example for "txt", if necessary, change it, for example. to "xml". Modified buffers are not deleted. Press \ bd to remove the buffers.

 map <Leader>bd :bufdo call <SID>DeleteBufferByExtension("txt") function! <SID>DeleteBufferByExtension(strExt) if (matchstr(bufname("%"), ".".a:strExt."$") == ".".a:strExt ) if (! &modified) bd endif endif endfunction 

[edit] Same without: bufdo (at the request of Luke Hermit, see comment below)

 map <Leader>bd :call <SID>DeleteBufferByExtension("txt") function! <SID>DeleteBufferByExtension(strExt) let s:bufNr = bufnr("$") while s:bufNr > 0 if buflisted(s:bufNr) if (matchstr(bufname(s:bufNr), ".".a:strExt."$") == ".".a:strExt ) if getbufvar(s:bufNr, '&modified') == 0 execute "bd ".s:bufNr endif endif endif let s:bufNr = s:bufNr-1 endwhile endfunction 
+4
Jul 01 '10 at 10:45
source share

I also had a need for this feature all the time. This is the solution I have in my vimrc.

 function! GetBufferList() return filter(range(1,bufnr('$')), 'buflisted(v:val)') endfunction function! GetMatchingBuffers(pattern) return filter(GetBufferList(), 'bufname(v:val) =~ a:pattern') endfunction function! WipeMatchingBuffers(pattern) let l:matchList = GetMatchingBuffers(a:pattern) let l:count = len(l:matchList) if l:count < 1 echo 'No buffers found matching pattern ' . a:pattern return endif if l:count == 1 let l:suffix = '' else let l:suffix = 's' endif exec 'bw ' . join(l:matchList, ' ') echo 'Wiped ' . l:count . ' buffer' . l:suffix . '.' endfunction command! -nargs=1 BW call WipeMatchingBuffers('<args>') 

Now I can just do :BW regex (for example :BW \.cpp$ and erase all the corresponding buffers that match this pattern in their path name.

If you want to delete, not erase, you can, of course, replace exec 'bw ' . join(l:matchList, ' ') exec 'bw ' . join(l:matchList, ' ') to exec 'bd ' . join(l:matchList, ' ') exec 'bd ' . join(l:matchList, ' ')

+2
01 Feb 2018-11-11T00:
source share

Very simple: use the command :bd[elete] . For example :bd[elete] buf#1 buf#5 buf#3 will delete buffers 1, 3, and 5.

+1
Oct 19 '14 at 18:33
source share

TAB will only autofill one file for you with Vim 7.4.282
use <ca> to autocomplete all files.

You can simply use:

 bd filetype 

then simply use <ca> to facilitate the execution of all open files of the specified file type.

for example, you have 1.xml, 2.xml, 3.xml and 4.xml, you can do:

 bd xml 

then press <ca>

vim will be autocompleted for you as follows:

 bd 1.xml 2.xml 3.xml 4.xml 

you can just press Enter to execute the command.

if you made changes to one of the files mentioned above, do not forget to do:

 bd! xml 
0
May 16 '16 at 12:45
source share



All Articles