Getting the linux error code from make in Vim

2019-08-12 05:28发布

问题:

I'm trying to get the "0 of success, nonzero if error" return code from make in Vim. Specifically, I am on Ubuntu and using v:shell_error does not work.

After digging around and looking at this question, it seems to be because of my shellpipe setting, which is

shellpipe=2>&1| tee

The tee pipes the make output back into vim. The shell is apparently returning the error code from tee to vim and not from make. How do I get make's error code instead?

回答1:

You can try to make a custom function for that. E.g. using :call system("make > make.out") run make redirecting output into a file. After that load the error file using :cf make.out. Never tried that myself, though.

In the end, results of make might be also simply checked by testing whether the result is there, in the file system:

:make | if !filereadable("whatever-make-was-supposed-to-create") | throw "Make failed!!!" | endif

(Here the '|' symbol is vim's command separator.) Assigning that to a keyboard shortcut would remove the need for typing.

P.S. I usually try to make my programs to produce no warnings, so I never really came across the issue. What BTW leads to another possible solution: simply remove warnings (or simply undesired output lines) using e.g. grep -v tabooword from the make output by overriding the 'makeprg'. What is actually described in the help: :h 'makeprg'.

P.P.S. I got started on the VIM... Provided that you also use bash as a shell. Did you tried to add to the exit ${PIPESTATUS[0]} to the shellpipe? E.g.:

:set shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

Just tested that on Debian and it worked for me. :h 'shellpipe' for more.



回答2:

The only thing I can currently think of is creating two wrapper scripts for make and tee. I'm sure there's an easier way, but for now you might try this:

Create a make wrapper script:

#!/bin/bash

make $@
echo $? > ~/exit_code_cache

Create a tee wrapper script:

#!/bin/bash

tee $@
return `cat ~/exit_code_cache` # (or do something else with the exit code)

Use the new make :set makeprg=mymake and setup your own shellpipe that uses your tee wrapper (shellpipe=2>&1 | mytee).

It's not tested, but the idea should be clear. Hope it helps.