21

How can I make command aliases in Windows' Command Prompt like I would with bash?

I found out about doskey in a forum thread, so I can do something like:

doskey ls=dir /b

...and now the command ls acts a little more like ls on Unix. (I type ls so often in cmd, it isn't even funny.)

But how do I get this to stick between sessions? It goes away the next time I open cmd.exe. (Is there something like .bash_profile?)

Kevin Panko
  • 7,466

11 Answers11

42

It is rather easy to setup permanent aliases in the Windows command prompt using the @DOSKEY command and HKCU\Software\Microsoft\Command Processor Autorun option.

Quick step-by-step guide:

  1. Create a new batch file, call it Alias.bat. Copy/paste the text below. TIP: I recommend creating a C:\Bin folder for all your command line tools.
  2. Open the register HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
  3. Add an String Value named Autorun and set the value to absolute path of the Alias.bat file.
  4. Done.

This batch file will execute every time you open a command prompt.

Contents of Alias.bat

DOSKEY ls=DIR $* 
DOSKEY cp=COPY $* 
DOSKEY xcp=XCOPY $*
DOSKEY mv=MOVE $* 
DOSKEY clear=CLS
DOSKEY h=DOSKEY /HISTORY
DOSKEY alias=if ".$*." == ".." ( DOSKEY /MACROS ) else ( DOSKEY $* )

Now you can type alias (i.e DOSKEY /MACROS) to view the current list of aliases/macros.

To add new aliases for the current session only you can use alias name=command.

Dennis
  • 523
11

Also sort of off-topic -

Use PowerShell instead of the cmd.exe command line. The good news is that PowerShell has the equivalent of .bash_profile, and runs just like the cmd.exe command line. It comes with a built-in alias generation feature. The bad news is that there is a bit of a learning curve if you want to do anything more complicated than simple cmd.exe commands.

By the way, ls is defined as an alias of dir, right out of the box.

Kevin Panko
  • 7,466
mkClark
  • 356
3

There is a registry entry at HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun which allows you to run a command when you start a cmd prompt. This includes a batch file.

Phoshi
  • 23,483
1

CMD with aliases/functions from doskey and bashrc (from cygwin>bash), forward slashes also work in cmd (win7 onwards)

  1. Create shorcut to cmd to use cmdrc.cmd

    cmd -a "/k %HOME%\cmdrc.cmd c:\ "

2 C:> cat %HOME%/cmdrc.cmd

:: Override same named builtin windows commands
:: To override these aliases, prefix with '@', eg. @date will not use alias.
@doskey date=c:\cygwin\bin\date.exe $*
@doskey echo=c:\cygwin\bin\echo.exe $*
@doskey mkdir=c:\cygwin\bin\mkdir.exe $*
@doskey rmdir=c:\cygwin\bin\rmdir.exe $*
@doskey find=c:\cygwin\bin\find.exe $*
@doskey time=c:\somedir\time.exe $*

:: using a particular rsync with some options
@doskey rsync=c:\cygwin\bin\rsync.exe -e "ssh -F %HOME%/.ssh/config" $*   

@doskey cp='cp -vi %*'
@doskey ..=cd ..
@doskey ...=cd ../.. 

:: Or use bashrc (bash aliases, bash functions):

:: eg. In bashrc we have "alias cpvi='cp -vi'"

@doskey cpvi=bash -ic "set +u; cpvi $*"  

:: now cpvi is callable from cmd.

:: Install clink_x64 if you want readline editing in cmd
mosh
  • 317
1

I'll be a necromancer for a moment and raise this thread from the dead. It's answer was not satisfactory for me. I knew there was a better way. I dabbled at making and including bat files and trying to figure out how to get the spaghetti ball working well it didn't well. Anyway back to Google I went..

I did find this too How to add new DOS alias/commands and create a keyboard shortcut for an admin DOS It works great, it should work on 98-7 (kinda funny numbering system but hey it's windows). I hope it helps those on this thread and those Google sends this way.

This way is not as simple as Alias, and neither is doskey. Once setup this is about the same effort.

I'll also add that parts of this merged with Dennis Williamson's answer are much better. You can have shell environments for multiple versions of the same application, say php 4 for your old scripts and php 5 for your test server.

Anyhow this worked out much better for me.

wonea
  • 1,877
1

I suggest installing dos versions of these commands directly from this link.

Simple, light, efficient etc. Been using it for a while now, Makes me happy! Cygwin is a bit big for the essentials I need.

gronostaj
  • 58,482
0

Try putting them in autoexec.bat. I'm not sure, you'll have to try it. Put autoexec.bat itself in C:\

0fnt
  • 2,001
0

cygwin can be used in this case although this is not exactly an on-topic answer.

http://www.cygwin.com/ CygWin
Download: http://cygwin.com/setup.exe

To access it easily in windows, you can put c:\cygwin\bin in your path.

note that there are a few command that clashes with windows software which is not equivalent e.g. find(1) vs find.exe -- find(1) lists all files and subdirectories whereas find.exe functions like grep.

another option is to access

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

check out

 cmd.exe /? 
for more details
bubu
  • 10,029
0

Put your aliases into a file called, perhaps, alias.cmd and add /K \path\to\alias.cmd to the shortcut that you use to start cmd.exe.

Reference

0

.cmdrc, the most convenient unix-like experience

If you are looking for a bash-like experience, this is the closest you can get.

  • Loads automatically with every instance of cmd
  • Doesn't require keyword DOSKEY for aliases
    example: ls=ls --color=auto $*
  • Supports arbitrary commands (e.g. custom welcome prompt, etc.)

See this QA for details How to set an alias in Windows Command Line?

cmd greetings & aliases

Qwerty
  • 586
0

Solving it by setting a PATH environment

create ls.c file

enter the following code

int main()
{
system("dir /b");
return 0;
}

into ls.c and compile the file:

$gcc ls.c -o ls

an executable file ls.exe is created

make a directory, name it as cmdline_tools

$mkdir cmdline_tools

copy ls.exe to the directory

set the path of the directory in PATH environment [copy the location of the directory and paste it in the PATH]

C:/cmdline_tools

Open your cmd.exe and type

$ls

It works as dir command. You can do for other commands too in the same way. If not let us know.