我需要在 vim 中运行外部命令,并且仅在失败时才查看标准输出。
例如,当我按 F9 时,我想运行do.sh
并仅收到有关错误的通知。现在我使用 double<cr><cr>
关闭默认输出窗口,但这不会显示任何错误:
nmap <F9> :!./script.sh<cr><cr>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
作为奖励,不需要 double,<cr>
因为 system() 返回输出而不显示任何内容。