HOWTO Unixify your Windows cmd.exe with AutoRun registry and cygwin
Wednesday, October 11, 2006
On Windows XP I use cygwin but sometimes I need to use the native cmd.exe and since I am used to my unix look and feel (and tools) I also want to create cmd environment that resembles my cygwin and my Mac OS X prompts, at least a little bit. That little bit is setting up a custom prompt and some aliases as well as (the most important) accessing unix tools from cmd terminal as well as having access to windows command line tools at the same time from the same prompt.
Let’s dive in. Let’s say your username on windows is ‘marekj’ and your cygwin home variable points to windows %userprofile% environment variable folder which is ‘c:\Documents and Settings\marekj’ (I believe default cygwin points usually to c:\cygwin\home\username but I changed it to use my windows xp userprofile path) and that’s where I have my .bash_profile for my cygwin prompt and that’s where I will create a profile for me cmd.exe as well.
AutoRun setup
I first create a .bat profile file which I name ‘.AutoRun.bat’ (you can name it whatever you want as long as it ends with .bat) This .bat file will be executed at runtime upon starting cmd.exe (it actually is executed when starting cygwing.bat as well but will not affect cygwin).
In this file which full path is “%userprofile%\.AutoRun.bat” I’ll put all the initialization stuff at the time of cmd.exe execution.
Then I need to tell registry to use this file at runtime. Open registry editor (regedit) and create (or modify) this key:
“HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\AutoRun\REG_EXPAND_SZ”
(this is a key of type Expanded String Value)
Why make it Expandable string value? Because we are going to stuff this value there:
“%USERPROFILE%\cmd.exe.bat” (make sure you include the quotes)
If your user name is ‘marekj’ your %userprofile% value will probably be
c:\Documents and Setting\marekj
If we don’t include quotes in our value then we’ll fail because we have an empty space in our path.
Now when you start cmd.exe it will automatically execute everything in your .AutoRun.bat file.
My .AutoRun.bat file looks like this:
@echo off REM Set this: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\AutoRun\REG_EXPAND_SZ REM to hold this value "%USERPROFILE%\.AutoRun.bat" prompt $+$M$P$_$_$$$S chdir %userprofile% doskey ls=ls --file-type -1 doskey l=ls -l -Ggh --file-type doskey x=exit
As you can see I unixfy my cmd.exe eprience a bit by creating a simple custom prompt (run prompt /? in cmd.exe to get the values and their meanings) and assigning an alias (by using doskey in windows xp) to run unix ls commands instead of dir (I haven’t figured out how to define COLORS by file type to run ls --color --file-type. Currently it’s using 16 windows colors but I am not sure where they are defined. if you do then let me know.)
I also set up to have my prompt start up in my home folder. (I am not using ‘command prmpt here’ registry tweak. I think if I did I would need to comment this line out since it will just change Dir back to home no matter where you started the prompt from.
Using cygwin tools in cmd.exe
Of course it will work only if you have cygwin installed and your %path% includes a pointer to the folder where cygwin bin so when you execute something from cmd.exe it will be found. (default cygwin tools live in c:\cygwin\bin so I append this path value to my %PATH% environment variable)
After setting up the path value to cygwin you can use pretty much every cygwin tool inside cmd.exe box, even ssh or rsync (I mean cmd.exe is just a dumb terminal that runs smart tools)
So in conclusion I am using an AutoRun registry key to hold the value of initialization bat file where I set prompt, doskey and my default working directory for my cmd.exe and accessing cygwin tools from cmd.exe