Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 XP command prompt.

share|improve this question

7 Answers 7

up vote 73 down vote accepted

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.
share|improve this answer
1  
+1 NICE!!! Haven't seen (or remembered) Doskey for years!... (Playing the memories song in my head!) –  William Hilsum Oct 1 '09 at 0:19
17  
No need for an AutoHotkey script here. Windows provides a way to AutoRun a batch file whenever you launch cmd.exe: 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 Aug 16 '10 at 22:15
    
I'm really surprised no one has suggested PowerShell it's pretty damned good. –  Eddie B Dec 6 '12 at 18:51

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

share|improve this answer

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"
share|improve this answer
    
'color OA' should probably be 'color 0A'. –  csnullptr Jun 2 '11 at 23:29
1  
You only need the "Autorun"="..." line under the [HKEY_...] line, unless you want to explicitly set the other keys too. –  c24w Feb 26 '13 at 11:38

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
share|improve this answer
    
BTW instead of whereis hack you can use where which is a builtin command –  Dheeraj Bhaskar Nov 19 at 12:37

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.

share|improve this answer
    
Nice solution, though a bit overkill for my needs. –  Pops Oct 1 '09 at 14:02
    
Indeed it is. I just like having control over how my stuff works, is all :P –  Phoshi Oct 1 '09 at 15:50

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.

share|improve this answer

You can have almost every linux utility and shell you used inschool, by installing Cygwin, a Linux-like environment for Windows.

However, when working with a new operating system, it's much better to look forward rather than return to the past. Windows is a graphical environment, where graphical applications do almost everything that you used to do via the command line in Linux. Launching system commands is done therefore by mostly using the mouse, rather than the keyboard. "Be prepared" is the word.

share|improve this answer
    
The main problem here is Cygwin is very much a bash emulator - does things differently to cmd. –  Phoshi Sep 30 '09 at 21:07
1  
it's not an emulator dude. it's actually bash. –  quack quixote Oct 2 '09 at 14:45
    
Yes it's actually bash. A lighter and perhaps better alternative if you want to go this route is mingw - Minimalist GNU for Windows. I've had some not insurmountable issues with Cygwin with regards to file permissions. –  GGB667 Sep 11 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.