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.
9 Answers
As Christian.K said in his comment, the DOSKEY command can be used to define macros, which are analogous to aliases.
doskey macroName=macroDefinitionMacro 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|someMacrofail. - 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% 8 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_pathNow you can type quickcd at the command line.
It can also be called inside a script using the call function
call quickcd 4 subst
If you're really trying to get around something like this:
C:> cd \users\myLoginID\documents\clients\MTV\strategy\roadmap\deliverable\finalYou 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\finalThen, 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 1 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.
0Unlike Linux, in Windows aliases can take arguments, and can be given parameters to assign those arguments into in their definition.
The internal built-in command is doskey, meaning it's not an external executable file located in %SystemRoot%\System32 but is a feature of cmd.exe which itself is located in %SystemRoot% or %Windir% both pointing at C:\Windows folder by default.
Doskey defines aliases among many other things. In Windows terminology, aliases is called macros. To set an alias you use
doskey alias=command $1 $2 ... $9 or $* (to catch them all) Here $1 points to the first argument, and $* points to the all arguments.
I'd always use $* and double quote $1 if there's only one argument to omit spaces which are treated as special characters, namely as separators (for commands).
Moreover, you can define your aliases macros in a separate file, say LinuxAliases.macro and then source it using
doskey /macrofile=LinuxAliases.macroIt will load all the aliases defined inside the file for the current session only, meaning when you exit out of cmd.exe process (also known as the shell and Command Prompt), they're gone.
To have permanent aliases you can type the full command somewhere in a regedit key called either AutoRun or AutoCommand or something similar, what I cannot recall now alongside the macro file's internal syntax :P
Here are the official, and even better documentations for the command.
You can use doskey.exe, here is an example:
doskey qcd=cd [pathname]And now if you type qcd then it will cd to that path.
Here is some help with doskey macro's:
doskey [macroname]=[command]That is the simplest form, here is an example:
doskey word=echo This is a really, really long sentence.and if you type word the output is:
This is a really, really long sentence.
You can use $1-$9 for parameters, here is an example:
doskey npd=notepad $1and if you type npd it will open notepad but if we type npd test.txt it will open test.txt in notepad.
You can use $* for all parameters, here is another example:
doskey prt=echo $*and if you type prt This is short. then the output will be:
This is short.
You can use $T for multiple commands, here is an example:
doskey newline=echo $1 $T echo $2and if you type newline Hello and then the output will be:
Hello
and
I hope you understand.
I've made an "alias" like that specifically for changing directory. Basically I created a file named fav.bat and put it into a folder in PATH (e.g C:\MyBatches):
@setlocal
@echo off
set userChoice=%1
rem The format is:
rem call :condition SHORTCUT FOLDER
call :condition web c:\Git\SecThor\SecWeb\App\
call :condition cloud c:\Git\SecThor\SecCloud\dojo.backend\
call :condition good c:\Users\Vitaly\Dropbox\Projects\goodread-friends\
endlocal
popd
GOTO :eof
:condition
rem We do PUSHD twice since endlocal does popd once :|
if "%userChoice%"=="%1" pushd %2 & pushd %2 & echo pushd %2
if "%userChoice%"=="" echo %1 =^> %2
GOTO :eofThen I can jump to any predefined folder very quickly. I wrote a full article about that technique here.
This is an old question, but was still #2 result when googling this issue before I found a fix so I'll share my solution.
- Create a folder for your scripts
C:\shell-scripts - Add created folder to
PATH - Inside that folder, create a
alias.cmdand/oralias.ps1(depending on if you use CMD or PowerShell) - Contents of file:
pushd "C:\some\path\here"
- Make sure you close and re-open your terminal to load PATH
- Run
alias
Note on my machine I was able to make both a .cmd and a .ps1 file with the same name without conflicts so I can use the alias in either PowerShell or CMD.
assuming you remain on the same drive, i.e. no D: then cd D:\somedir\ needed.
In my case, C: holds system files, but all work is on D:
2 parts. first set some working directories, using a batch file.
let's say localprofile.cmd. You can have multiple ones of these, just run them as needed.
set wd1=D:\home\work\fb440.dumper
set wd2=D:\home\work\py\testsnow use another command file to move around, based on those environment variables you just set.
wd.cmd
echo off
d:
if %1.==. set | findstr wd
if %1==1 cd %wd1%
if %1==2 cd %wd2%
if %1==3 cd %wd3%
if %1==4 cd %wd4%
if %1==5 cd %wd5%
if %1==6 cd %wd6%And a bit of a sample use:
D:\home\bin>wd 2
D:\home\bin>echo off
D:\home\work\py\tests>wd 1
D:\home\work\py\tests>echo offwd by itself is supposed to show me the list of bookmarked directories. It works, but not sure why I am getting this "unexpected cd" message.
D:\home\work\fb440.dumper>wd
D:\home\work\fb440.dumper>echo off
wd1=D:\home\work\fb440.dumper
wd2=D:\home\work\py\tests
cd was unexpected at this time.
D:\home\work\fb440.dumper> Regular .cmd batches
Aside from directory navigation, you can create a foo.cmd somewhere on your PATH and it can act much as an alias. Remember, unlike Linux/OSX shells, cmd files can affect environment variables and the session, without needing to source them.
I have a cdbin.cmd to navigate me, for example:
d:
cd \home\binAnd this is a pseudo-grep grep.cmd
findstr -n %1 %2in action (whatever sort /+2 /+10 means)
D:\home\work\fb440.dumper>grep class *.py | sort /+2 /+10
dumper2.py:18:class PrettySafeLoader(yaml.SafeLoader):
dumper2.py:27:class Comparator(object):
dumper2.py:3:class module_settings:
linker2.py:5:class module_settings:
dumper2.py:65:class DataCompProcessor(object):
dumper2.py:69:class DataCompTextRenderer(DataCompProcessor):
dumper2.py:76:class DataComparator(object):