如何转义 Vim 命令中的 % 和 # 字符?
我正在使用 Ack (https://github.com/mileszs/ack.vim) --literal
标志在 Vim 中搜索项目。我注意到,每当我搜索带有 %
或 #
字符的字符串时,搜索结果与我预期的不匹配。我做了一些研究,发现这是因为 Vim 会在命令中扩展这些字符(%
是当前文件,#
是其他内容,不确定是什么)。
考虑到这些符号在代码中经常出现,在执行搜索时这是非常烦人的行为。有没有办法逃脱它们,最好是自动逃脱,以便搜索按预期进行?我当前的映射是:nnoremap
。
示例
假设我在 CSS 文件中的某个位置有一个选择器 #body
,我想找到它。这些是我尝试过的事情(没有成功):
:Ack --literal #body
:Ack --literal \#body
:Ack --literal "#body"
:Ack --literal "\#body"
有什么想法为什么转义在这里不能像往常一样工作,或者这甚至在寻找什么?我还没有发现这些示例与任何内容匹配。
解决方案
我已经通过双重转义字符使其工作。例如, :Ack --literal "\\#body"
将显示 :ack -H --nocolor --nogroup --column --literal "#body"
在结果窗口的状态栏中显示预期结果。报价似乎也是必需的。
I'm using Ack (https://github.com/mileszs/ack.vim) with the --literal
flag to search through projects in Vim. I noticed that whenever I search for a string with the %
or #
characters, the search doesn't match things as I'd expect it to. I did some research and found that it's because Vim will expand these characters in commands (%
is the current file and #
to something else, not sure what).
This is pretty annoying behavior when performing a search, considering these symbols come up pretty often in code. Is there a way to escape them, preferably automatically, so that the search works as expected? My current mapping is: nnoremap <leader>al :Ack --literal<space>
.
Example
Say I have a selector #body
in a CSS file someplace and I want to find it. These are the things I've tried (that haven't worked):
:Ack --literal #body
:Ack --literal \#body
:Ack --literal "#body"
:Ack --literal "\#body"
Any ideas why escaping wouldn't work as usual here, or what this is even searching for? I haven't had these examples match anything.
Solution
I've gotten it to work by double-escaping the characters. For example, :Ack --literal "\\#body"
will show :ack -H --nocolor --nogroup --column --literal "#body"
in the statusline of the result window and bring up the expected results. The quotes seem to be required as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对@sehe的答案有一个补充:当你执行
!...
或system('...')
vim 不会处理...< /code> 本身,但调用 shell,如下所示:
{shell} {shellcmdflag} {shellxquote}...{shellxquote}
。对于 ack 调用,这将类似于["/bin/bash", "-c", "ack -H --nocolor --nogroup --literal #body"]
,因此 bash 将忽略--literal
之后的所有内容,因为#
是注释字符。对于'#body'
不会这样做,因为带引号的字符串内不可能有注释。I have one addition to @sehe's answer: when you do
!...
orsystem('...')
vim does not process...
by itself, but calls shell, like this:{shell} {shellcmdflag} {shellxquote}...{shellxquote}
. For ack call this will be something like["/bin/bash", "-c", "ack -H --nocolor --nogroup --literal #body"]
, so bash will ignore everything after--literal
because#
is a comment character. It won't do so for'#body'
because no comments are possible inside a quoted string.这是 ack.vim 中的一个错误,不知何故,当你执行
:Ack --literal \#body
时,ack 程序甚至没有被调用,但是,我使用了
: Ack --literal '\#body'
(注意额外的引号)确实按预期工作:我还没有真正测试过它......
It is a bug in ack.vim, somehow the ack program isn't even being called when you do
:Ack --literal \#body
However, I used
And it seems that doing
:Ack --literal '\#body'
(mind the extra quotes) does work as expected:I haven't really tested it...
您只需在它们前面加上反斜杠
即可输出当前缓冲区的文件名
打印一个单独的“%”字符
You just prefix them with a backslash
outputs the current buffer's filename
prints a solitary '%' character
显然你必须多次转义,如 an ack.vim 问题:
Apparently you have to escape multiple times as mentioned in an ack.vim issue: