2012-10-22 61 views
4

在vimscript中,如何遍历当前文件中正则表达式的所有匹配项,然后为每个结果运行shell命令?遍历正则表达式结果vimscript

我认为这是一个开始,但我无法弄清楚如何喂它整个文件,并得到每场比赛。

while search(ENTIRE_FILE, ".*{{\zs.*\ze}}", 'nw') > 0 
    system(do something with THIS_MATCH) 
endwhile 

回答

0

您可以改为使用subtitute()。例如...

call substitute(readfile(expand('%')), '.*{{\zs.*\ze}}', 
    \ '\=system("!dosomething ".submatch(1))', 'g') 
1

假设我们有内容的文件:

123 a shouldmatch 
456 b shouldmatch 
111 c notmatch 

,我们喜欢用正则表达式

.*shouldmatch 
匹配

123 a shouldmatch 
456 b shouldmatch 

如果你只有一个比赛每li您可以使用readfile(),然后循环播放各行,并使用matchstr()检查每行。 [1]

function! Test001() 
    let file = readfile(expand("%:p")) " read current file 
    for line in file 
    let match = matchstr(line, '.*shouldmatch') " regex match 
    if(!empty(match)) 
     echo match 
     " your command with match 
    endif 
    endfor 
endfunction 

你可以把这个功能您~/.vimrccall Test001()调用它。

[1] http://vimdoc.sourceforge.net/htmldoc/eval.html#matchstr%28%29