Is there anything wrong with opening up a new vim tab (for scratch work) while authoring a git commit?
Update: This Seems to Be a Quirk With Vi vs. Vim
On further inspection prompted by @muru's comment, it seems there are a couple of subtleties here.
- The
EDITOR
environment variable was actually unset, so it defaulted tovi
instead ofvim
as originally posted. Edited. In order for vi to exit with error, it is actually important to attempt to quit first with
:q
before using:q!
, and this behavior can be reproduced without using tabs:Launch vi:
vi; echo $?
Type something in insert mode
- Attempt to quit with
:q
and get the errorE37: No write since last change (add ! to override)
- Actually quit with
:q!
- Observe that the exit code is
1
Oddly enough, modifying the above steps by (1) using vim instead of vi, or (2) using vi but quitting with
:q!
directly without using:q
first, yields a0
exit code instead. Bothvi
andvim
have the same exact:version
string on my system, so this is probably a difference in how vim behaves when started asvi
on the command-line.
Original Steps with Git + Vi
Steps to reproduce:
Start making a new commit:
$ export EDITOR=vi # goes without saying :) $ git commit
Open up a new tab with
:tabe
and type something. Anything.- Switch back to the commit tab with
gt
. - Type some message, and close the commit tab with
:x
(writing changes). - Now, attempt to quit the tab with
:q
. - Now, quit the scratch tab with
:q!
(discarding changes). Recoil as vi tells you your meticulously-crafted commit message has also been discarded: 😱
error: There was a problem with the editor 'vi'. Please supply the message using either -m or -F option.
What is Git not happy with in this seemingly innocuous series of steps? 🤔
(This gets confusing because I'm testing Vim's behavior, not Vi's)
Vim's exit code indicates success when repeating the above instructions, so it's not clear why Git thinks Vim quit in error:
$ vim a-file; echo "exit code: $?"
exit code: 0
^ This example is identical to how Git launches Vim, given its process entry in that state:
/usr/bin/vi /path/to/git/repo/.git/COMMIT_EDITMSG
Also, swapping the tab exit order by closing the scratch tab first, then the commit tab, has exactly the same result.
Coming back to the million-dollar question—why is this happening? Answers in the form of snarky references to the Vim/Git docs are most welcome 😄
System: macOS High Sierra 10.13.4
vim :version: VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 29 2017 18:37:46)
git --version: git version 2.15.1 (Apple Git-101)
:version
actually shows 8.0.1283? – Mass Jun 2 '18 at 21:57:version
. It is the same when vim is invoked from git, and directly. – mxxk Jun 3 '18 at 0:17vi
whenvim
is the editor? Do you have other configuration overriding theEDITOR
variable? Maybe$VISUAL
, or some git config settings? – muru Jun 4 '18 at 8:02:q
before:q!
– mxxk Jun 4 '18 at 17:08