# How to set an alias in Windows Command Line?

I used to work on bash and benefit a lot from alias. Is there any equivalent way in Windows Command Line?

I don't want to simulate a Linux environment, so cygwin is not a choice. I just need some shortcut for some very long command, like cd a_very_long_path.

-

## migrated from stackoverflow.comMar 4 '13 at 9:53

This question came from our site for professional and enthusiast programmers.

You would use doskey (don't be fooled by the name it is not the old MS DOS doskey). Use doskey /? for more information. Other than that, I'm afraid, this question is off topic here. –  Christian.K Mar 4 '13 at 7:42
Great comment, thanks. @Christian.K –  StarPinkER Mar 4 '13 at 7:44
Possible duplicate of Is there a command to change path from current location to default directory, in Windows command prompt? Specifically, see my answer to know how you can have your DOSKey macros be auto-loaded when the Command Prompt is opened. –  Karan Mar 4 '13 at 18:22
related + a way to make doskey permanent superuser.com/questions/49170/… –  laggingreflex Aug 4 '14 at 5:52

As Christian.K said in his comment, the DOSKEY command can be used to define macros, which are analogous to aliases.

doskey macroName=macroDefinition


Macro parameters are referenced in the definition via $ prefixed positions: $1 through $9 and $* for all.

But there are serious limitations with DOSKEY macros:

• The macros only work on the interactive command line - they do not work within a batch script.
• They cannot be used on either side of a pipe: Both someMacro|findstr '^' and dir|someMacro fail.
• They cannot be used within a FOR /F commands: for /f %A in ('someMacro') do ... fails

The limitations are so severe that I rarely use DOSKEY macros.

Obviously you can create batch scripts instead of macros, and make sure the script locations are in your PATH. But then you must prefix each script with CALL if you want to use the script within another script.

You could create simple variable "macros" for long and oft used commands, but syntax is a bit awkward to type, since you need to expand the "macro" when you want to use it.

Definition:

set "cdMe=cd a_very_long_path"


Usage (from command line or script)

%cdMe%

-
Thanks, then is there any way to work within a batch script? –  StarPinkER Mar 4 '13 at 12:19
@JermaineXu - No way with DOSKEY macros. See my updated answer for crude alternatives. –  dbenham Mar 4 '13 at 12:31
The pro of the second solution is that, piping will work. The first solution with doskey will not work when you want to use the macro in piping. –  Unhandled Exception Dec 20 '14 at 8:39
@UnhandledException - You are right! I never thought to test that since lack of batch support was enough for me to avoid DOSKEY macros. They also cannot be used as a command within FOR /F IN() clause. –  dbenham Dec 20 '14 at 13:01
@UnhandledException - I've updated my answer to include your info. Thanks. –  dbenham Dec 20 '14 at 13:12

SUBST

If you're really trying to get around something like this:

you can use the subst command to map that long path to a separate drive letter

Then, when you want to jump into that folder, you can just type m: at the command line.

The advantage of this over doskey is that it works in all batch programs, and shows up in any file dialog box within Windows.

If you don't want the mapping any more:

subst m: /D

-
If I could vote you more than one...:) perfect solution... –  Anindya Chatterjee Apr 5 '14 at 21:17
I also like this but it DOES eat up drive letters. –  GGB667 Sep 11 '14 at 21:10

You can make a batch script and save it into your path.

On Linux you would make a script and add it to the folder ~/bin on windows you can do the same.

Add %USERPROFILE%\bin to your PATH environment variable. Then save your scripts in there.

quickcd.cmd

@echo off
cd a_very_long_path


Now you can type quickcd at the command line.

It can also be called inside a script using the call function

call quickcd

-

You could use the same trick, that windows uses: set an environment-variable (or just a variable in a batch-context) for example there is an environment-variable %windir% (and some others) So you can do an

cd C:\Windows\


or

cd %windir%


which does the same. So all, you have to do is:

set "mydir=C:\very\long\path\to\my\data\"


after that you can do (from whereever you are):

dir %mydir%


or

cd %mydir%


or whatever you want.

-
That is the ticket @stephan ! –  jbchurchill Sep 23 '14 at 13:40