在vim脚本/函数中迭代匹配的最简单/最优雅的方法是什么?
我正在寻找一个优雅的解决方案(在vim脚本中)迭代所有匹配的 缓冲区中的正则表达式。那大概是在vim脚本/函数中迭代匹配的最简单/最优雅的方法是什么?,vim,Vim,我正在寻找一个优雅的解决方案(在vim脚本中)迭代所有匹配的 缓冲区中的正则表达式。那大概是 fu! DoSpecialThingsWithMatchedLines() for matched_line_no in (GetMatchedPositions("/foo\\>.*\\<bar")) let line = getline(matched_line_no) call DoItPlease(line) end for endfu fu!符
fu! DoSpecialThingsWithMatchedLines()
for matched_line_no in (GetMatchedPositions("/foo\\>.*\\<bar"))
let line = getline(matched_line_no)
call DoItPlease(line)
end for
endfu
fu!符合条件的特殊情况()
对于(GetMatchedPositions(“/foo\\>.\\\\中的匹配行号),您可以使用:he:global例如
:%g/foo\\>.*\\<bar/call DoItPlease(getline("."))
大多数情况下,我会将michael的解决方案用于:global
您还可以使用filter(getline('1','$),'v:val=~“foo\\>.\\\\\.\\\\\\\\\\\这种方法当然满足了我的功能要求(ed)。然而,我希望在构造中有一个for..,这样我就可以直观地看到发生了什么。
fun! Doit(line)
echo a:line
endfun
fun! MyDo()
%g/foo/call Doit(getline("."))
endfun
:call MyDo()
:call map(filter(getline('1','$'), 'v:val =~ "foo\\>.*\\<bar"'), 'DoItPlease(v:val)')