How to close all buffers that do not appear in a window in vim?

I have a horde of buffers open in vim, and only some of them open in split windows or other tabs. Is there a way to close everything except those that are currently visible in one of these sections or tabs?

+37
vim
07 Oct '09 at 23:47
source share
5 answers

Here's an alternative solution that you can add to 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() .

+21
08 Oct '09 at 7:13
source share

One more example. Using the example provided in the Vim help for tabpagebuflist() to get a list of buffers that appear in a bookmark or window. I have 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() 
+24
Sep 06 2018-11-11T00:
source share

Add this to your .vimrc:

 function! CloseHiddenBuffers() let i = 0 let n = bufnr('$') while i < n let i = i + 1 if bufloaded(i) && bufwinnr(i) < 0 exe 'bd ' . i endif endwhile endfun 

Then you can do this to close hidden buffers:

 :call CloseHiddenBuffers() 

(You probably want to bind a key or command to it.)

Update:

The version for tab support has been updated here. (I myself do not use tabs, so I did not understand that bufwinnr only works for windows on the current page).

 function! CloseHiddenBuffers() " figure out which buffers are visible in any tab let visible = {} for t in range(1, tabpagenr('$')) for b in tabpagebuflist(t) let visible[b] = 1 endfor endfor " close any buffer that loaded and not visible for b in range(1, bufnr('$')) if bufloaded(b) && !has_key(visible, b) exe 'bd ' . b endif endfor endfun 
+8
Oct 08 '09 at 0:26
source share

I know why the second script is not working correctly.

This is due to the bufloaded () function, which should be bufexits ()!

Indeed, the delete buffer does not load! Just remove this condition in order, but it makes some warnings when we try to destroy a buffer that is not in use, so we should use bufexists (b).

The final decision is as follows:

 function! CloseHiddenBuffers() " Tableau pour memoriser la visibilite des buffers let visible = {} " Pour chaque onglet... for t in range(1, tabpagenr('$')) " Et pour chacune de ses fenetres... for b in tabpagebuflist(t) " On indique que le buffer est visible. let visible[b] = 1 endfor endfor " Pour chaque numero de buffer possible... for b in range(1, bufnr('$')) " Si b est un numero de buffer valide et qu'il n'est pas visible, on le " supprime. if bufexists(b) && !has_key(visible, b) " On ferme donc tous les buffers qui ne valent pas 1 dans le tableau et qui " sont pourtant charges en memoire. execute 'bwipeout' b endif endfor endfun 

Thanks you.

+3
Feb 07 '10 at 9:56
source share

There's a plugin that does just that and a little more!

Check out close-buffers.vim

0
Sep 27 '17 at 1:50
source share



All Articles