Monday, August 16, 2010

Permanent Windows command-line aliases with doskey and AutoRun

On Unix, you can configure command line aliases, like this: "alias lls=ls -l". From then on, running "lls" will automatically run "ls -l", saving you a few keystrokes. The alias will go away whenever you open a command prompt; to make the alias permanent, you can save it in your .profile or .bashrc.

On Windows, you can do the same thing using the DOSKEY utility to create "macros" like this: "doskey ls=dir". Doskey macros are basically equivalent to Unix aliases (though they have different advanced features).

Automatically loading your Doskey macros is a bit more trouble, however. Doskey allows you to export a macro file, like this: "doskey /macros > my-favorite-macros.txt". You can then import your macro file like this: "doskey /macrofile=my-favorite-macros.txt".

Instead of your .profile, you'll need to configure your AutoRun registry value, in "HKEY_CURRENT_USER\Software\Microsoft\Command Processor". I have my AutoRun set to "c:\dev\autorun.bat" which runs a variety of helpful utilities.

I hope this helps!

2 comments:

James Brown said...

If you put this in your doskey macro file you can define new aliases, view the current aliases and load and save aliases using the alias command. This assumes you store your macros in doskey.mac in your user profile directory.

alias=IF "$*" == "" (doskey /macros) ELSE (IF /I "$1" == "SAVE" (doskey /macros $g "%USERPROFILE%\doskey.mac" & ECHO Aliases SAVED) ELSE (IF /I "$1"=="LOAD" (doskey /macrofile="%USERPROFILE%\doskey.mac" & ECHO Aliases LOADED & doskey /macros) ELSE (doskey $*)))

Anonymous said...

Thanks, it works, just what I was looking for.