如何从shell命令中消除vim命令的歧义 [英] How to disambiguate vim commands from shell commands
问题描述
来自如何轻松逃脱命令行过滤器:
实际上,这是一个XY问题。不过,我必须将原始问题发布到另一个问题中,因为Y问题确实很有趣,我想知道。我将在评论中发布指向X问题的链接。
And actually, this has been a bit of an XY problem. I'll have to post the original problem in a different question, though, because the Y question is really interesting and I would like to know. I'll post a link to the X question in the comments.
这是 X问题。
我曾经以为,通过弄清楚如何逃避%
,我可以解决问题,但事实证明Vim的行为更深入比我想象的要好。显然,Vim用自己的脚本语言对 |
有特殊的含义,这使事情变得复杂:
I had thought that by figuring out how to escape the %
, I would solve the problem, but it turns out Vim's behavior goes deeper than I had thought. And that is that apparently, Vim has some special meaning for |
in its own scripting language, which complicates things:
:nmap L :r !ls | awk '{printf "\"\%s\"\n", $0}'<CR>
预期结果:
按 L
将使用 ls
的输出填充缓冲区,并用引号引起来。
Pressing L
fills the buffer with the output of ls
, with each line quoted.
实际结果:
E492: Not an editor command: awk '{printf "\"\%s\"\n", $0}')<CR>
Vim似乎认为 awk
,我意思是vim命令,而不是shell命令。我该如何消除歧义?我尝试了许多组合,包括用子外壳包围过滤器,并在AWK前面也放置!
,但是...
Vim seems to think by awk
, I mean a vim command, rather than a shell command. How do I disambiguate this? I have tried many combinations, including surrounding the filter with a subshell, and putting a !
in front of awk as well, but...
子shell:
:nmap L :r !(ls | awk '{printf "\"\%s\"\n", $0}')<CR>
E492: Not an editor command: awk '{printf "\"\%s\"\n", $0}')<CR>
Subshell + !awk
:
Subshell + !awk
:
:nmap L :r !(ls | !awk '{printf "\"\%s\"\n", $0}')<CR>
[No write since last change]
/bin/bash: -c: line 0: syntax error near unexpected token `)'
/bin/bash: -c: line 0: `awk '{printf "\"%s\"\n", $0}')<CR>'
shell returned 1
Press ENTER or type command to continue
L
:r !(ls
!awk
不带子shell:
!awk
without subshell:
:nmap L :r !ls | !awk '{printf "\"\%s\"\n", $0}'<CR>
[No write since last change]
/bin/bash: -c: line 0: syntax error near unexpected token `newline'
/bin/bash: -c: line 0: `awk '{printf "\"%s\"\n", $0}'<CR>'
shell returned 1
Press ENTER or type command to continue
L
:r !ls
我无语。
推荐答案
Ryan 很有帮助地指出,我没有尝试逃避管道的攻击,这完全让我无视:
Ryan helpfully pointed out that I didn't try escaping the pipe. Something that had entirely slipped my mind:
:nmap L :r !ls \| awk '{printf "\"\%s\"\n", $0}'<CR>
^
它起作用了。
现在,如果我们能解决 Y问题,我会成为一个快乐的露营者。
Now if we could just solve that Y problem, I would be a happy camper.
这篇关于如何从shell命令中消除vim命令的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!