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
.
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.
See the doskey technet documentation, or type doskey /?
or help doskey
from the command line for more information.
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 '^'
anddir|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%
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 /d 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
subst
If you're really trying to get around something like this:
C:> cd \users\myLoginID\documents\clients\MTV\strategy\roadmap\deliverable\final
You can use the subst
command to map that long path to a separate drive letter
subst m: c:\users\myLoginID\documents\clients\MTV\strategy\roadmap\deliverable\final
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
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.