115

I'm new with git, so I decided to learn git using the tutorials of github. The third chapter said:

"For this first example we’ll modify the README file to add ourselves as an author on the project. So we simply edit the file. Now we want to commit that change, so we run the git commit -a command."

When I use the git commit -a command, the console opens a vim and I wrote my message, but I don't know how to close this vim editor from the console. How do I save the message and close vim?

Jean
  • 4,582
  • 9
  • 44
  • 74
  • 9
    This graphical cheat sheet is very useful: http://www.viemu.com/vi-vim-cheat-sheet.gif. I use it as my desktop wallpaper. – yasouser May 23 '11 at 17:47
  • @Jean - What platform are you on, and are you using console or GUI Vim? Don't you get an introductory text (version number, author and so on) on the start screen? – Rook Feb 14 '12 at 01:45

6 Answers6

198
  1. In vim, you save a file with :wEnter while in the normal mode (you get to the normal mode by pressing Esc).
  2. You close your file with :q while in the normal mode.

You can combine both these actions and do Esc:wqEnter to save the commit and quit vim.

As an alternate to the above, you can also press ZZ while in the normal mode, which will save the file and exit vim. This is also easier for some people as it's the same key pressed twice.

abcd
  • 40,177
  • 7
  • 71
  • 97
  • 25
    You can also do git commit -a -m "your message here" that will not bring up VIM. – David Basarab May 23 '11 at 14:55
  • 8
    @David: Of course. A lot of these questions here are because people forget the `-m` option and then are thrown inside a vim window and don't know how to get out. – abcd May 23 '11 at 14:57
  • 3
    @yoda: You can also get to normal mode by using ctrl+c. Also, it's Shift+(z+z) to save and quit (two capital Z's). – Rocket Hazmat May 23 '11 at 14:58
  • 1
    @Rocket: You're correct. I didn't realize I had entered `z` instead of `Z`. – abcd May 23 '11 at 15:01
  • 5
    @EEva: Yes, but I'd rather not teach that to a newbie, as if they forget the `:`, then they start deleting characters, and then that leads to panic 2. – abcd May 23 '11 at 15:13
  • I guess the title of the question needs be changed to 'Closing vim' – MP0 May 23 '11 at 15:29
62

Instead of trying to learn vim, use a different easier editor (like nano, for example). As much as I like vim, I do not think using it in this case is the solution. It takes dedication and time to master it.

git config core.editor "nano"
Uku Loskit
  • 37,259
  • 9
  • 85
  • 88
  • 56
    That's funny; nano was default on Ubuntu and I couldn't figure out how to save so I used your command to switch to vim. – hyperslug Sep 11 '12 at 02:50
11

See this thread for an explanation: VIM for Windows - What do I type to save and exit from a file?

As I wrote there: to learn Vimming, you could use one of the quick reference cards:

Also note How can I set up an editor to work with Git on Windows? if you're not comfortable in using Vim but want to use another editor for your commit messages.

If your commit message is not too long, you could also type

git commit -a -m "your message here"
Community
  • 1
  • 1
eckes
  • 56,506
  • 25
  • 151
  • 189
  • You can also follow this answer to get out of vim http://stackoverflow.com/questions/6098742/using-git-commit-a-with-vim/6098842#6098842 – David Basarab May 23 '11 at 14:56
  • While this is true, [it is not advisable](http://robots.thoughtbot.com/post/48933156625/5-useful-tips-for-a-better-commit-message). – Jorge Leitao Sep 21 '13 at 07:05
8

The better question is: How do I interrupt the commit when I quit vim?

There are 2 ways:

  1. :cq or :cquit
  2. Delete all lines of the commit message, including comments, and then :wq

Either way will give git an error code, so it will not proceed with the commit. This is particularly useful with git commit --amend.

cdunn2001
  • 15,667
  • 7
  • 51
  • 42
  • Btw, I remember this from Ham radio lingo. At the start of *Contact*, after her father dies, young Ellie (Jena Malone) tells the Universe, "CQ CQ CQ". That means, ["Calling all stations."](https://en.wikipedia.org/wiki/CQ_(call)) – cdunn2001 Jul 11 '16 at 17:23
6

Try ZZ to save and close. Here is a bit more info on using vim with Git

Abizern
  • 129,329
  • 36
  • 198
  • 252
1

To exit hitting :q will let you quit.

If you want to quit without saving you can hit :q!

A google search on "vim cheatsheet" can provide you with a reference you should print out with a collection of quick shortcuts.

http://www.fprintf.net/vimCheatSheet.html

chrisjlee
  • 18,399
  • 22
  • 72
  • 104