2010-08-17 30 views
1

我經常想要擦除加載了給定擴展名的所有緩衝區(通常是由補丁生成的.rej文件)。只是在做:bw [!] * .rej會抱怨如果有多個匹配。有沒有人有任何好的提示?目前我要麼重複使用:bw * .rej + tab-complete,或者如果有很多緩衝區,請使用:ls和:bw一組數字的緩衝區。如何獲得:bwipe * .ext擦除匹配vim中的通配符的所有人

+0

聽起來像是你可能需要編寫(或發現)一個插件來做到這一點。 :bwipe只會像你所經歷的那樣需要一個'bufname'。 – cam 2010-08-17 21:59:19

回答

1

在vim中泛化有點困難(除了文件系統上的文件)。因此,最好的方法似乎是將通配符轉換爲正則表達式,然後檢查緩衝區列表中的每個緩衝區以查看它是否匹配。事情是這樣的:

" 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 

它可以作爲:

:BWipe *.rej 

或:

:BWipe *.c *.h 
+0

這非常有幫助,謝謝! – Luke 2010-08-20 16:48:01

0

順便說一句,我結束了一個非常低技術的解決方案去(我個人喜歡儘可能少的修改vim以便我在任何機器上都可以):

我添加了一個映射:

:地圖<比照>:BW *名爲.rej

後來我反覆按 <比照> <標籤> <CR>

+0

聽起來合理。通過在可用的任意版本控制下保留整個'.vim'(或Windows上的vimfiles)和vimrc,我可以繞過「在任何機器的家中」問題。這樣,我所做的任何更改都會被推送到我正在使用的任何一臺機器(或USB存儲棒)。 – DrAl 2011-01-11 18:00:48

相關問題