Using git commit -a with vim

GitVim

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

  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.

Solution 2 - Git

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"

Solution 3 - Git

See this thread for an explanation: https://stackoverflow.com/questions/4708645/vim-for-windows-what-do-i-type-to-save-and-exit-from-a-file/4729480#4729480

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

Also note https://stackoverflow.com/questions/10564/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"

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeanView Question on Stackoverflow
Solution 1 - GitabcdView Answer on Stackoverflow
Solution 2 - GitUku LoskitView Answer on Stackoverflow
Solution 3 - GiteckesView Answer on Stackoverflow
Solution 4 - Gitcdunn2001View Answer on Stackoverflow
Solution 5 - GitAbizernView Answer on Stackoverflow
Solution 6 - GitchrisjleeView Answer on Stackoverflow