Greycastle Logo

Alias for windows command line

Alias for windows command line

When working with git and node js you end up using the command line quite a lot. As a Windows grown developer this is kind of a strange sensation really, you quickly start to appreciate why those Linux guys constantly advocated the bash and you also understand why they didn’t move over to windows. The windows command line leaves a lot to wish for and if you try to do the simplest things you’ll have to fight long and hard to get there.

Anyway, I wanted to open some files for editing from the command line and I want to open them using Sublime. I could add the Sublime directory to my path variable and open it using sublime_text, this feels quite naff to be honest and I came across some posts saying “just alias it”. What’s that I wonder, hmm.. okay only available for OS X/Linux, darn.

I’ll just have to create an alias for Windows then. This here batch file will do exactly that, put it in any folder in your path variable (I’ve got a common one set up for all my command line tools), then write this:

alias sublime "C:Program FilesSublimesublime_text.exe"

That’ll create you an alias command batch file that will trigger Sublime with any arguments you give it:

C:Code>alias sublime "c:Program FilesSublime Text 2sublime_text.exe"
Created alias: sublime
Alias will trigger: "c:Program FilesSublime Text 2sublime_text.exe"
Alias file is located next to alias.cmd: "C:CodeToolsBinsublime.cmd"

The file isn’t complicated at all, it just contains:

"c:Program FilesSublime Text 2sublime_text.exe" %*

True, I could’ve just written this once and been done with it 🙂 but I thought, why not give us in the Windows world a bit of that bash yummyness. Here’s the full content of the alias.cmd batch file for any one interested:

@ECHO off

IF "%1" == "" goto failUsage

WHERE alias.cmd > tmpPathFile
SET /p currdir= < tmpPathFile
del tmpPathFile

SET currdir=%currdir:alias.cmd=%

SET alias=%1
SET targetName="%currdir%%alias%.cmd"

SET params=
:loop
SHIFT
IF _empty_%1 == _empty_ goto loopDone
SET params=%params% %1
goto loop
:loopDone
SET params=%params:~1%
SET output=%params% %%*

Echo %output% > %targetName%
Echo Created alias: %alias%
Echo Alias will trigger: %params%
Echo Alias file is located next to alias.cmd: %targetName%

GOTO end

:failUsage
Echo Invalid arguments given, please use like this:
Echo alias ^<alias^> ^<file and arguments to alias^>
Echo.
Echo Example: alias rmdir_rec rmdir -r
Echo.

:end

Now to continue with what I was actually supposed to do… 🙂

© 2024 Greycastle