224

I have a problem setting Sublime Text 2 as the core.editor with git.

I've read through every post I could find addressing the problem, but still nothing is working for me. I am running Windows.

I have done:

git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe'"

and tried that with various arguments like -m. When I open my .gitconfig, this is what is in there:

[user]
    name = Spencer Moran
    email = smoran02@gmail.com
[core]
    editor = 'C:/Program Files/Sublime Text 2/sublime_text.exe'

If I go to Git and type:

README.markdown --edit

the README file opens in Notepad, not Sublime Text.

Does anyone have any idea what I'm doing wrong or how I could fix this?

Oleg Sliusar
  • 25
  • 3
  • 10
Spencer Moran
  • 2,320
  • 3
  • 13
  • 13
  • 12
    If you're typing _only_ `README.markdown --edit` in cmd.exe, `git` isn't involved at all. Change your file associations in windows if you want that to work. – Mat Jan 21 '12 at 07:58
  • 1
    A general FYI for most of the answers below: `-w` means "Wait for the file to be closed before returning," and `-n` means "Open a new window." Source: http://docs.sublimetext.info/en/latest/command_line/command_line.html – MarredCheese Apr 05 '17 at 18:18
  • My favorite settings for using Sublime Text 3 as my git editor: https://stackoverflow.com/questions/2596805/how-do-i-make-git-use-the-editor-of-my-choice-for-commits/48212377#48212377 – Gabriel Staples May 24 '18 at 00:02

16 Answers16

359

Windows

Sublime Text 2 (Build 2181)

The latest Build 2181 just added support for the -w (wait) command line argument. The following configuration will allow ST2 to work as your default git editor on Windows. This will allow git to open ST2 for commit messages and such.

git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"

Sublime Text 3 (Build 3065)

Sublime Text 3 (Build 3065) added the subl.exe command line helper. Use subl.exe -h for the options available to you. I have hot_exit: true and remember_open_files: true set in my Sublime Text user settings. I have found the following to git config to work well for me.

git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

Mac and Linux

Set Sublime as your editor for Git by typing the following command in the terminal:

git config --global core.editor "subl -n -w"


With this Git config, the new tab is opened in my editor. I edit my commit message, save the tab (Ctrl+S) and close it (Ctrl+W).

Git will wait until the tab is closed to continue its work.

chicks
  • 1,901
  • 2
  • 19
  • 32
jrotello
  • 4,271
  • 1
  • 18
  • 15
  • 14
    Personally, in addition to setting my editor as @jrotello suggests, my ST2/git experience improved a lot when I add the following options to my ST2 settings: { "hot_exit": false, "remember_open_files": false, "close_windows_when_empty": true } – Sandy Jul 30 '12 at 17:30
  • @Sandy Can you explain? I'm kind of fond of these settings and I'd like to know the flip side – Kos Nov 17 '12 at 09:27
  • 3
    Note that the above path uses forward slashes instead of backslashes. Using backslashes causes it to fail (even when stored with `c:\\program...` in the .gitconfig). – Phrogz Jan 02 '13 at 19:46
  • 6
    @Phrogz: I was able to get it working in my .gitconfig with the following syntax: `editor = 'C:\\Program Files\\Sublime Text 2\\sublime_text.exe' -w` – awayken Feb 27 '13 at 20:23
  • 4
    For me, this will open a new tab if sublime text is already open. Closing the tab, doesn't let git know its done. I've tried --multiinstance, but it doesn't seem to do anything. – David Faivre Jun 10 '13 at 12:49
  • 5
    note that when `hot_exit` is false and the lights went out all unsaved work will begone!!!!!!!!!! – Jürgen Paul Aug 14 '13 at 11:08
  • 2
    @David: On Sublime Text 3 (3059) at least, adding -n works for opening a new window - found at http://www.sublimetext.com/docs/3/osx_command_line.html – jdasilva Mar 22 '14 at 18:37
  • 4
    Actually, using "-n -w", git doesn't seem to be able to tell that the sublime text window closed (if I have an existing sublime window open at least). "-w --multiinstance" still seems to work the best, though in the new sublime window any existing tabs are re-opened. It seems they can safely be ignored. – David Faivre Mar 24 '14 at 15:47
  • @David: I'm using the same settings as up in the first comment from Sandy and it seems like your scenario is working for me. I get a nice fresh window and closing the tab or window works. (For me, I'm OK with those settings in general use.) – jdasilva Mar 24 '14 at 18:27
  • On Win8.1, using ST3 + Git version 1.9.4-preview20140611, after adding the program to my path and restarting I can launch ST3 via "git commit" successfully with commit info in a file named "COMMIT_EDITMSG". However, Git, immediately on file launch, says "Aborting commit due to empty commit message." - saving and closing ST3 after entering a commit message does nothing in Git. – danjah Jul 29 '14 at 22:55
  • I got it to work under Windows by using only `editor = subl.exe -w` and adding the Sublime Text directory to the path environment variable. – kleinfreund Feb 09 '15 at 20:06
  • @kleinfreund when you say that you used `editor = subl.exe -w` do you mean Powershell or Git Bash? I ran this in both but it's resulting in a sytax error- is there anything that I need to add before or after the command? Also- can you explain what I should do to add the Sublime Text directory to the path environment variable? – Ryan Chase Nov 24 '16 at 04:06
  • All, I am using Git Bash on a Windows machine I'm running `git config --global code.editor "'C:\\Program Files\\Sublime Text 3\\sublime_text.exe' -n -w -m"` but when I run git commit it's opening in the Notepad. Before adding the -m flag it was opening in the Git Bash editor. I have downloaded Sublime 3 and the exe file is called "sublime_text.exe". I have not done anything else (for example, I have not changed the environment variables). Any thoughts? – Ryan Chase Nov 24 '16 at 04:33
  • I had trouble with subl.exe never exiting (windows 10 RS4/sublime 3126) no matter the flags. Oddly enough using the regular sublime_text.exe with -w -n worked. – Jonathan Gawrych May 26 '18 at 21:56
