我可以将字符串作为标准输入传递给 vim 的系统调用吗? [英] Can I pass a string as stdin to a system call from vim?
问题描述
我想从 vim (:help :!
) 运行系统命令,并且我想将字符串变量的内容作为该命令的 stdin 传递.
I want to run a system command from vim (:help :!
), and I want to pass the contents of a string variable as stdin for that command.
我已经知道我可以将当前缓冲区中的行作为标准输入传递(参见 :help :w_c代码>).但我不想修改当前缓冲区,也不想创建新缓冲区.
I already know I can pass lines from the current buffer as stdin (see :help :w_c
). But I don't want to modify the current buffer, and I don't want to make a new buffer.
我知道我可以将字符串写入临时文件,然后将该文件作为输入通过管道传输并删除它.现在,这是我所知道的针对我的应用程序的最佳解决方案.但首先避免创建文件会更干净.
I know I can write the string to a temporary file, then pipe that file as input and delete it. Right now, that's the best solution I know for my application. But it would be cleaner to avoid creating the file in the first place.
可以吗?
推荐答案
所以基本上,你有一个变量,比如一个字符串,你想把这个变量传递给一个外部命令,并在一个变量中获取返回值缓冲区.
so basically, you have a variable, say, a string, and you want to pass this variable to an external command, and get returned value in a variable instead of buffers.
如果上面是正确的,你可以这样做,看下面的例子:
If above is right, you could do in this way, see the following example:
let s="vim is good"
let r=system("sed 's/good/the best/'",s)
echo r
"display vim is the best
echo s
"display vim is good
这篇关于我可以将字符串作为标准输入传递给 vim 的系统调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!