git commit 出现There was a problem with the editor ‘vi’

今天在Mac OS X 10.9.3上使用git的时候遇到这么一个问题,执行git commit操作会显示如下错误:
There was a problem with the editor ‘vi’

$ git config –global core.editor /usr/bin/vim

I have had an annoying problem with git and vi. I like to use vim to edit my commit messages, but I’ve been hit with this annoying message every time I write the message and quit vim.

error: There was a problem with the editor ‘vi’
After a little bit of digging I found that this message is shown by git when the editor exits with a non-zero exit code. You can use $? to see the exit code of last script or application.

$ vim # then exit vim with :q immediately
$ echo $?
I’m still not sure why vim is exiting with non-zero exit code, but it is definitely related to my .vimrc – moving it to .vimrc.bak seemed to fix the problem. I’m using the excellent pathogen plugin to manage my vimfiles, so I plan to go through that and my installed plugins to find the cause of the problem.

There is a fix though, I’m not sure what’s causing this, but I found a post on the vim-mac mailing list which shows this:

$ vim # and exit with :q
$ echo $?
$ /usr/bin/vim # and exit with :q
$ echo $?
$ which vim
Running vim with /usr/bin/vim seems to make it exit cleanly. So to fix the problem with git commit you just need to run this:

$ git config –global core.editor /usr/bin/vim
I’d still like to get to the root of the problem, but this gets me my git commit messages back!

CC BY-NC-SA 4.0 git commit 出现There was a problem with the editor ‘vi’ by 桔子小窝 is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.