Vim:忽略映射替换列表中的错误

发布于 2024-12-27 09:07:28 字数 447 浏览 4 评论 0原文

我经常进行许多正则表达式替换,因此希望映射一个键来轻松完成此操作。但是,如果在文件中找不到要替换的正则表达式之一,则我的键映射会由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2025-01-03 09:07:28

/e 标志添加到每个替换命令中。它不会显示错误消息并继续,就像没有发生错误一样。这将是新的映射:

map <F12> :%s/spam/foo/e<enter>:%s/ham/bar/e<enter>:%s/eggs/baz/e<enter>

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:

map <F12> :%s/spam/foo/e<enter>:%s/ham/bar/e<enter>:%s/eggs/baz/e<enter>
风吹雨成花 2025-01-03 09:07:28

您可以在模式末尾添加一个“标志”:

:%s/spam/foo/e

如果没有找到垃圾邮件,则不会发出错误并允许您继续。

请参阅:帮助标志

You can add a "flag" at the end of pattern:

:%s/spam/foo/e

won't spit an error if it doesn't find spam and allow you to continue.

See :help flags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文