I was recently watching this Play by Play: Refactoring with Ben Orenstein on peepcode and was amazed by the way Ben used the ‘alias’ command on his Mac. I loved the idea of applying the DRY principle to my daily workflow, so I set out to find a Windows equivalent of the Unix ‘alias’. To my surprise it lead me to my old friend ‘doskey’.
Doskey allows you to create ‘macros’ that are basically the same thing as a Unix alias. The missing aspect is that doskey does not automatically remember the macros you create. They are lost once you close the console session.
After a quick search I came across Dan Fabulich’s post about how to overcome this Doskey limitation and replicate the desired behaviour.
Here is what i landed on:
Create the alias command
Create a new command file with the following contents (ie: win-alias.cmd)
@echo off IF "%*" == "" goto displayMacros IF /I "%1" == "SAVE" goto saveMacro IF /I "%1" == "LOAD" goto loadMacro goto runMacro :displayMacros doskey /macros goto end :saveMacro doskey /macros > "path/to/macros.txt" & ECHO Aliases SAVED goto end :loadMacro doskey /macrofile="path/to/macros.txt" & ECHO Aliases LOADED & doskey /macros goto end :runMacro doskey %* goto end :end
This command does the following:
- When called without any parameters, it will show you all your aliases
- When called with the ‘save’ parameter, it will overwrite your macros.txt file with your current session macros
- When called with the ‘load’ parameter, it will re-load all the macros into the current session
Create Doskey Macros File
The first macro we need to create will be to map ‘alias’ to the ‘win-alias.cmd’ we just created. To do this:
doskey alias="/path/to/win-alias.cmd" $*
Now when you execute the ‘alias’ command it will run the win-alias.cmd passing any parameters you include
Now lets save our new macro out to a persistent file
This will create the ‘macros.txt’ file in the location you specified in the win-alias.cmd. The contents should look something like the following:
Load Doskey Macros
Create a file with the following contents (ie: CommandAutoRun.cmd)
@echo off doskey /macrofile="path/to/macros.txt"
Open RegEdit and navigate to “HKEY_CURRENT_USER\Software\Microsoft\Command Processor”
Create a new string (SZ) key named “AutoRun” with a value to the full path of the command file you just created
Now, every time you open a new command prompt this command file will be executed and the doskey macros file will be loaded.
Now you can create new aliases as you need them. For example, say you notice that every time you ‘cd’ into a directory you always list out the contents of the folder. You could create a new alias to make the two actions the new default behaviour.
alias cd=cd $* $t dir alias save cd /some/folder
Now when you use the ‘cd’ command it will automatically list the folder contents
I used some special reserved syntax in this command
- $* is the same as %* in a command file. It will just pass along all the extra arguments you specify. In this case, the folder you are opening
- $t separates multiple commands
See the documentation for a full list of doskey options.
Some helpful aliases
Here some of the aliases in my macros.txt file that I find useful:
cd=cd $* $t dir gs=git status gl=git log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative ga=git add . $t git status gaf=git add -A $t git status gc=git commit -m $* gpush=git push origin master gpull=git pull origin master npp=start "Notepad++" /B "C:\Program Files (x86)\Notepad++\notepad++.exe" $* subl=start "Sublime Text" /B "C:\Program Files\Sublime Text 2\sublime_text.exe" $* curl=start "curl" /B "\Utils\Curl\curl.exe" $* portqry=start "PortQry" /B "\Utils\PortQryUI\PortQry.exe" $*
Lastly, here is a post that shows how you can add comments to your macros.txt file if you wan to organize the contents