Git for Windows tip: opening Sublime Text from bash

I wrote a post about how to open NotePad++ from the bash prompt a few years ago but I recently made the switch to using Sublime Text 2 as my standard text editor and had to figure out how to do the same thing with Sublime. It was surprisingly difficult to find it on Google.

1. Create a text file called subl (with no extension) with the following content:

#!/bin/sh
"C:\Program Files\Sublime Text 2\sublime_text.exe" $1 &

2.  copy it into the C:\Program Files (x86)\Git\bin folder.

The first line indicates that this is a shell script. The first part of the second line is the path to the Sublime exe. The $1 parameter passes in any parameters so that you can use it like this from the bash prompt (command line) to open a file:

subl text.txt

or

subl .

to open the current folder.

The last parameter & indicates that it should open Sublime in the background so that you can continue using the command prompt.

40 thoughts on “Git for Windows tip: opening Sublime Text from bash

  1. Great! Though make sure sublime isn’t configured to “Run as administrator”, otherwise you’ll get the error “Bad file number” when running subl.

    1. PANKAJ, go to the location where you created the subl.txt file. Then right-click, rename, and remove the .txt. It will give you an error stating the file will be unreadable. Once you do this open git command line again and type subl . you should then see the sublime text editor open. Hope that helps

      1. I’m getting the same thing, yet there is no .txt I made it specifically that way. Could you pleas help?

  2. hi michael, i still receive the same error as pankaj every after the fix. Can you pls help?

    I got the same problem. I an on windows, but can’t seem to get the .text extension of…

  3. I am using windows 10 and I have done everything as told. Also I have even pasted the subl file to all the directories mentioned in the comments but still get :command not found”

    1. @Ivan (and anyone else having issues in Windows 10) – I was having the same issue, and was able to resolve through a combination of peoples comments in this thread. The problem is that ‘C:\Program Files (x86)\Git\bin’ wasn’t where my git bash was actually launching from, so it makes sense that it had no idea what ‘subl’ was. I will attempt to detail how to resolve this issue:

      – First, launch your git bash client and type ‘explorer.exe .’

      – Assuming you haven’t modified the path which git defaults to on launch, this will open your home directory in Windows Explorer. This will be the starting path for where you will add the ‘subl’ file.
      (NOTE: if you have modified your start path, just type ‘cd /’ and that should bring you to the home directory, then start from the first step again)

      – In the instructions from this post, it tells you to add the ‘subl’ file to ‘C:\Program Files (x86)\Git\bin’, but it should in fact be added to ‘[path from above]\usr\bin’.
      For me, it was ‘C:\Users\[userFolder]\AppData\Local\Programs\Git\bin’

      – Once I placed the ‘subl’ file in that directory, I was able to launch programs with sublime text as described!

      Hope this helps someone!
      TK

  4. Anyone who has recently downloaded Sublime will need to change the path in the copied text to reflect the correct verion (Version 3) instead of the stated version (Version 2). Otherwise the bin file will not have a legitimate path to Sublime.

    1. Ah, some content correction: alias subl=’/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe –multiinstance’

      Now that works. Also I have added to run the editor in “–multiinstance” mode. If you do not like it – remove. But I do :)

  5. For those of you who are getting the ‘bash: subl: command not found’ error:
    Try to save the file with ” ” around the file name. Also, place it in the Git/cmd directory.

    Hope this helps :)

Leave a reply to Jason Inman Cancel reply