How do I make git use the editor of my choice for commits?
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, thecore.editor
configuration variable, theVISUAL
environment variable, or theEDITOR
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 pressEnter
. It will pop open Notepad++. - Write your commit message at the top of the file, and save and close the file. Done!
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, 2022Comments
-
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.
-
Michael Burr about 14 years"How to get git to go to vim for commit comments from the git-go?"
-
Stevoisiak almost 7 yearsRelated (possible duplicate): How can I set up an editor to work with Git on Windows?
-
Shawn Eary almost 3 yearsWhat's wrong with EMACS (smile) ?
-
-
armandino about 14 yearsBtw, the above is true for CVS and SVN, and I guess other version controls.
-
Ralph Sinsuat about 14 years@armandino: Yes, the others might use
VISUAL
orEDITOR
, but they certainly don't useGIT_EDITOR
orcore.editor
. -
armandino about 14 yearsThanks for clarifying Mark. I meant the EDITOR variable. I believe the GIT_EDITOR (if defined) simply overrides it.
-
brasskazoo about 14 yearsYep - 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 about 14 yearsThe EDITOR environment variable has the advantage that a number of other programs will respect it as well.
-
Anonigan about 14 yearsNote 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 almost 13 yearsFor the sake of completeness, core.editor means [core] editor = ... in the file
-
Marco M. almost 12 yearsyou can test you successfully changed it by trying to amend the last commit message.
git commit --amend
-
MichielB about 11 yearsOn Debian this also works, thanks! It defaults to pico - argh.
-
icl7126 about 11 yearsfinally 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 almost 11 yearsYou 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 over 10 yearsNice! Thanks Anmol, I was having that issue where it was committing on an empty message.
-
Pablissimo over 10 yearsIf 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 over 10 years@givanse what do you mean?
-
Rup over 10 yearsI guess he means it's trivial by analogy to the vim answer ?
-
Matej over 10 yearshmm no it isn't, but if you use something else than vim (such as textmate).
-
Abramodj about 10 yearsYou should change your answer to git config --global core.editor "vim -w" to actually allow the user to write a commit message.
-
Adam F about 10 yearsWhy would you want to use sublime for git commits?
-
Anmol Saraf about 10 yearsAny 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 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 usemvim
, then the editor you specify should bemvim -f
rather thanmvim
. -
Josef about 9 yearsto 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 almost 9 yearstextmate also has a "wait" option, so for me, it's this:
git config --global core.editor "/usr/local/bin/mate -w"
-
Nick Volynkin almost 9 yearsFrom 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 almost 9 yearsSomehow it's not delivering the text to git. I got 'Aborting commit due to empty commit message.' error.
-
Mahendran almost 9 yearsI have used the first command to setup SL. cmd + S then cmd +W to close editor
-
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 gotAborting commit due to empty commit message.
, and -W opened a vim welcome window. -
underscore_d over 8 yearsOf 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 about 8 yearsstill opens the normal notepad for me
-
kukinsula about 8 yearsHere's how to set emacs in terminal mode when comiting
git config --global core.editor "emacs -nw"
-
mwfearnley about 8 yearsInstead 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 about 8 years
--remote-wait-silent
to avoid ugly error message :) -
Technext almost 8 years@ErikE: Thanks man! Both
-multiInst
and-notabbar
were required. This was bugging for the last few hours. -
Ninja almost 8 yearsIt works, while
GIT_EDITOR
andEDITOR
not work --ubuntu -
Klaas over 7 yearsIf TextWrangler is already open your solution will open another instance with the same file set of the first one. This seems dangerous.
-
Klaas over 7 yearsI 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 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 over 7 yearsIf you have both
core.editor
andGIT_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 over 7 yearsTo 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 over 7 yearsfor sublime text 3:
git config --global core.editor "subl -n -w" ~
-
Stevoisiak almost 7 yearsFor Windows users, you can use Notepad with
git config --global core.editor "notepad"
-
Automatico almost 7 yearsVisual Studio Code also support the
-w
parameter. Eg.$ git config --global core.editor "code -w"
. Neat stuff -
Andrew Ward over 6 yearsISSUE: 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 over 6 yearsI 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 over 6 yearsAlternate answer here: stackoverflow.com/a/48212377/4561887, with Linux example.
-
VonC over 6 yearsThat looks quite detailed. +1
-
Radon8472 over 6 yearsbetter 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 about 6 yearsIn Sublime Text 3, there is now a
Git Commit
syntax type. You can skip the custom "sublime-text project" step now. -
Gabriel Staples about 6 yearsI 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 about 6 yearsI 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 almost 6 yearsIf 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 almost 6 yearsInstead of editing gitconfig manually you can use this command
git config --global core.editor "'C:/Program Files/Sublime Text 3/subl.exe' --wait"
-
Major almost 6 years@haziz
update-alternatives
will show any editors that have been installed. Koen just doesn't have Emacs installed. -
Timo almost 6 yearsTIP: If you use
sublime
withgit
and also usetrim_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 over 5 yearsI use
git config --global core.editor "emacs -nw -q"
, where the-q
skips initialization files. -
XPlatformer about 5 yearswithout the quotes, you'd need a lot of escaping: [core] editor = c:/Program\\ Files\\ \(x86\)/Vim/vim81/vim.exe
-
benichka almost 5 yearsThanks, 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 over 4 yearsWhen I install
vim-gtk3
on my Ununtu 16.04 (and I suspect 18.04), the default is automatically switched tovim
, until then I havenano
as the default. So I think that may be why some people never need to run this command and it works as expected. -
EA304GT over 4 yearsWorks in *buntu18.04LTS
-
Tom Hale almost 4 yearsIt's best to set
$EDITOR
tovim -e
to be pedantically safe. -
Bar Horing over 3 yearsVSCode users:
git config --global core.editor "code --wait"
-
alper almost 3 yearsHow could I set editor for git merge ? Does the core editor cover that as well?
-
Pathros over 2 years
git config --global core.editor "vim"
worked for me in Laravel Homestead