我最近写了一些将“git commit”作为子进程运行的 golang 代码。 Git 依次调用 vim 作为子进程,以便我可以编辑提交消息。但是,我发现以这种方式执行 vim 时,退格键无法按预期工作。提交后,终端输出被破坏,就好像某个组件对每行的列数有很多跟踪。后一种行为已通过重置命令修复。
输出中出现了一个非常有趣的线索:
Vim: Warning: Input is not from a terminal
所以看起来终端信息没有传播到子进程。
我直接用vim或者在命令行直接运行git commit就没有这些问题。您对我如何排查和解决此问题有什么想法吗?
我在 Mac 上使用 vim 8.1:
VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 9 2018 16:08:01)
macOS version
Included patches: 1-235
Compiled by Homebrew
Huge version with MacVim GUI. Features included (+) or not (-)...
我的golang程序没有修改exec.Cmd对象的Cmd.Env变量。当我通过 exec.Command("which", "vim") 在 golang 中运行“which vim”时,我能够确认与命令行相同的位置。但是,我意识到 git commit 不一定会为 vim 找到相同的位置。
我的 .gitconfig 包含:
[core]
editor = vim
但我看不出其他任何东西看起来明显会改变编辑器的行为。
最佳答案
解决方案是显式设置 cmd.Stdin:
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
我已经设置了 stderr 和 stdout,但没有意识到还需要设置 stdin。给出了一些解释为什么会这样的细节 here .
关于git - Vim 在作为子进程调用时忘记了如何使用左箭头键。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52046871/