问题
From How to easily escape a command-line filter:
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.
This is the "X" question.
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>
Expected result:
Pressing L
fills the buffer with the output of ls
, with each line quoted.
Actual result:
E492: Not an editor command: awk '{printf "\"\%s\"\n", $0}')<CR>
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...
Subshell:
:nmap L :r !(ls | awk '{printf "\"\%s\"\n", $0}')<CR>
E492: Not an editor command: awk '{printf "\"\%s\"\n", $0}')<CR>
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
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
I'm speechless.
回答1:
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>
^
It worked.
Now if we could just solve that Y problem, I would be a happy camper.
来源:https://stackoverflow.com/questions/49619411/how-to-disambiguate-vim-commands-from-shell-commands