在vimscript中,如何遍歷當前文件中正則表達式的所有匹配項,然後爲每個結果運行shell命令?遍歷正則表達式結果vimscript
我認爲這是一個開始,但我無法弄清楚如何餵它整個文件,並得到每場比賽。
while search(ENTIRE_FILE, ".*{{\zs.*\ze}}", 'nw') > 0
system(do something with THIS_MATCH)
endwhile
在vimscript中,如何遍歷當前文件中正則表達式的所有匹配項,然後爲每個結果運行shell命令?遍歷正則表達式結果vimscript
我認爲這是一個開始,但我無法弄清楚如何餵它整個文件,並得到每場比賽。
while search(ENTIRE_FILE, ".*{{\zs.*\ze}}", 'nw') > 0
system(do something with THIS_MATCH)
endwhile
您可以改爲使用subtitute()
。例如...
call substitute(readfile(expand('%')), '.*{{\zs.*\ze}}',
\ '\=system("!dosomething ".submatch(1))', 'g')
假設我們有內容的文件:
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
你可以把這個功能您~/.vimrc
與call Test001()
調用它。
[1] http://vimdoc.sourceforge.net/htmldoc/eval.html#matchstr%28%29