Home > Point2 - Technical > Setup Persistent Aliases & Macros in Windows Command Prompt (cmd.exe) using DOSKey

Setup Persistent Aliases & Macros in Windows Command Prompt (cmd.exe) using DOSKey

May 14, 2010

Our development machines here at Point2 are not standardized; we have a mixture of Windows XP, 7, and Mac OSX/Unix computers. I find myself constantly switching back and forth between command prompt interfaces when pair programming. As a result, I catch myself using “ls” to list a directories contents regardless of what system I am on. I am currently using a Windows XP machine for my developer box and I wanted to setup an alias to the “ls” command to actually perform a “dir”. Here is how I accomplished it…

There is a command available in a Window’s shell that let’s you “alias” command to whatever you please: DOSKey. It allows you to create “macros” to execute one or more other commands with a custom name. If you open up a command prompt and type in the following, you will now be able to use “ls” to list the current directory’s contents.

DOSKEY ls=dir

It also handles command line arguments, either by index or as a collection using $1 – $2 or $* respectively. This allows you to do things perform a “dir” command every time you change directories. This example would be done with the following macro definition.

DOSKEY cd=cd $1$Tdir

This all sounds very simple and easy until you close your command prompt, open a new one, and realize all of these macros you defined earlier did not persist between instances. The DOSKey command does not save these alias automatically. The DOSKey command does support saving the “currently defined macros” to a file which will allow you to run a simple command in any new shell to load macros from any saved file. The problem is, I already forget to use “dir” instead of “ls” so I know for sure I will not remember to run a certain DOSKey command every time I open up a new command prompt. I needed something more automatic.

I investigated what options I have for running commands as part of every run of cmd.exe. I found out there is a command line argument, \K, that I can use on cmd.exe to tell it to run a ‘.cmd’ or ‘.bat’ file to run commands on startup. So you can run something like to following command to load a shell instance with the following command file being ran automatically.

cmd.exe /K C:\path\to\file.cmd

This allows you to add all of the commands you want into that file and have them run automatically for the command prompt you are about to open. In order to pass this argument, I created a shortcut to cmd.exe in my Quick Launch toolbar which I could modify and use exclusively for my command prompt instances. This can easily be done by going into your C:\WINDOWS\system32 directory, right-clicking on cmd.exe and selecting “Send to Desktop”. Right click on the newly created shortcut (on your desktop) and select “Properties”. On the Shortcut tab, you will find the “Target” field which you will have to modify to include the command line option. Here is what my configuration looks like:

The only other thing from my configuration worth mentioning is the “Start in” setting I have specified. The value “%HOMEDRIVE%%HOMEPATH%” will open the command prompt in your user’s home directory as opposed to the default which opens the new window in the system32 directory which usually isn’t very helpful.

My doskey.cmd file is also worth taking a look at. It only currently has a few alias for common Unix commands but it will give you a good idea of what kind of things are capable.

@echo off

DOSKEY ls=dir
DOSKEY cd=cd $1$Tdir
DOSKEY clear=cls

It is probably best to also include the “@echo off” at the top of your script too just so you don’t have to see the noise in the shell running your script. There are also a lot more powerful features to DOSKey that I have yet to experiment with but you can see how easy it is now to add permanent macros into your command prompt.

Another thing worth noting is that you should look at the other tabs in the cmd.exe Shortcut Properties window because it makes it easy to do things like increase the buffer size of text for the shell, change font sizes and color, as well configure the behavior of command history tracking. After tweaking with all these settings, here is my new custom, improved Command Prompt:

The only “gotcha” here is that you have to open command prompt window using the Shortcut but there are other tools available which will let you run Shortcuts a simple keystroke so this should not be an issue. I hope this makes every Window’s Command Prompt user’s life a little easier. 🙂

