在 Vim 中捕获 shell 命令的输出

Capturing output of shell command in Vim

我正在尝试编写一个函数,该函数将根据 Bundler 显示的内容在 gem 的目录中接受 gem 名称和 运行 Ctrl-P。现在我一直在研究如何从 shell 命令捕获输出。

到目前为止我有:

function! GemCtrlP(gem_name)
   execute '!bundle list ' . a:gem_name
endfunction

我想保存 bundle 调用的输出,然后将其通过管道传输到 CtrlP。我可以做 CtrlP 部分,但我不确定如何捕获 shell 输出

有什么想法吗?

使用:redir命令可以实现任意vim命令的抓取。 对于shell命令,通常使用system()函数更容易。

查看帮助

:h :redir
:h system

帮助中应该有足够的示例(或者看一些插件)。

Christian 发布内容的更详尽版本:

function! GemCtrlP(gem_name)
  let path = system('bundle list ' . a:gem_name . ' | tr -d "\n"')
  execute 'CtrlP ' . path
endfunction