在 Vim 中从 make 获取 linux 错误代码

发布于 2024-09-06 18:25:14 字数 414 浏览 9 评论 0原文

我试图从 Vim 中的 make 获取“0 成功,如果错误则非零”返回代码。具体来说,我在 Ubuntu 上并且使用 v:shell_error 不起作用。

深入挖掘并查看 这个问题,似乎是因为我的shellpipe设置,即

shellpipe=2>&1| tee

tee 将 make 输出传送回 vim。 shell 显然将错误代码从 tee 返回到 vim,而不是从 make 返回。我如何获取 make 的错误代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-09-13 18:25:14

您可以尝试为此创建一个自定义函数。例如,使用 :call system("make > make.out") run make 将输出重定向到文件中。之后使用 :cf make.out 加载错误文件。不过我自己从来没有尝试过。

最后,也可以通过测试结果是否存在于文件系统中来简单地检查 make 的结果:(

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

这里的 '|' 符号是 vim 的命令分隔符。)将其分配给键盘快捷方式将消除打字的需要。

PS 我通常尝试让我的程序不产生警告,所以我从来没有真正遇到过这个问题。 BTW 导致了另一种可能的解决方案:通过覆盖 'makeprg',使用例如 grep -v tabooword 从 make 输出中简单地删除警告(或只是不需要的输出行)。帮助中实际描述的内容::h 'makeprg'

PPS 我开始使用 VIM... 前提是您还使用 bash 作为 shell。您是否尝试将出口 ${PIPESTATUS[0]} 添加到 shellpipe 中?例如:

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

刚刚在 Debian 上进行了测试,它对我有用。 :h 'shellpipe' 了解更多。

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.

伴随着你 2024-09-13 18:25:14

我目前唯一能想到的是为 make 和 tee 创建两个包装脚本。我确信有一种更简单的方法,但现在您可以尝试以下操作:

创建一个 make 包装器脚本:

#!/bin/bash

make $@
echo $? > ~/exit_code_cache

创建一个 tee 包装器脚本:

#!/bin/bash

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

使用新的 make :set makeprg=mymake 并设置您自己的 < code>shellpipe 使用您的 tee 包装器 (shellpipe=2>&1 | mytee)。

它没有经过测试,但想法应该很清楚。希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文