如何找到&在 Vim 中用一个表达式替换多个表达式?

发布于 2024-12-16 14:27:56 字数 244 浏览 3 评论 0原文

我正在将一个示例从 PDF 复制粘贴到 Vim,我必须将所有 替换为 " 以及所有 ''' 以便代码正常工作。

嗯,这可能看起来更容易理解: 我想同时用 foobar 替换所有 foobar

I am copy-pasting an example from a PDF to Vim and I have to replace all and with "
and all and with ' so that the code works.

Well that will probably seem easier to understand:
I want to replace all foo and bar with foobar simultaneously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

那小子欠揍 2024-12-23 14:27:56

vi 中尝试一下:

:1,$s/[“”]/"/g

然后

:1,$s/[‘’]/'/g

Try this in vi:

:1,$s/[“”]/"/g

then

:1,$s/[‘’]/'/g
俏︾媚 2024-12-23 14:27:56

使用 tr 作为过滤器:

Unix 方式:

:%!tr “”‘’ \"\"\'\'

Use tr as a filter:

Unix way:

:%!tr “”‘’ \"\"\'\'
我做我的改变 2024-12-23 14:27:56

如果您想用“foobar”替换所有“foo”和所有“bar”,您可以使用:

%s/\v<(foo|bar)>/foobar/g

这将替换“foo”和“bar”,但将保留任何“foobar”。

  • %s/ - 替换整个文件
  • \v - 使用非常神奇的正则表达式语法(请参阅 :help magic 了解更多信息)
  • < - 匹配左侧单词边界
  • (foo|bar) - foo 或 bar
  • > - 匹配右侧单词边界
  • /foobar/< /code> - 替换字符串
  • g - 全局(将会发生对于每一次出现,而不仅仅是该行中的第一个)

请注意,如果您只是处理标点符号,您可能需要删除此正则表达式的单词边界部分,否则它将不起作用。

If you want to replace all "foo"s and all "bar"s with "foobar" you can use this:

%s/\v<(foo|bar)>/foobar/g

This will replace the "foo"s and the "bar"s but will leave any "foobar"s alone.

  • %s/ - substitute across the whole file
  • \v - use very magic regex syntax (see :help magic for more info)
  • < - match a left word boundary
  • (foo|bar) - foo or bar
  • > - match a right word boundary
  • /foobar/ - replacement string
  • g - globally (will happen for every occurrence, not just the first on the line)

Note that if you are just dealing with punctuation you'll probably want to remove the word boundary parts of this regex or it won't work.

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