2017-09-27 26 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' 
相關問題