This post will cover how to set the default text editor used by git or mercurial via the command line. The default text editor is used whenever the version control system requires us to enter a message, like a commit message.

Although we will set the editor from the command line on Linux and Windows bear in mind that for Linux the best way to set the preferred text editor is using environment variables. Both version control systems will check the $(VISUAL) and $(EDITOR) Linux environment variables (in that order of precedence) if no editor has been set. As they control the default text editor for the OS it makes sense to stick to just using those but we will still cover how to set via the command line as an example.

ViM for Linux

We’ll start simple and set our editor to ViM on Linux. This should be on the path by default but just to make sure we’ll try and install it first. This assumes we’re using Ubuntu or some other Linux flavour that uses apt-get for package management.

For git this is pretty simple.

$ sudo apt-get install vim
$ git config --global core.editor "vim"

The first line ensures we have the editor installed, the second tells git to set the core.editor variable in our per-user git configuration file (~/.gitconfig) to ViM. Instead of --global we could have used --system ($(prefix)/etc/gitconfig) to set the editor variable for all users or --local (.git/config) to set the editor variable for the current repository but we don’t want to force our choice on other people and having a different editor for each repository seems needlessly complex.

For mercurial it is slightly more annoying.

$ sudo apt-get install vim
$ hg config --edit

The first line is the same as with git to ensure the editor is installed, this is not necessary assuming we ran the git commands above. The second line tells mercurial to open the per-user configuration file ($HOME/.hgrc) in a text editor to allow us to modify it by adding an entry editor = vim to the [ui] section as shown below.

# example user config (see "hg help config" for more info)
[ui]
# name and email, e.g.
# username = Jane Doe <jdoe@example.com>
username = 
email = 
editor = vim

[extensions]
# uncomment these lines to enable some popular extensions
# (see "hg help extensions" for more info)
#
# pager =
# color =

Unfortunately there is no way of editing the configuration file directly from the command line.

Similar to git, if for some reason we wanted to we could also use the flag --global (/etc/mercurial/hgrc) to edit the config file for all users or --local (.hg/hgrc) to edit the config file for the current repository.

The --edit, --global and --local flags are only valid from mercurial 3.0 onwards, for earlier versions the config file must be found manually.

Sublime Text for Windows

Setting an editor on Windows is very similar although the configuration files are for both git and mercurial are in slightly different locations.

For git the per-user configuration file is located %USERPROFILE%\.gitconfig and the system wide configuration file is located somewhere like <install-dir>\config. For mercurial the per-user configuration file is located %USERPROFILE%\.hgrc or %USERPROFILE%\Mercurial.ini and the system wide configuration file is located in various locations including <install-dir>\Mercurial.ini or in the registry. For more information on the exact details refer to the git or mercurial documentation.

Now we’ll set the editor to the Sublime Text 3 command line helper subl.exe. Assuming the helper has not been added to %PATH% we can use the %PROGRAMFILES% environment variable to build up the absolute path to the helper. Note that we also need to enclose the path to the helper in quotes to deal with the spaces in the directory names.

We also need some additional command line options to let Sublime Text know that we want to open a new window -n and that we want to wait for the new window to be closed before returning -w.

Now let’s put that all together to set the editor for git.

> git config --global core.editor "\"%PROGRAMFILES%\Sublime Text 3\subl.exe\" -n -w"

The %PROGRAMFILES% environment variable will be expanded before the path is added to the configuration file by git, the editor configuration variable cannot contain environment variables as git will not attempt to expand them when opening the editor.

And then open the per-user configuration file for mercurial.

> hg config --edit

And manually set the editor.

[ui]
editor = "c:\Program Files\Sublime Text 3\subl.exe" -w -n

Like git we can’t use an environment variable inside the configuration file as mercurial will not expand them when opening the editor. If we had installed Sublime Text anywhere other than the default install location or our Program Files directory was not on c:\ we would need to update the path accordingly.

This will only work from a Windows command prompt, any Linux style shell on Windows will have a different absolute path to Sublime Text. To get the editor to also work from a Git for Windows Bash shell or a Cygwin shell we could add subl.exe to %PATH% and remove the absolute path from the configuration entry. Currently it will not work from the newer Bash on Ubuntu on Windows prompt as subl.exe is the wrong binary type.

Summary

Now we’ve got a basic understanding of how version control configuration files work and have our preferred text editor to edit our commit messages.

See the links below for more information on the other ways to set the default source control text editor: