Vim 如何删除所有隐藏的缓冲区?

Vim 如何删除所有隐藏的缓冲区?,vim,Vim,我喜欢在启用'hidden'的情况下运行Vim。不过有时候,我有很多隐藏的缓冲区,我想把它们都杀掉。我可以使用什么命令来删除缓冲区列表中的每个隐藏缓冲区?这没有经过彻底的测试,所以先暂时使用它 function! DeleteHiddenBuffers() let i=1 let lastbuf=bufnr("$") while i <= lastbuf if buflisted(i) && bufwinnr(i) == -1

我喜欢在启用
'hidden'
的情况下运行Vim。不过有时候,我有很多隐藏的缓冲区,我想把它们都杀掉。我可以使用什么命令来删除缓冲区列表中的每个隐藏缓冲区?

这没有经过彻底的测试,所以先暂时使用它

function! DeleteHiddenBuffers()
    let i=1
    let lastbuf=bufnr("$")
    while i <= lastbuf
        if buflisted(i) && bufwinnr(i) == -1
        sil exe "bdelete" i
        endif
        let i=i+1
    endwhile
endfunction

或者创建一个映射

这里的方式与之前由王子Goulash发布的函数略有不同。代码未经测试。它使用一个函数来解析
:buffers
命令的输出,该命令包括隐藏缓冲区的“h”标记。如下所示:

function! DeleteHiddenBuffers()
    redir => buffersoutput
    buffers
    redir END
    let buflist = split(buffersoutput,"\n")
    for item in filter(buflist,"v:val[5] == 'h'")
            exec 'bdelete ' . item[:2]
    endfor
endfunction

bufexplorer.vim可以管理您的vim缓冲区。
. 您可以使用
:BufExplorer
在窗口中显示所有vim缓冲区。然后按“d”将其删除。

尝试以下功能:

function DeleteHiddenBuffers()
    let tpbl=[]
    call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
    for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)==-1')
        silent execute 'bwipeout' buf
    endfor
endfunction
以下是我使用的:

:bufdo bd
不需要插件或任何东西。

扩展版的answer,它跳过修改过的缓冲区并输出关闭的缓冲区数量

function! DeleteHiddenBuffers()
  let tpbl=[]
  let closed = 0
  call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
  for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)==-1')
    if getbufvar(buf, '&mod') == 0
      silent execute 'bwipeout' buf
      let closed += 1
    endif
  endfor
  echo "Closed ".closed." hidden buffers"
endfunction
此插件: 具有关闭所有隐藏缓冲区的命令
:b删除隐藏的

function! DeleteBuffers(action)
"action=1 delete all unnamed buffers
"action=2 delete all unmodified buffers
"action=3 delete all buffers which are both unnamed and unmodified
"action=4 delete all hidden buffers
"action=5 delete all buffers which are both hidden and unnamed 
"action=6 delte all bufferes which are both hidden and unmodified
"action=7 delete all buffers which are all of hidden, unmodified and unnamed.
"action=8 delete all buffers which are not loaded.
    for buf in getbufinfo()
    if a:action == 1
        if strlen(buf.name) == 0
            silent execute 'bwipeout!' buf.bufnr
        endif  
    elseif a:action == 2
        if buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 3
        if strlen(buf.name) == 0 && buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 4
        if buf.hidden != 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 5
        if buf.hidden != 0  && strlen(buf.name) == 0
           silent execute 'bwipeout!' buf.bufnr
         endif
    elseif a:action == 6
        if buf.hidden != 0  && buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 7
         if buf.hidden != 0  && buf.changed == 0 && strlen(buf.name) == 0
             silent execute 'bwipeout!' buf.bufnr
         endif
    elseif a:action == 8
         if buf.loaded == 0 
              silent execute 'bwipeout!' buf.bufnr
         endif
      endif
endfor
endfunction

上面是我的DeleteBuffers函数。没有像地图,扩展,过滤器等复杂的东西。。。在任何模式下,每个调用都可以根据需要绑定到任何键映射。可以根据需要轻松修改。

看起来是一个不错的解决方案,但我认为如果人们使用标签,它不会起作用
bufwinnr
对于在窗口中打开的缓冲区,在当前选项卡以外的选项卡上返回-1。与往常一样,这是一个有趣的解决方案。我偶尔使用
map
,但从不使用
filter
;我修改了自己的解决方案以使用
过滤器
。谢谢。@Rican7我打赌它不仅会打破nerdtree。例如,Command-T打开其缓冲区一次,但不希望它关闭(或者,至少在我碰巧遇到问题的过去版本中,不希望它关闭)。如果您看到这样的问题
&&!空(getbufvar(v:val,&buftype))
可能会有所帮助。或者
&&index([“help”,“”],getbufvar(v:val,&buftype”)!=-1
因为
:help
可能会产生一堆缓冲区。@ZyX哇,你太棒了!这就解决了!虽然,我不得不反转布尔逻辑,因为条件是在一个过滤器上:
&&empty(getbufvar(v:val,&buftype))
我本来想把它转换成一个插件,但是其他人做了:(唉,没有属性,尽管它显然是从这个答案中剪切和粘贴的)。从技术上讲,这确实删除了所有隐藏的缓冲区。它还会删除其余的缓冲区。:)
function! DeleteBuffers(action)
"action=1 delete all unnamed buffers
"action=2 delete all unmodified buffers
"action=3 delete all buffers which are both unnamed and unmodified
"action=4 delete all hidden buffers
"action=5 delete all buffers which are both hidden and unnamed 
"action=6 delte all bufferes which are both hidden and unmodified
"action=7 delete all buffers which are all of hidden, unmodified and unnamed.
"action=8 delete all buffers which are not loaded.
    for buf in getbufinfo()
    if a:action == 1
        if strlen(buf.name) == 0
            silent execute 'bwipeout!' buf.bufnr
        endif  
    elseif a:action == 2
        if buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 3
        if strlen(buf.name) == 0 && buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 4
        if buf.hidden != 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 5
        if buf.hidden != 0  && strlen(buf.name) == 0
           silent execute 'bwipeout!' buf.bufnr
         endif
    elseif a:action == 6
        if buf.hidden != 0  && buf.changed == 0
            silent execute 'bwipeout!' buf.bufnr
        endif
    elseif a:action == 7
         if buf.hidden != 0  && buf.changed == 0 && strlen(buf.name) == 0
             silent execute 'bwipeout!' buf.bufnr
         endif
    elseif a:action == 8
         if buf.loaded == 0 
              silent execute 'bwipeout!' buf.bufnr
         endif
      endif
endfor
endfunction