Advertisements
  1. webbertiger
    August 17, 2010 at 5:38 PM

    Thanks, Jesse. You made my day. I really like customized alias commands in UNIX/Linux environment. It’s good to find a way to get similar thing in Windows.

  2. James
    November 1, 2010 at 10:44 AM

    If you save your doskey macros to a file, then you can have them installed automatically when the command shell starts by adding a REG_EXPAND_SZ to the registry at HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun. Set this string to <> and your macro commands will be available whether you start them via “run cmd” or a cmd icon. Or you could call your doskey.cmd file using this setting (although it seems less efficient than calling DOSKEY once).

  3. November 18, 2010 at 12:32 AM

    It turns out that cmd.exe has an honest-to-god init file, just like a ~/.zshrc kinda thing… It’s just that the feature isn’t turned on by default! It just needs turning on in the Registry (which I call the Special People Filesystem).

    I found out about this when I was digging around in the normally unhelpfully noisy output of “cmd /?”. So, you have to specify a filespec in the registry key

    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

    and/or the registry key

    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

    and then that filespec (presumably always to .bat) is run every time cmd starts. (Currently, the value is “”.)

    I have a c:\utils directory with personal stuff in it, and so I fired up regedit and set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor to c:\utils\_init.bat

    A probably smarter name to go with would be %HOMEDRIVE%%HOMEPATH%\cmd_init.bat which might require quotes around it. Or maybe put it in %APPDATA%\cmd_startup.bat — you get the idea.

    Regardless of where my init file goes, I have it set up with some handy aliases like you do, and the world’s a better place because of it.

    For serious unixery, you could write an ls.pl (or whatever) in Perl and do: doskey ls=perl -S ls.pl $*

    Of course, at some point you lose your mind and need cygwin, but that solves some problems while creating others.

    The large print giveth and the small print taketh away!

  4. December 23, 2010 at 12:48 PM

    I am glad other people are getting some mileage out of this not-so-obvious feature of the CMD program. Thank you for your suggestions to help make these aliases available in all terminals and not just the ones started by the shortcut. I don’t usually like messing around in the Registry but this seems like the perfect reason to get my hands dirty.

  5. Alex
    June 2, 2011 at 4:02 PM

    You can emulate a UNIX-style alias command in Windows with the following:

    doskey alias=if “.$*.” == “..” ( doskey /MACROS ) else ( doskey $* )

    Then again, it would be so much easier if Microsoft were to accept the decades of experience of the Unix community and adopt a compatible shell…

    • Robin
      August 16, 2011 at 1:04 AM

      Genius!

  6. October 9, 2011 at 11:26 AM

    Hey Jesse,

    Great work…. Keep up the good work!

  7. January 15, 2012 at 4:29 PM

    I realize this is old, but: if using multiple macros, why are you invoking doskey multiple times instead of using doskey /MACROFILE=%myMacroFile%

    • January 26, 2012 at 8:37 AM

      Using a macrofile is probably a more efficient usage but this article was written when I just found out about DOSKEY and I wasn’t aware of the more advanced options. Either way will let you set-up some nice aliases. Thanks for the tip!

  8. Prabhas
    November 2, 2012 at 12:55 AM

    Thanks Jesse. Its really great

  9. Joe Grand
    November 16, 2012 at 1:08 PM

    Thank you very much. You wrote a clear explanation that was easy to follow and implement. I really appreciate your work here.

  10. MykulH
    January 2, 2013 at 2:23 AM

    Great post, really helpful.
    Was pulling my hair out swapping between Linux and Windows and always using ‘ls’ on both, now I can.

  11. January 28, 2013 at 3:45 PM

    Many thanks – I’ve been banging my head thinking that windows didn’t have this option for a long, long time. I now feel enlightened 😀

  12. Jeff
    June 16, 2013 at 2:59 PM

    Awesome, Thanks!

  13. July 29, 2013 at 7:01 PM

    Fantastic site. Lots of helpful info here.
    I am sending it to several buddies ans also sharing in delicious.
    And obviously, thank you in your effort!

  14. Scott
    October 16, 2013 at 12:37 AM

    thanks, good tip! I appreciate your effort!

  15. February 14, 2014 at 10:58 AM
  16. Kim
    April 24, 2014 at 2:52 AM

    Thanks a lot! That was what I was looking for (although what I’m really looking for is Microsoft replacing their clunky, kludgy command/batch, ahem, stuff, with some grown-up shell)

    • mdchris
      April 24, 2014 at 7:38 AM

      They already did, it’s called powershell.

  1. September 22, 2011 at 2:23 PM
  2. November 7, 2011 at 1:41 PM
  3. December 21, 2012 at 11:09 AM
  4. April 18, 2013 at 12:05 AM
  5. August 13, 2013 at 7:07 AM
  6. September 6, 2013 at 12:53 AM
Comments are closed.
%d bloggers like this: