25

I have this strange problem with using vi as the editor for git commit -a.

Normal flow is: I type git commit -a, vi appears, I enter my commit message, and then :wq to save & exit from vi.

This works fine. However, sometimes I make a typo and instead of :wq I type :Wq (because I use the shift for :). Then, when I correct myself and type :wq again, git gives the following error:

error: There was a problem with the editor 'vi'.
Please supply the message using either -m or -F option.

How can I fix this?

3
  • What version of git and vi? I can't repro this with vim 7.3, git version 1.7.11.1. Commented Jul 26, 2012 at 22:00
  • @Christopher : vi 7.3 and git 1.7.9.6 (Apple Git-31.1), the default ones on Mountain Lion afaik
    – houbysoft
    Commented Jul 26, 2012 at 23:07
  • quick solution is to use brew'd vim and git
    – shea
    Commented Aug 12 at 17:56

4 Answers 4

19

vi is existing with a non-zero status, although without additional details about your setup, it's difficult to tell why. If you're using a lot of plugins to vim, you might try moving your .vimrc file to .vimrc.back and seeing if you can replicate. There are a few sources that discuss this problem, including at least one with a potential solution:

git config --global core.editor /usr/bin/vim

I would try removing your .vimrc first, however. It seems to cause more consternation.

2
  • 5
    Thanks, git config --global core.editor /usr/bin/vim fixed it.
    – houbysoft
    Commented Jul 27, 2012 at 13:04
  • 3
    @applOOb's solution is better, since it's not a hack like this one.
    – jefflunt
    Commented Dec 24, 2016 at 18:15
30

As answered here: https://stackoverflow.com/questions/22699614/git-commit-messages-lost-by-vi

The real solution is to

git config --global core.editor vim -f

According to vim documentation - -f option should be used when Vim is executed by a program that will wait for the edit session to finish

3
  • 3
    Finally... this is the right solution. It also fixes the problem of hitting :W and having git reject your commit message when you subsequently save it.
    – Ryanmt
    Commented Nov 6, 2014 at 22:23
  • A solution that's actually a solution and not some lame workaround! I really hope people that read the accepted answer understand the difference between their normal vim invocation and the hack they're using.
    – jefflunt
    Commented Dec 24, 2016 at 18:15
  • 1
    @applOOb might need to use quotes there: git config --global core.editor 'vim -f', otherwise -f won't be included.
    – Stan
    Commented Oct 7, 2022 at 14:55
1

Do you use the golang vim plugins? This line is causing the vi exit value problem for me: filetype plugin indent off

Removing the whole golang plugin block in .vimrc fixed the problem. Installing the golang plugin using fugitive can be as easy as: ln -s $GOROOT/misc/vim ~/.vim/bundle/vim-golang

(I just documented the problem in this issue on the vim-golang github)

0

Something is wrong with Git config I guess.

So the solution is to reset Git config and it worked for me:

git config --global core.editor $(which vim)

$(which vim) is more flexible than /usr/bin/vim (vim executable may not be there for some people)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .