Create an alias in Windows XP
Back in school, I used to have a .login file along the lines of
alias ll = ls -l
alias dir = ls -Fhl
alias web = cd ~/public/public_www/development
I'd like to do that sort of thing with my XP box here at work, but most of the resources I've found online seem fairly complicated and heavy-duty. Is there a way to do this that doesn't involve mucking about in the registry or running a large batch file?
My original reason for asking this was that I only need the command line for one command in one specific folder, and I wanted to be able to get to that folder quickly when I launched the command line. But the accepted answer for this question is so good that I decided to ask about my original issue as a separate question: Change to default start folder for Windows command prompt.
Solution 1:
Not many people seem to know about it, but you can use the doskey
built-in macro tool, the only issue is that it doesn't save. There are many ways to work around this though.
usage:
doskey ls=dir
ls
will now do a directory listing just like dir
would.
If you want to use arguments with the commands, use this syntax:
doskey d=dir $*
As for the workaround to make them save:
- save all aliases to a file in this format:
doskey ls=dir doskey ..=cd ..
and place it in one of the directories in your path. Name it something short like a.cmd, so when you open cmd you can type a to load your aliases.
If typing an a and pressing Enter seems too much work, throw this into your AutoHotkey script:
WinWaitActive, C:\WINDOWS\system32\cmd.exe
Send {a}{Enter}
Loading aliases automatically:
You can change all shortcuts to cmd to point to %SystemRoot%\system32\cmd.exe /K C:\path\to\aliases.cmd
, replacing C:\path\to\aliases.cmd
with the location of your aliases file. If you typically run it from the run box, you can:
- Rename the cmd executable to cmd2.exe for example, and replace it with a script or another executable which launches the above command (I wouldn't really recommend this method as a lot of apps depend on cmd)
- Make a batch script and call it cmda (cmd with aliases) for example. Have it launch the above command and put this batch script somewhere in your path.
Solution 2:
It's a simple as:
-
Create a file with aliases, e.g. c:\bin\aliases:
ls=dir /ONE $* cd=cd /d $* python=python -ic "" ps=tasklist $* kill=taskkill /IM $*
-
Create a file with all the stuff you want to run when cmd.exe is started, including loading the aliases with doskey e.g. c:\bin\cmd_autoruns.cmd:
@echo off cls color 0A doskey /macrofile=c:\bin\aliases
-
Create and run once a batch file (e.g. set_cmd_autorun.cmd) which will set the Command Processor
Autorun
key to our cmd_autoruns.cmd:reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d c:\bin\cmd_autoruns.cmd
As an alternative to set_cmd_autorun.cmd it is also possible to instead create a .reg file like the one below and then merge it with a double click:
REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009
"DefaultColor"=dword:00000000
"EnableExtensions"=dword:00000001
"PathCompletionChar"=dword:00000009
"Autorun"="c:\\bin\\cmd_autoruns.cmd"
Solution 3:
My answer is similar to vriolk's
I created a .bat file that contained my macros (e.g. c:\winscripts\autoexec.bat):
@doskey whereis=c:\winscripts\whereis.cmd $* @doskey ls=dir /b $* @doskey l=dir /od/p/q/tw $*
and then from a cmd prompt ran "cmd /?" to find the registry key to edit for the cmd autorun:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun and/or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
using regedit, add the path for your macro batch file to the AutoRun value (add the AutoRun key if it's not there):
c:\winscripts\autoexec.bat
now whenever you run "cmd" from the Start->Run prompt, this autoexec.bat will also run and create the doskey macros for you.
By the way, whereis.cmd contains this:
@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
which searches your PATH variable for the term you provide:
c:>whereis javaw c:\jdk\bin\javaw.exe
Solution 4:
a very quick and dirty way to have a ready shortcut, that doesn't require a lot of fuss - is to create a batch file named after the alias, in one of the directories that are a part of the PATH environment variable. For example, i wanted to invoke Notepad++ through an alias, so i created npp.bat in C:\WINDOWS that contained the following:
"c:\Program Files\Notepad++\notepad++.exe" %1 %2 %3 %4 %5
now npp command can be used from any cmd shell, without autorun files and/or excursions to the registry
Solution 5:
You can create .cmd files and place them someplace in your %PATH% (such as C:\Windows). To use your web alias as an example:
@C:
@cd \inetpub\wwwroot
Would do something like:
M:\> web
C:\inetpub\wwwroot>
I'm not aware of any way to make a flat .aliases style file.