73

I want to add a commit message using vim (or nano, or any other text editor) rather than writing it in the command line using git commit -m.

How I can do this?

4
  • 2
    If you don't specify -m <message> on the command line, git will open an editor for your commit message. See the git-var man page for some information on how to configure which editor will be used. Commented Jan 11, 2018 at 13:45
  • 1
    git commit without the -m will open your default editor for the commit message Commented Jan 11, 2018 at 13:45
  • I created a new test file. Did commit without the -m parameter and vim did appear. After I insert the text in vim, how do I save it to be the commit message? Commented Jan 11, 2018 at 13:50
  • 1
    Just save and exit with :wq Commented Jan 11, 2018 at 16:43

5 Answers 5

125

This is mostly a question about using VIM, so let's do it.

First, you need to setup Git to use Vim as the editor, if that's not the default for you. You can do that by adding to the git config of your choice (none which is local, --global or --system):

git config --global core.editor vim

Then when you commit, you don't add the -m parameter, leave it blank:

git commit
// or
git commit -a

After that, you are in VIM, in escape mode. You then need to start insert mode to write. The simplest way is to type i, and a message will appear on the bottom (-- INSERT --). You are in insert mode and you can now type in your message.

After that, you must exit insert mode, and you do that by pressing Esc once. The -- INSERT -- message on the bottom should vanish. You are now in escape mode again, and you must save and quit.

That is done by using the : key to enter command mode and typing the command wq or x, leaving you with either :wq or :x typed at the bottom.

w stands for write and q for quit, so wq is write and quit. x is an alias for wq.

After that you just press Enter and you're done, out of VIM.

If you have any doubts post a comment and I'll add it up.

Sign up to request clarification or add additional context in comments.

3 Comments

I followed all the steps here. After I did :x, I got back to normal terminal window. How can I check that the file was committed with the message I typed in vim editor?
I am really sorry for not checking the comments section and taking so long. I bet you already know this by now, but for others who don't: just check the log with git log. This opens the git log for all commits using less. You can navigate using up and down arrows or spacebar, and quit by pressing the letter q.
I changed "vim" to "nvim" and it worked like a charm too. Thank you!
16

When using

git commit

a vim console will open automatically, when using the 'normal' settings.

In the vim-console, you will have to press i first, than you will be able to enter your text at the position of the cursor.

Next, you will have to press ESC to leave the input menu. To save your changes, type :wq (write & quit, the colon is to enter the command-mode), then hit ENTER.

Please have a look at the help files.

3 Comments

This isnt my question though. I can insert text, I need to know how to save it to be the commit message.
Sorry, added this in the answer. Use :wq and ENTER.
:wq+ENTER gives E32: No file name.
6

If you don't pass in the -m parameter into git commit then by default Git will open vim to allow the user to write the commit message.

To change the editor to something different run the following command (eg. for vim);

git config --global core.editor "vim"

1 Comment

OP specifically requests Vim. Answer intentionally substitutes Vim for Emacs. Editor war intensifies.
0

If you don't use the -m flag, then the editor will be opened. By default, it's vim but you can change it:

git config --global core.editor nano

Comments

-1

just add the commit message on first

use X to exit (capital X) it automatically save the commit message

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.