How do I make git use the editor of my choice for commits?

1,175,732

Solution 1

If you want to set the editor only for Git, do either (you don’t need both):

  • Set core.editor in your Git config: git config --global core.editor "vim"

OR

  • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

If you want to set the editor for Git and also other 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.


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"

Solution 2

Copy paste this:

git config --global core.editor "vim"

In case you'd like to know what you're doing. 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).

Solution 3

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: 

Solution 4

In windows 7, while adding the "Sublime" editor it was still giving me an error:

Aborting commit due to empty commit message.

Sublime was not able to keep the focus.

To fix this I opened the .gitconfig file in c:/users/username/ folder and added the following line with --wait option outside the single quotes.

[core]
      editor = 'F:/Program Files/Sublime Text 2/sublime_text.exe' --wait

Hope its helpful to somebody facing similar issue with Sublime.

Solution 5

In Windows 7, setting editor to Notepad++

  • Open any text editor.
  • Open this file: C:\Users\YOUR_USERNAME\.gitconfig
  • Add this section to the bottom:

For 64 bit Notepad++ use:

[core]
    editor = 'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar

For 32 bit Notepad++ use:

[core]
    editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar
  • Save and close the file.
  • When you're committing with git, just write git commit and press Enter. It will pop open Notepad++.
  • Write your commit message at the top of the file, and save and close the file. Done!
Share:
1,175,732
brasskazoo
Author by

brasskazoo

I am a Software and Cloud Solutions Engineer with a passion for building quality and automation into the development, testing and deployment lifecycles. Currently working with a large AWS hybrid-cloud integration project, including VPC architecting and solution design, serverless applications and shifting applications to EC2. Primarily I work with terraform, node.js, react and java, with side projects currently in react-native and GraphQL for cross-platform mobile applications. Agile, DevOps culture, Code Quality and Continuous Integration are cornerstones of my development efforts.

Updated on July 16, 2022

