Create an alias in Windows XP

55

54

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.

Pops

Posted 2009-09-30T18:45:58.840

Reputation: 7 353

A quick&dirty option is to just add the folder to the PATH variable. Right-click My Computer, choose Properties, go to Advanced, then Environment Variables. More info: stackoverflow.com/a/20773224/722036

– aexl – 2016-09-10T09:56:25.380

Answers

84

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.

John T

Posted 2009-09-30T18:45:58.840

Reputation: 149 037

Does the defined macros work from the Win+R (Run) box ??? – ZEE – 2017-01-05T21:28:36.523

19

No need for an AutoHotkey script here. Windows provides a way to AutoRun a batch file whenever you launch cmd.exe: http://technet.microsoft.com/en-us/library/cc779439(WS.10).aspx I configure it to point to c:\dev\autorun.bat which loads doskey macros and runs other convenient utilities.

– Dan Fabulich – 2010-08-16T22:15:12.670

1+1 NICE!!! Haven't seen (or remembered) Doskey for years!... (Playing the memories song in my head!) – William Hilsum – 2009-10-01T00:19:08.420

I'm really surprised no one has suggested PowerShell it's pretty damned good. – Eddie B – 2012-12-06T18:51:03.537

39

It's a simple as:

  1. Create a file with aliases, e.g. c:\bin\aliases:

    ls=dir /ONE $*
    cd=cd /d $*
    python=python -ic "" 
    ps=tasklist $*
    kill=taskkill /IM $*
    
  2. 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
    
  3. 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"

user29888

Posted 2009-09-30T18:45:58.840

Reputation: 391

My path had spaces and having trouble in adding to the registry. I used following: `Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor] "Autorun"=""C:\Path has spaces\foldername\cmd_autoruns.cmd""` – Neeraj Gulia – 2020-01-01T06:48:39.550

'color OA' should probably be 'color 0A'. – csnullptr – 2011-06-02T23:29:26.407

1You only need the "Autorun"="..." line under the [HKEY_...] line, unless you want to explicitly set the other keys too. – c24w – 2013-02-26T11:38:59.247

5

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

andematt

Posted 2009-09-30T18:45:58.840

Reputation: 151

BTW instead of whereis hack you can use where which is a builtin command – Dheeraj Bhaskar – 2014-11-19T12:37:08.673

2

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

hello_earth

Posted 2009-09-30T18:45:58.840

Reputation: 153

1

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.

djhowell

Posted 2009-09-30T18:45:58.840

Reputation: 3 535

0

The way I did it was with a quick python script:

import sys
import string
import os
import glob

def listAll():
        for infile in glob.glob("c:\\aliases\\*.bat"):
            fileName = infile
            fileName = fileName[len("c:\\aliases\\"):len(fileName)-4]
            fileContents = open("c:\\aliases\\" + fileName + ".bat", "r")
            fileContents.readline()
            fileContentString=fileContents.readline()
            fileName += " is aliased to "
            fileName += fileContentString[0:len(fileContentString)-3]
            print fileName

def listSome(which):
        for infile in glob.glob("c:\\aliases\\*.bat"):
            fileName = infile
            fileName = fileName[len("c:\\aliases\\"):len(fileName)-4]
            fileContents = open("c:\\aliases\\" + fileName + ".bat", "r")
            fileContents.readline()
            fileContentString=fileContents.readline()
            if fileName.find(which)==0:
                fileName += " is aliased to "
                fileName += fileContentString[0:len(fileContentString)-3]
                print fileName

if len(sys.argv)>1:
    if sys.argv[1]!="-p":
        file = open("c:\\aliases\\"+sys.argv[1]+".bat", "w")
        file.write("@ECHO OFF\n")
        counter=0
        totalInput=""
        counter=0
        for arg in sys.argv:
            if counter > 1:
                totalInput+= arg + " "
            counter+=1

        if totalInput.find(".exe")!=-1:
            file.write("\"")

        counter=0

        for arg in sys.argv:
            if counter > 1:
                file.write(arg)
                if sys.argv[1]==sys.argv[2]:
                    if counter==2:
                        file.write(".exe")
                temparg=str(arg)
                if temparg.find(".exe")!=-1:
                    file.write("\"")
                file.write(" ")
            counter+=1
        file.write("%*")

        print "Aliased " + sys.argv[1] + " to " + totalInput
    else:
        if len(sys.argv)>2:
            listSome(sys.argv[2])
        else:
            listAll()
else:
    listAll()

Apologies for the poor scripting, but the usage is quite nice, imo. Place it somewhere in your path, add .py to your PATHEXT, and add c:\aliases to your PATH too (or change it, whatever suits), then use:

alias <command> <action>

to alias (Yep, no =, though it wouldn't be hard to add a .split in there), and:

alias -p <command or part of>

To display what something is.

Hackish, but stupidly useful. There's an equivalent unalias script, but I'm sure you can work that one out.

edit: This obviously requires python, written on v26 but will probably work in anything recent. As before, sorry for the quality :)

edit2: Actually, something like this but to add to the doskey stuff would be better. You can add startup commands to cmd with the autorun registry key, too, so that could be much cleaner.

Phoshi

Posted 2009-09-30T18:45:58.840

Reputation: 22 001

1Nice solution, though a bit overkill for my needs. – Pops – 2009-10-01T14:02:27.247

Indeed it is. I just like having control over how my stuff works, is all :P – Phoshi – 2009-10-01T15:50:00.290