vim,如何在搜索行之前搜索文本并添加新行

时间:2014-11-03 07:33:59

标签: vim

Input :

<Action name="Compile" />
<Action name="Debug" />


Ouput:

<Action name="Parse" />
<Action name="Compile" />
<Action name="Debug" />

使用vim,我如何搜索行开头包含单词&#34;编译&#34;并在前面加上另一条线?

我累了:%s,但它没有替换整行

2 个答案:

答案 0 :(得分:3)

我会做Prince Goulash的方式但是......

:%s/^.*Compile/<Action name="Parse" \/>\r&

^.*Compile                     " matches everything from the first char on the line 
                               " up to and including 'Compile'
<Action name="Parse" \/>\r&    " replaces the match with the new desired line,
                               " followed by a newline, followed by the match

非常优雅......

:g/Compile/t-|s//Parse

:g/Compile/t-                  " copies the matching line above itself
:s//Parse                      " substitutes the last search pattern with 'Parse'
                               " on that new line

答案 1 :(得分:2)

您可以使用:g命令搜索行,然后在每个匹配的行上提供:normal!命令:

:g/Compile/normal! O<Action name="Parse" />

!确保:normal来电不会调用用户映射。