Comments

  • brasskazoo
    brasskazoo almost 2 years

    I would prefer to write my commit messages in Vim, but git is opening them in Emacs.

    How do I configure git to always use Vim? Note that I want to do this globally, not just for a single project.

  • armandino
    armandino about 14 years
    Btw, the above is true for CVS and SVN, and I guess other version controls.
  • Ralph Sinsuat
    Ralph Sinsuat about 14 years
    @armandino: Yes, the others might use VISUAL or EDITOR, but they certainly don't use GIT_EDITOR or core.editor.
  • armandino
    armandino about 14 years
    Thanks for clarifying Mark. I meant the EDITOR variable. I believe the GIT_EDITOR (if defined) simply overrides it.
  • brasskazoo
    brasskazoo about 14 years
    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!
  • Boojum
    Boojum about 14 years
    The EDITOR environment variable has the advantage that a number of other programs will respect it as well.
  • Anonigan
    Anonigan about 14 years
    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.
  • JRG
    JRG almost 13 years
    For the sake of completeness, core.editor means [core] editor = ... in the file
  • Marco M.
    Marco M. almost 12 years
    you can test you successfully changed it by trying to amend the last commit message. git commit --amend
  • MichielB
    MichielB about 11 years
    On Debian this also works, thanks! It defaults to pico - argh.
  • icl7126
    icl7126 about 11 years
    finally someone who knows how to write it simple! Thanks. But you should mentioned, that in the path to notepad have to be used '/' or double backslash '\\', otherwise git will complain...
  • ErikE
    ErikE almost 11 years
    You may need to add at least -multiInst as a parameter to notepad++ and possibly -notabbar. Do this if git doesn't seem to know when you've finished editing the file and either waits forever or not at all.
  • ddavison
    ddavison over 10 years
    Nice! Thanks Anmol, I was having that issue where it was committing on an empty message.
  • Pablissimo
    Pablissimo over 10 years
    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!
  • Matej
    Matej over 10 years
    @givanse what do you mean?
  • Rup
    Rup over 10 years
    I guess he means it's trivial by analogy to the vim answer ?
  • Matej
    Matej over 10 years
    hmm no it isn't, but if you use something else than vim (such as textmate).
  • Abramodj
    Abramodj about 10 years
    You should change your answer to git config --global core.editor "vim -w" to actually allow the user to write a commit message.
  • Adam F
    Adam F about 10 years
    Why would you want to use sublime for git commits?
  • Anmol Saraf
    Anmol Saraf about 10 years
    Any editor for git commit will mostly be used to add multiple lines of comments and Sublime is a programmer choice for various reason for many developers. People generally have a tendency to use one editor for most of their coding and other works. Sublime is just a personal choice, it can be any editor.
  • Rory O'Kane
    Rory O'Kane over 9 years
    @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.
  • Josef
    Josef about 9 years
    to set the config on the commandline, I need double quotes inside single quotes like >git config --global core.editor '"C:/Program Files (x86)/Notepad++/notepad++.exe"'
  • trungly
    trungly almost 9 years
    textmate also has a "wait" option, so for me, it's this: git config --global core.editor "/usr/local/bin/mate -w"
  • Nick Volynkin
    Nick Volynkin almost 9 years
    From Pro Git: "By default, Git uses whatever you’ve set as your default text editor ($VISUAL or $EDITOR) or else falls back to the vi editor to create and edit your commit and tag messages. " Isn't this order a reversal to what you're citing from man git-commit?
  • Mahendran
    Mahendran almost 9 years
    Somehow it's not delivering the text to git. I got 'Aborting commit due to empty commit message.' error.
  • Mahendran
    Mahendran almost 9 years
    I have used the first command to setup SL. cmd + S then cmd +W to close editor
  • 7stud
    7stud over 8 years
    @RoryO'Kane, Thanks for the -f tip for mvim(i.e. macvim)! Both -w and -W didn't work correctly for me. With -w I got Aborting commit due to empty commit message., and -W opened a vim welcome window.
  • underscore_d
    underscore_d over 8 years
    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/…
  • Manza
    Manza about 8 years
    still opens the normal notepad for me
  • kukinsula
    kukinsula about 8 years
    Here's how to set emacs in terminal mode when comiting git config --global core.editor "emacs -nw"
  • mwfearnley
    mwfearnley about 8 years
    Instead of going to File->Save as, you could blank the file (or comment out any non-blank lines), and then Git will abort due to an empty commit message.
  • Shinigami
    Shinigami about 8 years
    --remote-wait-silent to avoid ugly error message :)
  • Technext
    Technext almost 8 years
    @ErikE: Thanks man! Both -multiInst and -notabbar were required. This was bugging for the last few hours.
  • Ninja
    Ninja almost 8 years
    It works, while GIT_EDITOR and EDITOR not work --ubuntu
  • Klaas
    Klaas over 7 years
    If TextWrangler is already open your solution will open another instance with the same file set of the first one. This seems dangerous.
  • Klaas
    Klaas over 7 years
    I did install the TextWrangler command line tools (barebones.com/support/textwrangler/cmd-line-tools.html) and then used git config --global core.editor "edit -w". This will open the commit message in the current instance and as soon as you close only this commit message document, the commit will continue.
  • SourceSeeker
    SourceSeeker over 7 years
    @haziz: On my system, one of the listed options is Emacs (others include various versions of Vim, plus nano and ed).
  • Mikko Rantalainen
    Mikko Rantalainen over 7 years
    If you have both core.editor and GIT_EDITOR set, GIT_EDITOR will be used. This is nice for setting default via git config and overriding the config for individual commands if needed. For example, I just needed $ GIT_EDITOR=nano git rebase -i origin/master because my X was not available to use my regular editor.
  • cropredy
    cropredy over 7 years
    To add the Notepadd++ params, I had to do this: editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar - that is, params outside the delimiting single quotes
  • yaitloutou
    yaitloutou over 7 years
    for sublime text 3: git config --global core.editor "subl -n -w" ~
  • Stevoisiak
    Stevoisiak almost 7 years
    For Windows users, you can use Notepad with git config --global core.editor "notepad"
  • Automatico
    Automatico almost 7 years
    Visual Studio Code also support the -w parameter. Eg. $ git config --global core.editor "code -w". Neat stuff
  • Andrew Ward
    Andrew Ward over 6 years
    ISSUE: On a mac , configured with "core.editor=mvim --nofork --remote-wait", I find that if I have OTHER vim windows open, unrelated to the git update/session, the --remote-wait requires ALL of them to be closed before git recognizes it's editor session as completed. Need to make the --remote-wait ONLY apply to the single window launched for that git editor session. Any suggestions?
  • Gabriel Staples
    Gabriel Staples over 6 years
    I just posted a more thorough Sublime Text answer here: stackoverflow.com/a/48212377/4561887. Thanks for your answer on it to get me started!
  • Gabriel Staples
    Gabriel Staples over 6 years
    Alternate answer here: stackoverflow.com/a/48212377/4561887, with Linux example.
  • VonC
    VonC over 6 years
    That looks quite detailed. +1
  • Radon8472
    Radon8472 over 6 years
    better you add the line like this core.editor = 'notepad' .git/COMMIT_EDITMSG --wait so it opens and saves the default edit message and you dont need "save as"
  • yegeniy
    yegeniy about 6 years
    In Sublime Text 3, there is now a Git Commit syntax type. You can skip the custom "sublime-text project" step now.
  • Gabriel Staples
    Gabriel Staples about 6 years
    I disagree: that does syntax highlighting only. My custom project doesn't touch syntax higlighting: it sets rulers and gives a context for your git commits to open up in so they don't open up in whatever project you have or last had open. The two are unrelated; do them both.
  • Gabriel Staples
    Gabriel Staples about 6 years
    I just added step 2.5, BUT (bit "but" here): I don't always use Sublime Text 3 as my editor (sometimes I use Eclipse since it has far superior symbol tracking and indexing than Sublime even though it's otherwise a crappy editor compared to Sublime), and I really prefer to use Git from the command-line, so I most definitely do not recommend skipping Step 1 even if you install the "Git" package into Sublime as I've described in Step 2.5 just now. Yes you can do Git Commits straight from Sublime but I'd prefer the command line.
  • KayakinKoder
    KayakinKoder almost 6 years
    If you're having an issue setting up Sublime (or any installed editor, really) often supplying the full path fixes the issue: git config --global core.editor "'C:/Program Files/Sublime Text 3/subl.exe' --wait"
  • KayakinKoder
    KayakinKoder almost 6 years
    Instead of editing gitconfig manually you can use this command git config --global core.editor "'C:/Program Files/Sublime Text 3/subl.exe' --wait"
  • Major
    Major almost 6 years
    @haziz update-alternatives will show any editors that have been installed. Koen just doesn't have Emacs installed.
  • Timo
    Timo almost 6 years
    TIP: If you use sublime with git and also use trim_trailing_white_space_on_save you want to add an alias for patch adding because removing trailing white space breaks patch edits where it's very much meaningful. This can be achieved with something like this: git config --global alias.patch "-c core.editor=vim add --patch"
  • miguelmorin
    miguelmorin over 5 years
    I use git config --global core.editor "emacs -nw -q", where the -q skips initialization files.
  • XPlatformer
    XPlatformer about 5 years
    without the quotes, you'd need a lot of escaping: [core] editor = c:/Program\\ Files\\ \(x86\)/Vim/vim81/vim.exe
  • benichka
    benichka almost 5 years
    Thanks, I was struggling with the fact that just "vim" launched the "internal" vim instance for Git (without my preferences, etc.). Providing the full path to my instance solved the issue!
  • Alexis Wilke
    Alexis Wilke over 4 years
    When I install vim-gtk3 on my Ununtu 16.04 (and I suspect 18.04), the default is automatically switched to vim, until then I have nano as the default. So I think that may be why some people never need to run this command and it works as expected.
  • EA304GT
    EA304GT over 4 years
    Works in *buntu18.04LTS
  • Tom Hale
    Tom Hale almost 4 years
    It's best to set $EDITOR to vim -e to be pedantically safe.
  • Bar Horing
    Bar Horing over 3 years
    VSCode users: git config --global core.editor "code --wait"
  • alper
    alper almost 3 years
    How could I set editor for git merge ? Does the core editor cover that as well?
  • Pathros
    Pathros over 2 years
    git config --global core.editor "vim" worked for me in Laravel Homestead