在GVim中运行shell命令而不回显Vim命令

时间:2013-06-29 18:17:07

标签: shell vim

当我使用:!运行shell命令时,例如:

!echo hi

它会打印VimScript命令及其输出,所以我得到:

:!echo hi
hi

当我在命令行模式下执行此操作时,这是正常的,但是当我通过.vim文件运行它时,我不想看到它 - 我只是想查看命令的结果。

有没有办法禁用VimScript命令的回显?

我知道我可以使用

echo system('echo hi')

但这会妨碍我将它与交互式shell程序一起使用......

顺便说一下,我正在使用Linux - 在Windows中这不是一个问题,因为shell命令无论如何都在新的控制台窗口上运行......

修改

这是我的小工作示例:

function! RunShellTask(cmd)
    execute '!'.a:cmd
    return v:shell_error
endfunction

call RunShellTask('echo hi')

我用:source %

运行它

1 个答案:

答案 0 :(得分:0)

您可以尝试:redir命令:

                     *:redi* *:redir*
:redi[r][!] > {file} Redirect messages to file {file}.  The messages which
                     are the output of commands are written to that file,
                     until redirection ends.  The messages are also still
                     shown on the screen.  When [!] is included, an
                              :
                              :
                     To stop the messages and commands from being echoed to
                     the screen, put the commands in a function and call it
                     with ":silent call Function()".
                     An alternative is to use the 'verbosefile' option,
                     this can be used in combination with ":redir".

我还没有测试过,但你可以尝试:redir @b,从一个用:silent call调用的函数执行shell命令,读取输出(从寄存器b),过滤掉vimscript命令,显示它在屏幕上然后:redir end

另一种选择是尝试一些提供类似功能的插件: