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

时间:2011-12-09 19:49:25

标签: vim

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

8 个答案:

答案 0 :(得分:20)

尝试以下功能:

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

答案 1 :(得分:5)

这与Prince 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

答案 2 :(得分:3)

@ZyX答案的扩展版本,它跳过修改后的缓冲区并输出已关闭的缓冲区数。

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

答案 3 :(得分:1)

bufexplorer.vim可以管理你的vim缓冲区。 Here to download 。您可以使用:BufExplorer在窗口中显示所有vim缓冲区。然后按'd'删除它。

答案 4 :(得分:0)

这未经过彻底测试,因此请先尝试使用它!

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

注意buflisted检查缓冲区是否存在,bufwinnr如果隐藏则返回-1(即没有指定的窗口)。你可以用

来调用它
call DeleteHiddenBuffers()

或创建映射。

答案 5 :(得分:0)

此插件:https://github.com/Asheq/close-buffers.vim 有一个命令:Bdelete hidden关闭所有隐藏的缓冲区。

答案 6 :(得分:0)

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函数。没有复杂的内容,例如map,extend,filter等。每个调用都可以在任何模式下根据需要绑定到任何键映射。可以根据要求轻松修改。

答案 7 :(得分:-4)

这是我使用的:

:bufdo bd

不需要插件或任何东西。