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.
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.
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:
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.
Now for the moment of truth. Here's the line of code you want to add to your
alias subl='"/c/Program Files/Sublime Text 2/sublime_text.exe"'
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
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
alias ls='ls -Alh --color=always' alias subl='"/c/Program Files/Sublime Text 2/sublime_text.exe"' alias irb='ruby -S irb'
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.