tags:

views:

157

answers:

2

Is there a way in vim to close all files (buffers, let's not get into that) from some directory and its subdirectories?

+8  A: 

Put the following into your .vimrc or in some custom file inside vim plugin folder.

function! s:close_buffers(name_regexp)
    for buffer_number in range(1, bufnr('$'))
        if !buflisted(buffer_number)
            continue
        endif

        let name = fnamemodify(bufname( buffer_number ), ':p')
        if name =~ a:name_regexp
            exec 'bdelete '.buffer_number
        endif
    endfor
endfunction

command! -nargs=1 CloseBuffers call s:close_buffers(<f-args>)

Use commands like

:CloseBuffers include
:CloseBuffers in.*e

to close buffers which name matches passed regexp.

That means that to close all files from the certain folder you can use

:CloseBuffers workspace/cpp
:CloseBuffers /home/my/project

To close all the files from the current dir and all subdirs

:exec "CloseBuffers ".getcwd()
Mykola Golubyev
+4  A: 

You can do it this way fairly concisely:

:silent! bufdo if expand('%')=~"some_regex"|bd|endif

Or if you want absolute pathnames instead of relative:

:silent! bufdo if expand('%:p')=~"some_regex"|bd|endif

Or if you want it to prompt you for the regex interactively you could set this up as a mapping:

:let regex=input("> ")|silent! bufdo if expand('%:p')=~regex|bd|endif

etc. etc.

Brian Carper