2011-01-19 95 views
50

替換如果我做這樣的事情:多個搜索和一行

:%s/AAA/BBB/| %s/111/222/

並且第一次搜索和替換沒有找到任何匹配,第二次搜索和替換將不會執行。即使命令「失敗」,有沒有什麼辦法可以告訴vim繼續?

回答

78

嘗試

:%s/aaa/bbb/e | %s/111/222/e 

和閱讀

:help :s_flags 

特別是在[E]條目:

When the search pattern fails, do not issue an error message and, in 
particular, continue in maps as if no error occurred. This is most 
useful to prevent the "No match" error from breaking a mapping. Vim 
does not suppress the following error messages, however: 
Regular expressions can't be delimited by letters 
\ should be followed by /, ? or & 
No previous substitute regular expression 
Trailing characters 
Interrupted 
+2

注意前綴每`s`以``%。當我嘗試這個時,我不小心只在第一個替換前放了一個`%`。該命令仍然可以正常運行,但不會執行您希望執行的替換。 – josch 2016-12-15 14:58:47