95

You can use this command on Mac and Linux:

git config --global core.editor "subl -n -w"
fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
sfufoet
  • 1,011
  • 6
  • 4
  • 26
    Actually, the recommendation on Mac these days is to symlink subl to somewhere in your $PATH and set your editor to 'subl -w'. It's in the official docs. – Sandy Jul 30 '12 at 16:39
  • 8
    The docs I referred to are: http://www.sublimetext.com/docs/2/osx_command_line.html – Sandy Jul 31 '12 at 22:18
  • 8
    The docs say to use `subl -w`, but if you have other tabs open this won't work correctly. I've found `subl -n -w` works better since it opens a new window. – iano Jun 06 '13 at 22:35
  • 1
    @iano if i do that should i close tab or close window to commit? close window didnt work – Karthik T Aug 05 '13 at 03:48
  • This opens an empty document in sublime for me until I followed these instructions: http://www.sublimetext.com/docs/3/osx_command_line.html but changed the path from Sublime Text.app to Sublime Text 2.app – Evanss Jul 10 '15 at 09:04
  • I had to use ```ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/``` to create the symlink for ```subl```first. See [here](http://stackoverflow.com/a/25154529/1505341). – Kerem Oct 27 '15 at 13:29
17

For Mac OS X in the file ~/.gitconfig under [core] I had to put this code to solve the issue on my end.

editor = /Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl  -n -w

This was when subl was working fine but git was unable to access it. And was displaying these errors

subl -n -w: subl: command not found
error: There was a problem with the editor 'subl -n -w'.
Ahmad Awais
  • 23,458
  • 4
  • 66
  • 52
16

For Mac & Sublime Text 3:

If git config --global core.editor "subl -n -w" doesn't work, use this git config command:

$ git config --global core.editor "'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' -n -w"

Note: it does not contain escaping backslashes.

nu-bo
  • 271
  • 2
  • 5
7

what worked for me in cygwin/zsh:

in /usr/local/bin create subl_git file

#!/bin/bash
/cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe -w -n `cygpath -w $@`

in gitconfig:

editor = /usr/local/bin/subl_git
meeech
  • 1,444
  • 13
  • 20
  • excuse my newbie question but how do I access the gitconfig? Is this different than running `git config --list`? Thanks in advance. – Ryan Chase Nov 24 '16 at 04:13
  • that just lists them out. you ned to edit your gitconfig – meeech Dec 08 '16 at 09:26
  • I had a space in my user folder so I used a modified -`"/cygdrive/c/Program Files/Sublime Text 3/subl.exe" -w -n "\`cygpath -d -w "$@"\`` – Mark Jan 19 '17 at 00:40
6

With Sublime Text 2 and the following configuration, if nothing happens when you close the git commit's comment file :

git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"

If the git commit doesn't finish when you close the COMMIT_EDITMSG comment file in Sublime, I've found this is probably because you are running CMD in administrator mode (and have a Sublime Text already opened in non admin mode). I had the same problem and it worked for me when I started my CMD in normal mode.

Ps : just wanted to answer to the problem @david pointed out in the accepted answer's comments, but I couldn't post comment since I'm new here... so I've made a new answer, because I think this can be useful to some people encountering the problem.

Francois
  • 71
  • 1
  • 6
  • Thanks for the tip. Do you know of a solution? – Scott Wegner May 04 '15 at 17:38
  • @ScottWegner Just launch your CMD in non-admin mode, as a workaround. You don't need to be in admin mode to run git commands. However, If you really need to be in admin mode, then I didn't find solution for this, except open another CMD instance. – Francois May 13 '15 at 10:27
  • thanks for the follow-up. I end up with elevated shells because my development environment requires admin-mode for other tools (Visual Studio), thus I run my entire shell elevated. I think I'll explore only elevating the tools that need it. – Scott Wegner May 13 '15 at 15:23
  • 1
    @ScottWegner I personally use [Cmder](http://gooseberrycreative.com/cmder/) (the full version even includes msysgit). It allows mulitple instances of CMD, bash or other Shells, in a unique window with tabs. You can launch some tabs in admin mode and others in normal mode. I usually have one tab dedicated to git and other tabs for my other tools. Maybe you would be interested in giving it a try. It's a great tool! – Francois May 13 '15 at 18:27
  • re: admin mode - i think the trick is that your shell and sublime have to be using the same privileges. i.e. if you're running your terminal elevated and sublime is also elevated it works smoothly. – Joseph Evensen Oct 03 '18 at 21:42
5

I just successfully made it with the help of PowerShell (command line tools for windows, which is also be use as the default command line on GithubforWindows)

Just make a folder "WindowsPowerShell" on "C:\Users\%username%\My Documents". Then, create a file called "profile.ps1". Make sure that you have the correct file extension, .ps1 and not .txt. inside the profile.ps1, put this line to set an alias command:

Set-Alias subl 'c:\program files\sublime text 2\sublime_text.exe'

Make sure the sublime directory on your machine is correct. You can also see the configuration details in here.

Now, I can edit files with Sublime Text 2 via PowerShell command just by typing:

subl filename

Hopefully this is usefull...

rwinzhang
  • 2,545
  • 2
  • 14
  • 11
3

I've faced the same problem with git editor in Windows this evening, and finally I've got it.

You'll need to set your editor path with the bash syntax and using the DOS 8.3 format to avoid spaces:

This doesn't work (for me):

git config --global core.editor "C://Program files/Sublime Text/sublime_text.exe"

This do:

git config --global core.editor /C/PROGRA~1/SUBLIME~1/sublime_text.exe

You can get the DOS 8.3 name for a directory with dir /x

Diego Buendia
  • 97
  • 1
  • 7
1

I was just struggling with this for some reason, none of the examples above quite worked for me. This was my solution however:

 git config --global core.editor "'c:/<path to sublime>/sublime_text.exe' -w $*"

You specifically need the ' ' around the path and the $* at the end of the args, all in " ".

justin.m.chase
  • 11,241
  • 6
  • 42
  • 84
1

For MAC Users I can say for Sublime use the following:

First Check the .gitconfig file using command:

  1. cd ~
  2. ls -la
  3. cat .gitconfig

Now one can add this configuration:

git config --global core.editor "subl -n -wl1"

Where l1 will tell to start at line 1.

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
bvmCoder
  • 1,083
  • 9
  • 7
1

If you are Mac User, then here is how:

git config --global core.editor '/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
mainframer
  • 15,543
  • 11
  • 43
  • 63
1

well, i had some trouble invoking notepad++ from gitbash even after setting it as my core.editor.

i got it all solved by appending the below line to my .bashrc file in my gitbash installation directory.

alias npp='winpty "C:/Program Files (x86)/Notepad++/notepad++.exe"'

so, i can invoke notepad++ with the alias "npp". i feel this can help you if you change the directory to the respective editor you want to use.

OLATOMIDE
  • 73
  • 1
  • 5
0

For Windows 7 I have found the following solution works:

git config --global core.editor "C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe -n -w"
tscott1
  • 3
  • 2
0

On Windows, if you are installing git, you can select other editor and give the following command line. C:\Program Files\Sublime Text 3\sublime_text.exe -w Installer Image

Xavier John
  • 5,857
  • 3
  • 25
  • 32
0

I haven't seen any instances of updates regarding Windows allowing Git installation to actively pick Sublime Text as your default text editor without all the sturm und drang of the original override codes... Sublime Text shown as option in (re)installation of Git

Angelfirenze
  • 77
  • 1
  • 13
0

On MacOS(Mojave) below is working for me for Sublime 3:

git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w"

Pratik Patel
  • 1,424
  • 2
  • 16
  • 28