Categories
commit-message editor emacs git vim

How do I make git use the editor of my choice for editing commit messages?

[*]

3085

How do I globally configure git to use a particular editor (e.g. vim) for commit messages?

3

4190

Setting the default editor for Git

Pick one:

  • Set core.editor in your Git config:

    git config --global core.editor "vim"
    
  • Set the GIT_EDITOR environment variable:

    export GIT_EDITOR=vim
    

Setting the default editor for all programs

Set the standardized VISUAL and EDITOR environment variables*:

export VISUAL=vim
export EDITOR="$VISUAL"

NOTE: Setting both is not necessarily needed, but some programs may not use the more-correct VISUAL. See VISUAL vs. EDITOR.


Fixing compatibility issues

Some editors require a --wait flag, or they will open a blank page. For example:

  • Sublime Text (if correctly set up; or use the full path to the executable in place of subl):

    export VISUAL="subl --wait"
    
  • VS Code (after adding the shell command):

    export VISUAL="code --wait"
    

16

  • 112

    The EDITOR environment variable has the advantage that a number of other programs will respect it as well.

    – Boojum

    Apr 8, 2010 at 0:45

  • 20

    Note that git config --global would write to your personal (per-user) git configuration file. On Unices it is ~/.gitconfig. So this would configure it for all your repositories.

    Apr 8, 2010 at 14:12

  • 57

    you can test you successfully changed it by trying to amend the last commit message. git commit --amend

    – Marco M.

    Aug 27, 2012 at 15:27

  • 13

    If you’re doing option #1 in Windows and have spaces in the path to the editor (say, if it’s under Program Files) then whack single-quotes inside your double-quotes. e.g. “‘C:/Program Files (x86)/Whatever/App.exe'” – obvious to some but it wasn’t to me!

    Oct 31, 2013 at 15:51

  • 5

    @Abramodj -w is not necessary; -w {scriptout} saves all characters you type when editing to replay later. Perhaps you are confusing it with -f, which is necessary when calling the GUI version of Vim. That is, if you use mvim, then the editor you specify should be mvim -f rather than mvim.

    Oct 13, 2014 at 9:00


771

Run:

git config --global core.editor "vim"

From man git-commit:

ENVIRONMENT AND CONFIGURATION VARIABLES

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

6

  • 4

    Btw, the above is true for CVS and SVN, and I guess other version controls.

    – armandino

    Apr 8, 2010 at 0:33


  • 6

    @armandino: Yes, the others might use VISUAL or EDITOR, but they certainly don’t use GIT_EDITOR or core.editor.

    Apr 8, 2010 at 0:35

  • Thanks for clarifying Mark. I meant the EDITOR variable. I believe the GIT_EDITOR (if defined) simply overrides it.

    – armandino

    Apr 8, 2010 at 0:39


  • 8

    Yep – svnbook.red-bean.com/en/1.1/ch07.html#svn-ch-7-sect-1.3.2 So in theory, if I’m using both svn and git, setting $VISUAL or $EDITOR would be the best solution to cover both by default!

    Apr 8, 2010 at 0:43

  • 12

    For the sake of completeness, core.editor means [core] editor = … in the file

    – JRG

    Aug 8, 2011 at 21:53

213

On Ubuntu and also Debian (thanks @MichielB) changing the default editor is also possible by running:

sudo update-alternatives --config editor

Which will prompt the following:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
* 3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number: 

7

  • 8

    On Debian this also works, thanks! It defaults to pico – argh.

    – MichielB

    Mar 29, 2013 at 15:39

  • 3

    Of course it works on Debian; it’s a Debian feature, which, like most things (ooh, controversial!), Ubuntu merely inherits. Debian’s alternatives system is a much easier way to manage defaults for the supported program types. For reference: debian-administration.org/article/91/…

    Jan 22, 2016 at 11:35


  • It works, while GIT_EDITOR and EDITOR not work –ubuntu

    – Ninja

    Sep 13, 2016 at 3:29

  • @haziz: On my system, one of the listed options is Emacs (others include various versions of Vim, plus nano and ed).

    Jan 11, 2017 at 1:19

  • 3

    @haziz update-alternatives will show any editors that have been installed. Koen just doesn’t have Emacs installed.

    – Major

    Aug 9, 2018 at 16:07