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

后端 未结 5 1677
野趣味
野趣味 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:02

    Here's an alternative solution you can drop in your .vimrc:

    function! Wipeout()
      " list of *all* buffer numbers
      let l:buffers = range(1, bufnr('$'))
    
      " what tab page are we in?
      let l:currentTab = tabpagenr()
      try
        " go through all tab pages
        let l:tab = 0
        while l:tab < tabpagenr('$')
          let l:tab += 1
    
          " go through all windows
          let l:win = 0
          while l:win < winnr('$')
            let l:win += 1
            " whatever buffer is in this window in this tab, remove it from
            " l:buffers list
            let l:thisbuf = winbufnr(l:win)
            call remove(l:buffers, index(l:buffers, l:thisbuf))
          endwhile
        endwhile
    
        " if there are any buffers left, delete them
        if len(l:buffers)
          execute 'bwipeout' join(l:buffers)
        endif
      finally
        " go back to our original tab page
        execute 'tabnext' l:currentTab
      endtry
    endfunction
    

    Use :call Wipeout().

提交回复
热议问题