Vim:忽略映射替换列表中的错误
我经常进行许多正则表达式替换,因此希望映射一个键来轻松完成此操作。但是,如果在文件中找不到要替换的正则表达式之一,则我的键映射会由于 E486:未找到模式
而停止,并且不会执行进一步的替换。例如,如果我的 vimrc 中的映射是,
map <F12> :%s/spam/foo/<enter>:%s/ham/bar/<enter>:%s/eggs/baz/<enter>
并且我的文件中没有 ham
,则 eggs
将不会被替换。我可以在替换上放置一个标志来忽略错误吗?我读过 :help regex
和 :help map
但没有找到任何可用的东西。
或者,是否还有另一种我所缺少的、更简单、更明显的方法来做到这一点?
I have a number of regex substitutions I regularly do, and therefore want to map a key to do this easily. However, if one of the regexes to be substituted aren't found in the file, my key mapping stops due to E486: Pattern not found
and no further replacements are carried out. E.g. if the mapping in my vimrc is
map <F12> :%s/spam/foo/<enter>:%s/ham/bar/<enter>:%s/eggs/baz/<enter>
and there is no ham
in my file, eggs
won't get substituted. Is there a flag I can put on the substitutions to ignore errors? I've read :help regex
and :help map
without finding anything usable.
Alternately, is there another, easier, more obvious way to do this that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
/e
标志添加到每个替换命令中。它不会显示错误消息并继续,就像没有发生错误一样。这将是新的映射:Add the
/e
flag to each substitution command. It doesn't show error messages and continues as if no error had ocurred. This would be the new mapping:您可以在模式末尾添加一个“标志”:
如果没有找到
垃圾邮件
,则不会发出错误并允许您继续。请参阅
:帮助标志
。You can add a "flag" at the end of pattern:
won't spit an error if it doesn't find
spam
and allow you to continue.See
:help flags
.