217

Trying to learn GitHub at the moment and doing this Git essentials tutorial over at nettuts. I'm on the lesson about making commits.

The teacher types git commit and it opens VIM as his editor (I'd also like to know how to make it open up in Sublime Text 2 instead) anyways it opens in VIM and I add in 1 line saying this is my first commit and hit save.

Next it then prompts me to save the output to the desktop, something I did not see in his screencast. Now I'm still in VIM and not sure how to get back to 'normal' terminal :(

I couldn't figure it out so I just exited the terminal and relaunched it, did git commit again and had some warning messages about duplicates! Not sure if I need to (E)edit anyways or (A)abort.

git status

enter image description here

vim

enter image description here

message when I reopen terminal and do git commit again

enter image description here

Leon Gaban
  • 27,845
  • 80
  • 281
  • 473
  • 3
    f you just want to know how to save and exit vim, it's _:wq_ – guido Nov 22 '12 at 07:09
  • possible duplicate of [Using git commit -a with vim](http://stackoverflow.com/questions/6098742/using-git-commit-a-with-vim) – Tim Moore Aug 06 '14 at 05:37
  • 2
    same problem here... as soon as I try to commit, the terminal opens some kind of editor that I can't quit out of. Very annoying! – Kokodoko May 25 '15 at 11:00
  • 3
    @Kokodoko yeah I just commit with `git commit -m 'my message'` now, that's it :) no need for an editor, never needed to use 1, only needed 1 liners. – Leon Gaban May 25 '15 at 17:20
  • 1
    @LeonGaban short commit messages are okay for the first commit but from them on you should try to adopt the technique of having a short subject and then detailing what actually happened in that commit. – liamvictor Jul 01 '15 at 07:17
  • 1
    @LeonGaban you are amazing – baydi Nov 25 '15 at 07:48
  • http://unix.stackexchange.com/questions/93144/exit-vim-more-quickly – chharvey Jan 16 '17 at 16:37

5 Answers5

381

To save your work and exit press Esc and then :wq (w for write and q for quit).

Alternatively, you could both save and exit by pressing Esc and then :x

To set another editor run export EDITOR=myFavoriteEdioron your terminal, where myFavoriteEdior can be vi, gedit, subl(for sublime) etc.

GGJON
  • 327
  • 1
  • 13
Gille
  • 5,303
  • 1
  • 15
  • 16
42

not really the answer to the VIM problem but you could use the command line to also enter the commit message:

git commit -m "This is the first commit"
Philipp Kyeck
  • 16,652
  • 15
  • 72
  • 105
22

You need to return to normal mode and save the commit message with either

<Esc>:wq

or

<Esc>:x

or

<Esc>ZZ

The Esc key returns you from insert mode to normal mode. The :wq, :x or ZZ sequence writes the changes and exits the editor.

lesderid
  • 3,081
  • 7
  • 36
  • 62
Lieven Keersmaekers
  • 53,391
  • 11
  • 100
  • 140
  • 2
    Nope... the esc key does nothing. I am stuck in "Merge branch master of ..." Please enter a commit message to explain why this merge is necessary. – Kokodoko May 25 '15 at 11:02
  • *Did* you enter a commit message? What happens when you press `:wq`? – Lieven Keersmaekers May 26 '15 at 06:24
  • 2
    In my case nothing happens. What is this cryptic commands, is this 2016 or 1996? – oyalhi Jan 08 '16 at 18:39
  • 1
    @Homo-Erectus - I started using vim some 4 years ago and my only regret is I didn't started earlier. I don't consider it time wasted, you should try it. It does have a steep learning curve mind you but it will be well worth it. As for the commands, I don't have much to add. Type , type :, type x and that should be it. – Lieven Keersmaekers Jan 08 '16 at 21:21
  • @oyalhi It is never late to start using vim – Anatoly Yakimchuk Apr 10 '17 at 13:27
16

Simply doing the vim "save and quit" command :wq should do the trick.

In order to have Git open it in another editor, you need to change the Git core.editor setting to a command which runs the editor you want.

git config --global core.editor "command to start sublime text 2"

Jani Hartikainen
  • 40,227
  • 10
  • 60
  • 82
  • 4
    Thanks for the tip on setting the editor to sublime text. I did run into one problem though, I had to set a `--wait` flag: `git config --global core.editor "subl --wait"`. This prevents `Aborting commit due to empty commit message.` – Eric May 30 '14 at 20:13
16

This is in answer to your question...

I'd also like to know how to make it open up in Sublime Text 2 instead

For Windows:

git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe'"

Check that the path for sublime_text.exe is correct and adjust if needed.

For Mac/Linux:

git config --global core.editor "subl -n -w"

If you get an error message such as:

error: There was a problem with the editor 'subl -n -w'.

Create the alias for subl

sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

Again check that the path matches for your machine.

For Sublime Text simply save cmd S and close the window cmd W to return to git.

mhatch
  • 3,757
  • 5
  • 30
  • 56