假设我在Vim中打开了多个文件作为缓冲区.文件有*.cpp,*.h有些是*.xml.我想关闭所有的XML文件:bd *.xml.但是,Vim不允许这样做(E93:不止一场比赛......).
有没有办法做到这一点?
PS我知道:bd file1 file2 file3有效.所以,我可以采用某种评估*.xml到file1.xml file2.xml file3.xml?
Bjö*_*ink 172
您可以<C-a>用来完成所有比赛.因此,如果您键入:bd *.xml然后点击<C-a>,vim将完成命令:bd file1.xml file2.xml file3.xml.
Fis*_*man 33
:3,5bd[elete]
Run Code Online (Sandbox Code Playgroud)
将删除缓冲区范围从3到5.
Cyl*_*ian 16
你也可以使用:
:.,$-bd[elete] " to delete buffers from the current one to last but one
:%bd[elete] " to delete all buffers
Run Code Online (Sandbox Code Playgroud)
你可以用它.
:exe 'bd '. join(filter(map(copy(range(1, bufnr('$'))), 'bufname(v:val)'), 'v:val =~ "\.xml$"'), ' ')
Run Code Online (Sandbox Code Playgroud)
将它添加到命令应该很容易.
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>)
Run Code Online (Sandbox Code Playgroud)
试试下面的脚本.该示例用于"txt",根据需要将其更改为例如"xml".修改的缓冲区不会被删除.按\ bd删除缓冲区.
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
Run Code Online (Sandbox Code Playgroud)
[编辑] 同样没有:bufdo(根据Luc Hermitte的要求,见下面的评论)
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
Run Code Online (Sandbox Code Playgroud)