2014-09-26 67 views
1

我需要在vim中运行外部命令,并且仅在失败时才看到STDOUT。在外部命令失败时在vim中显示STDOUT

例如,当我打F9时,我想运行do.sh并仅通知有关错误。现在,我使用双<cr><cr>关闭defualt输出窗口,但是这不会给我任何错误:

nmap <F9> :!./script.sh<cr><cr> 
+0

难道你不只是问这个问题,还是一个非常类似的问题https://stackoverflow.com/questions/26041129/display-make-output-in-vim? – Caek 2014-09-26 04:30:49

+0

不,这是一个不同的问题。只是有点相似。从那里解决方案不会在这里工作。 – 2014-09-26 06:00:00

回答

1

这可以方便地与system()功能和v:shell_error变量来实现。将以下内容放入.vimrc文件中。

nnoremap <leader>n :call Foo()<cr> 

function! Foo() 
    let bar = system('script.sh') 
    if v:shell_error 
    echo bar 
    endif 
endfunction 

作为奖励,没有必要为双<cr>因为system()的返回输出而不显示任何内容。

+0

太棒了!这正是我需要的。谢谢! – 2014-09-26 14:52:55

1

使用该做的事情做好的工具:外壳。像这样的包装可能会诀窍:

#!/bin/sh 
# 
# Run the program specified by the args silently and show stdout on failure. 

if ! "[email protected]" >stdout 2>stderr; then 
    cat stdout stderr 
fi 

您可能可以在vim映射中填充它。稍短的版本可能是:

./script.sh > stdout || cat stdout