tags:

views:

24

answers:

2

I often want to wipe all buffers loaded with a given extension (usually .rej files produced by patch). Just doing :bw[!] *.rej will complain if there is more than one match. Does anyone have any good tips? Currently I either repeatedly use :bw *.rej + tab-complete or, if there are a lot of buffers, use :ls and :bw a set of buffers by number.

A: 

Globbing in vim is a bit difficult (apart from for files on the file system). Therefore, the best way seems to be to convert the wildcard into a regular expression and then check each buffer in the buffer list to see whether it matches. Something like this:

" A command to make invocation easier
command! -complete=buffer -nargs=+ BWipe call BWipe(<f-args>)

function! BWipe(...)
    let bufnames = []
    " Get a list of all the buffers
    for bufnumber in range(0, bufnr('$'))
        if buflisted(bufnumber)
            call add(bufnames, bufname(bufnumber))
        endif
    endfor
    for argument in a:000
        " Escape any backslashes, dots or spaces in the argument
        let this_argument = escape(argument, '\ .')
        " Turn * into .* for a regular expression match
        let this_argument = substitute(this_argument, '\*', '.*', '')

        " Iterate through the buffers
        for buffername in bufnames
            " If they match the provided regex and the buffer still exists
            " delete the buffer
            if match(buffername, this_argument) != -1 && bufexists(buffername)
                exe 'bwipe' buffername
            endif
        endfor
    endfor
endfunction

It can be used as:

:BWipe *.rej

or:

:BWipe *.c *.h
Al
That is very helpful, thanks!
Luke
A: 

By the way, I ended up going with a very low-tech solution (I personally like to modify vim as little as possible so that I am at home on any machine):

I added a mapping:

:map <C-f> :bw *.rej

Then I repeatedly press <C-f> <Tab> <CR>

Luke