Vim 用符号搜索 ruby

Vim searching ruby with symbol

我正在尝试获取一个字符串,将其复制到寄存器,然后对其进行搜索替换。因此 @user = User.find(params[:id]) 我不断得到 E486 Pattern not found

我很确定我可以找到一种方法来转义适当的字符并使其以这种方式工作。这很好,但在这种情况下,我从寄存器 "a" 加载字符串。 :%s/<c-r>a/foo/gc

有没有办法转义整个字符串来做到这一点?

您可以通过向模式添加 \V 将正则表达式模式更改为非常普通。在这种模式下,唯一的特殊字符是反斜杠。如果你没有它们,你可以正常插入寄存器。 (或手动转义)

:%s/\V<c-r>a/foo/gc

如果你想自动转义它们,你可以将寄存器传递给带有表达式 register

的转义函数
:%s/\V<c-r>=escape(@a, '\')<cr>/foo/gc

相关帮助部分为 :h \V:h c_CTRL-R_=

就我个人而言,我会得到 visual star plugin (there are few out there). There is a nice Vimcast about this: Search for the selected text。然后我会将其与 :s//foo/gc 一起使用来实现您的目标。