21

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?

daviesgeek
  • 37,296
  • 51
  • 158
  • 200
houbysoft
  • 8,972
  • 15
  • 60
  • 93

4 Answers4

27

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

applOOb
  • 371
  • 3
  • 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 Nov 06 '14 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 Dec 24 '16 at 18:15
  • @applOOb might need to use quotes there: `git config --global core.editor 'vim -f'`, otherwise `-f` won't be included. – Stan Oct 07 '22 at 14:55
18

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.

Christopher
  • 366
  • 2
  • 4
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)

Jan
  • 151
  • 1
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)

ericn
  • 193
  • 1
  • 1
  • 7