Set Git to use Sublime Text for commit messages

Whenever I set up a new computer for development, one of the first things I do is tell terminal not to use Vim when asking me for commit messages.

I find Vim painful and one of the worst things about it is that even the command to close it is unintuitive. So I never want to find myself stuck in it and having to Google it yet again.

Below are the code snippets to past into your terminal. Note that, on Windows at least, you’ll need to make sure that path to the Sublime Text executable is correct. Just open a My Computer window and browse to that location to check or copy it.

If you want to use another text editor, you can do so also, you’ll just need to change everything within the single quotation marks. The other elements within the double quotation marks are still required and are explained below.

On Mac

git config --global core.editor "'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' -w --launch-or-new-window"

On Windows

git config --global core.editor "'c:/program files/sublime text 3/sublime_text.exe' -w --launch-or-new-window"

Now, when you type git commit Sublime text will open with a commit file ready for you. Simple type your message and save it, and then when you close the file or sublime text, terminal will continue.

You can also close without saving and the terminal will recognise that as you choosing to abort the commit.

What this code does…

  • It edits the git global configuration settings to provide it the editor that should be used.
  • It passes in the path to that editor.
  • -w This tells the terminal to wait until the sublime text file has been closed to continue. This is important and the process won’t work without this.
  • –launch-or-new-window This tells sublime text to open a new window if it’s already open, or just open if it’s closed.

    The last one is what I’ve found to be my preference, as if I sometimes close sublime without saving, I don’t want the next commit to open another copy of the same window (sometimes building up lots of windows on top of each other).

    -n does a similar thing, but forces a new window every time—Which sometimes causes the issue described above.

    Not include -n or –launch-or-new-window will mean that if a commit file is already open in sublime from previously it will also you if you want to reload it. This slows you down slight as you can’t just start typing. Note that with the –launch-or-new-window one, this still happens if you closed sublime without saving, but I find it helpful to let me know there’s a duplicate window floating around.

That’s it!

I’d love to know if any of these articles helped you.
Share what you’re building or ask me a question on Threads or somewhere else.

Instagram is a great place to see my final creative coding experiments, while the others are are great place to connect or see progress updates.

If my content has helped you, I’d never say no to donations to help me keep writing.

Here are some other things you might like


Author:

Date:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.