2014-09-26 65 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