如何转义 Vim 命令中的 % 和 # 字符?

发布于 2024-11-01 05:19:56 字数 930 浏览 5 评论 0原文

我正在使用 Ack (https://github.com/mileszs/ack.vim) --literal 标志在 Vim 中搜索项目。我注意到,每当我搜索带有 %# 字符的字符串时,搜索结果与我预期的不匹配。我做了一些研究,发现这是因为 Vim 会在命令中扩展这些字符(% 是当前文件,# 是其他内容,不确定是什么)。

考虑到这些符号在代码中经常出现,在执行搜索时这是非常烦人的行为。有没有办法逃脱它们,最好是自动逃脱,以便搜索按预期进行?我当前的映射是:nnoremapal :Ack --literal

示例

假设我在 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 技术交流群。

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

发布评论

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

评论(4

弃爱 2024-11-08 05:19:57

我对@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 !... or system('...') 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 程序甚至没有被调用,

但是,我使用了

`strace -f -o-e trace=process gvim | tee /tmp/log`

: Ack --literal '\#body' (注意额外的引号)确实按预期工作:

[pid  3833] execve("/usr/bin/ack", ["ack", "-H", "--nocolor", "--nogroup", "--literal", "#body"], [/* 25 vars */] <unfinished ...>

我还没有真正测试过它......

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

`strace -f -o-e trace=process gvim | tee /tmp/log`

And it seems that doing :Ack --literal '\#body' (mind the extra quotes) does work as expected:

[pid  3833] execve("/usr/bin/ack", ["ack", "-H", "--nocolor", "--nogroup", "--literal", "#body"], [/* 25 vars */] <unfinished ...>

I haven't really tested it...

提笔落墨 2024-11-08 05:19:56

您只需在它们前面加上反斜杠

:!echo %

即可输出当前缓冲区的文件名

:!echo \%

打印一个单独的“%”字符

You just prefix them with a backslash

:!echo %

outputs the current buffer's filename

:!echo \%

prints a solitary '%' character

嘿哥们儿 2024-11-08 05:19:56

显然你必须多次转义,如 an ack.vim 问题:

:Ack \\\#foo

Apparently you have to escape multiple times as mentioned in an ack.vim issue:

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