vim - 将两个 $ 符号之间的所有内容替换为\( 和\) 之间的文本

标签 vim

我希望将 $ 符号之间的文本替换为放置在“\(”和“\)”之间的文本,如

replace $this text$

replace \(this text\)

我已经尝试过命令“%s/\$\(.*\)\$/\(\1\)/”,结果是

replace $this text$ and $this also$.

被替换为

replace \(this text$ and $this also\).

如何避免这种情况?

最佳答案

如果我理解你的问题,我想你想要:

:%s/\$\([^\$]*\)\$/\\(\1\\)/g

说明:

  • 您需要转义替换字符串中的 \
  • 搜索字符串中需要使用 [^\$]* 而不是 .*,即由 0 个或多个“不是美元符号”的字符组成的序列.

将替换此文本:

replace $this text$
replace $this text$ and $this also$.

使用此文本:

replace \(this text\)
replace \(this text\) and \(this also\).

关于vim - 将两个 $ 符号之间的所有内容替换为\( 和\) 之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55001215/

相关文章:

Powershell ISE + vim

replace - 用 Vim 文件中其他地方的寄存器或行范围的内容替换

vim - 更改光标的保持时间?

用户输入的 Vim 映射

linux - 通过 Vim 保存平面文件向创建新行的文件添加一个不可见的字节

vim - 指定文件类型时覆盖 MacVim 的默认 filetype.vim

java - eclim .classpath 命令无法识别

eclipse - 如何在 gradle 项目中使用 eclim

vim - 是否有用于语义/语法突出显示的 vim 插件?

vim - 语法高亮在 Janus for Vim 中不起作用