How do I close all buffers that aren't shown in a window in vim?

后端 未结 5 1649
野趣味
野趣味 2020-12-23 16:23

I have a horde of buffers open in vim, with only a few of them open in split windows or on other tabs. Is there a way to close all but the ones that are currently visible i

5条回答
  •  礼貌的吻别
    2020-12-23 17:07

    Yet another take on this. Using the example given in the Vim help for tabpagebuflist() to get a list of the buffers that appear in a tab or window. I have the below in my .vimrc

    function! DeleteInactiveBufs()
        "From tabpagebuflist() help, get a list of all buffers in all tabs
        let tablist = []
        for i in range(tabpagenr('$'))
            call extend(tablist, tabpagebuflist(i + 1))
        endfor
    
        "Below originally inspired by Hara Krishna Dara and Keith Roberts
        "http://tech.groups.yahoo.com/group/vim/message/56425
        let nWipeouts = 0
        for i in range(1, bufnr('$'))
            if bufexists(i) && !getbufvar(i,"&mod") && index(tablist, i) == -1
            "bufno exists AND isn't modified AND isn't in the list of buffers open in windows and tabs
                silent exec 'bwipeout' i
                let nWipeouts = nWipeouts + 1
            endif
        endfor
        echomsg nWipeouts . ' buffer(s) wiped out'
    endfunction
    command! Bdi :call DeleteInactiveBufs()
    

提交回复
热议问题