2011-01-19 74 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