I recently wrote some golang code that runs "git commit" as a subprocess. Git in turns calls vim as a subprocess so that I can edit the commit message. However, I find that when vim is executed in this manner the backspace key does not work as expected. After the commit, the terminal output is corrupted as if some component has lot track of the number of columns per line. This latter behavior is fixed with the reset command.
A very interesting clue appears in the output:
Vim: Warning: Input is not from a terminal
So it appears that terminal information is not propagated to the subprocess.
I don't have these issues when I use vim directly or when I run git commit directly on the command line. Do you have any ideas on how I can troubleshoot and fix this problem?
I am using vim 8.1 on Mac:
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 (-)...
My golang program does not modify the Cmd.Env variable of the exec.Cmd object. When I run "which vim" in golang via exec.Command("which", "vim") I am able to confirm the same location as on the command line. However, I realize that git commit won't necessarily find the same location for vim.
My .gitconfig contains:
[core]
editor = vim
but I see nothing else that looks obviously like it would alter the behavior of the editor.
The solution was to set cmd.Stdin explictly:
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
I had setup stderr and stdout, but didn't realize it was necessary to also setup stdin. Some details explaining why this is the case are given here.