Last Updated: October 20, 2017
·
27.23K
· mitchp

Launch Sublime Text From the Command Line in Windows

This tip could have also been titled Adding Command Line Aliases (i.e. Shortcuts) in Windows but I learned this process because I was trying to add a shortcut to launch Sublime Text 2 from my CLI (command line) in Windows 7 by typing "subl". Long story short, these instructions should work with adding any alias.

Step 1

Download Git for Windows if you don't already have it. We're not using PowerShell here. We will use Git Bash once you've installed Git for Windows. I installed a nicer command line client called Console. Once you're all set with a bash command line for Windows we can find or create the file where we will store command line shortcuts, which are called aliases.

Step 2

Go to your root directory ~/ (you know, the one with your profile name as the folder name). I'm not talking about your C:\ drive. Depending on your situation, you may or may not already have the file we want to find or create: .bashrc

Remember, you can show your hidden files in Windows 7 by going here. If you don't see a file called ".bashrc" in your root directory, create one using Sublime Text 2 (which I'm assuming you have already downloaded since you want to create a shortcut to it). Create a new file in Sublime Text 2 and Save As ".bashrc", then put it in your root directory.

Step 3

Now for the moment of truth. Here's the line of code you want to add to your .bashrc file:

alias subl='"/c/Program Files/Sublime Text 2/sublime_text.exe"'

The alias subl is pretty clear, right? alias tells the the CLI what it is that you're making, and the subl is the text that you want to type to automatically launch Sublime Text 2. You could even make it alias st and then all you'd have to type is st

The part that is tricky is the file path. You have to tell the CLI where the file is that you want to launch when that alias is typed. In my case, and I think this is the case for most others on Windows, it automatically installed the Sublime Text 2 file .exe in Program Files (not the "(x86)" one). If you snoop around in your Program Files in the C:\ drive you will hopefully find the Sublime Text 2 folder there as well. If not, look around for it and copy the file path.

In my case, the .exe file to run Sublime Text 2 was directly in the Sublime Text 2 folder, so as you can see in my snippet, my path was "/c/Program Files/Sublime Text 2/sublime_text.exe". If yours is different, adjust this file path accordingly and add it in to .bashrc the same way I did.

It can be tricky because you have to put single quotes around the code that comes after alias subl=, but in this case you also have to put double quotes around that file path. So take note of that: Double quotes inside of single quotes.

Aliases can also be used to run long command line entries that you don't want to fully type. For those, you won't need double quotes, just single quotes. Here's what I have in my .bashrc:

alias ls='ls -Alh --color=always'
alias subl='"/c/Program Files/Sublime Text 2/sublime_text.exe"'
alias irb='ruby -S irb'

Step 4

Now you can just open your CLI and simply type subl to try it out. If it doesn't work, you can curse my name, but if it does work, congratulations! I believe you can use this technique to make custom shortcuts in the command line 'till your heart's content! Maybe you want to shorten git push to just gp next. Here's the line of code you would add for that:

alias gp='git push'

Let me know if I've given any bad, wrong, stupid, etc. advice in this post in the comments. I'm still a very green developer.

10 Responses
Add your response

That's a lot of work when you can do:

@CALL "C:\Program Files\Sublime Text 2\sublime_text.exe" %*

Just put that (assuming that's the correct path to sublime) in a batch file of your liking (subl.bat) and make sure that batch file is in a folder that is in your PATH.

over 1 year ago ·

@CALL does not return control to the command line. I use the following in a batch file in my PATH:</p>

@echo off
start "" "C:\Program Files\Sublime Text 3\sublime_text.exe" %*
over 1 year ago ·

No of the above solutions worked for me.

over 1 year ago ·

Not even the one in the main article? because that´s easy enough to understand and apply.

over 1 year ago ·

@mitchp/guys,
Can you help out how can I do the same using ConEmu? I use this as client for all cli purposes.

over 1 year ago ·

in your CLI (command line) or Git Bash (what im using) type in: alias subl='"/c/Program Files/Sublime Text 2/sublimetext.exe"' (this is the directory of where your sublimetext.exe file is. Remember that "subl" or "st" is to your discretion. I chose alias st so i can just type "st" in my command line and BAM it worked!

over 1 year ago ·

Hi.
There was a problem that I have experienced after did this.
If you open a file via subl subl a.file then it will be run as the sublimetext application itself, which means if you ctrl+c (stop the command) the application will be terminated(that really annoyed). That different with macosx version that will be run as a sub-application.

In current version (Build 3065) there is a subl.exe. But this file can't be executed if the sublimetext application itself not running.

over 1 year ago ·

Worked for me! Thanks!

over 1 year ago ·

What johanhammar said ..

over 1 year ago ·

I believe you missed a step, for users who had to create a .bashrc file for the first time, they need to source it in the command line, typing the following should do the trick:

. ~/.bashrc
over 1 year ago ·