简体   繁体   English

用换行符将参数传递给vim中的外部命令

[英]Pass argument with newline to external commands in vim

When I'm interacting with the shell or writing a bash script I can do: 当我与外壳进行交互或编写bash脚本时,我可以执行以下操作:

somecmd "some
arg"

Say now that I want to do the same in vim command-line mode: 现在说我要在vim命令行模式下做同样的事情:

:!somecmd "some<Enter>arg"

obviously won't work: as soon as I press <Enter> the command is executed. 显然是行不通的:只要按<Enter> ,命令就会执行。 But neither the following do: 但是以下两种方法均不能:

:!somecmd "some<C-V><Enter>arg"
:!somecmd "some<C-V>x0Aarg"

The first one inserts a carriage return instead of a line feed, which is right. 第一个插入回车符而不是换行符,这是正确的。 The second one will break the command in two, trying to execute somecmd "some<CV> first and then arg" , both of which fail miserably. 第二个命令会将命令分成两部分,尝试先执行somecmd "some<CV>然后执行arg" ,这两个命令都会失败。

I guess I could work around this using some echo -e command substitution, or embedding $'\\n' , but is it possible to type it directly in vim's command-line? 我想我可以使用echo -e命令替换或嵌入$'\\n' ,但是是否可以直接在vim的命令行中键入它? I don't fully understand why the "some<CV>x0Aarg" form doesn't work while $'some\\narg' does. 我不完全理解为什么$'some\\narg'不能使用"some<CV>x0Aarg"形式。 Is vim parsing the string previously to shell evaluation? vim是否先前已解析字符串以进行shell评估?

Well, I've found the answer myself, but I'm leaving here for further reference anyway. 好吧,我自己找到了答案,但无论如何我还是要留在这里作进一步的参考。 The documentation of :! :!的文档 states: 状态:

A newline character ends {cmd}, what follows is interpreted as a following ":" command. 换行符以{cmd}结尾,以下内容将解释为以下“:”命令。 However, if there is a backslash before the newline it is removed and {cmd} continues. 但是,如果在换行符之前有一个反斜杠,则将其删除并继续{cmd}。 It doesn't matter how many backslashes are before the newline, only one is removed. 换行符前有多少个反斜杠都没关系,只删除了一个。

So you (I) should type "some\\<CV>x0Aarg" instead of "some<CV>x0Aarg" . 因此,您(I)应该键入"some\\<CV>x0Aarg"而不是"some<CV>x0Aarg"

Plus, I could have done it using the system() function instead of the :! 另外,我可以使用system()函数代替:!来完成它:! command: 命令:

:call system("somecmd 'some<C-V>x0Aarg'")  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM