2010-06-02 34 views
3

通常Vim的全局命令:g //以每行爲基礎工作。是否有可能使其按每個事件的基礎進行工作,因爲一行中可能有多個事件。如何製作Vim的全局命令:g /按每個事件處理

+1

回答這個問題需要有關vim的腳本知識。 AFAIK,SU不是關於編程 - 而答案需要編程技巧...... – 2010-06-02 11:30:23

+3

@Don:即使這不需要腳本化解決方案(因爲功能不是內置的),這仍然是正確的放置它。 http://meta.stackexchange.com/questions/25925/vim-questions-so-or-su – Cascabel 2010-06-02 12:58:02

+0

你需要在每一次事件中做什麼操作?爲什麼你需要一個在線操作的操作員? – 2010-06-02 14:54:37

回答

0

不是一個直接的答案,但你可以使用類似於:rubydo,它將在每行代碼中運行一些ruby scriptlet。把它與紅寶石中的gsub結合起來應該讓你能夠在每次匹配時做任何事情。當然,你需要使用Ruby代碼來做到這一點,這可能不會讓你訪問,您可能需要毫不費力的一切(註冊追加將惱人的實例)

:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the 
         [range], with $_ being set to the text of each line in 
         turn, without a trailing <EOL>. Setting $_ will change 
         the text, but note that it is not possible to add or 
         delete lines using this command. 
         The default for [range] is the whole file: "1,$". 
0

你可以試試:

command! -bang -nargs=1 -range OGlobal 
    \ <line1>,<line2>call s:Global("<bang>", <f-args>) 

function! s:Global(bang, param) range 
    let inverse = a:bang == '!' 

    " obtain the separator character 
    let sep = a:param[0] 
    " obtain all fields in the initial command 
    let fields = split(a:param, sep) 

    " todo: handle inverse 
    let l = a:firstline 
    while 1 
    let l = search(fields[0], 'W') 
    if l == -1 || l > a:lastline 
     break 
    endif 
    exe fields[1] 
    endwhile 
endfunction 

,你可以爲:global使用,除了命令名稱是不同的(並且,由於尚未實現的爆炸選項)