2017-09-27 25 views
0

我知道简单的搜索一些东西附加和替换命令(:%s/apple/orange/g)在vim,我们发现所有的“苹果”和与“橙色”替换它们。VIM:找到一个模式,跳过一个字,并在所有匹配的行

但是,它可以做到在vim这样的事情? 找到所有的“小麦”的文件,并跳过下一个字(如果有的话)之后追加“专卖店”?

例: 原始文件内容:

Wheat flour 
Wheat bread 
Rice flour 
Wheat 

搜索后并替换:

Wheat flour store 
Wheat bread store 
Rice flour 
Wheat store 

回答

5

这是使用global命令的最佳时机。这将采用命令每一个给定的正则表达式匹配线。

     *:g* *:global* *E147* *E148* 
:[range]g[lobal]/{pattern}/[cmd] 
      Execute the Ex command [cmd] (default ":p") on the 
      lines within [range] where {pattern} matches. 

在这种情况下,该命令是norm A store和正则表达式是wheat。所以,把他们放在一起,我们有

:g/Wheat/norm A store 

现在,你可能这与替代命令,但我觉得全球是一个很大的方便性和可读性。在这种情况下,你必须:

:%s/Wheat.*/& store 

这意味着:

:%s/    " On every line, replace... 
    Wheat   " Wheat 
     .*   " Followed by anything 
     /  " with... 
      &  " The entire line we matched 
       store " Followed by 'store' 
相关问题