Aliases in Windows command prompt
To add to josh's answer,
you may make the alias(es) persistent with the following steps,
Create a .bat or .cmd file with your
DOSKEY
commands.Run regedit and go to
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Add String Value entry with the name
AutoRun
and the full path of your .bat/.cmd file.For example,
%USERPROFILE%\alias.cmd
, replacing the initial segment of the path with%USERPROFILE%
is useful for syncing among multiple machines.
This way, every time cmd is run, the aliases are loaded.
For Windows 10, add the entry to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
instead.
For completeness, here is a template to illustrate the kind of aliases one may find useful.
@echo off
:: Temporary system path at cmd startup
set PATH=%PATH%;"C:\Program Files\Sublime Text 2\"
:: Add to path by command
DOSKEY add_python26=set PATH=%PATH%;"C:\Python26\"
DOSKEY add_python33=set PATH=%PATH%;"C:\Python33\"
:: Commands
DOSKEY ls=dir /B $*
DOSKEY sublime=sublime_text $*
::sublime_text.exe is name of the executable. By adding a temporary entry to system path, we don't have to write the whole directory anymore.
DOSKEY gsp="C:\Program Files (x86)\Sketchpad5\GSP505en.exe"
DOSKEY alias=notepad %USERPROFILE%\Dropbox\alias.cmd
:: Common directories
DOSKEY dropbox=cd "%USERPROFILE%\Dropbox\$*"
DOSKEY research=cd %USERPROFILE%\Dropbox\Research\
- Note that the
$*
syntax works after a directory string as well as an executable which takes in arguments. So in the above example, the user-defined commanddropbox research
points to the same directory asresearch
. - As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the
alias.cmd
file. Seealias
above. If you are in a cmd session, entercmd
to restart cmd and reload thealias.cmd
file.
When I searched the internet for an answer to the question, somehow the discussions were either focused on persistence only or on some usage of DOSKEY only. I hope someone will benefit from these two aspects being together here!
Here's a .reg
file to help you install the alias.cmd
. It's set now as an example to a dropbox folder as suggested above.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%USERPROFILE%\\alias.cmd"
For single-user applications, the above will do. Nevertheless, there are situations where it is necessary to check whether alias.cmd
exists first in the registry key. See example below.
In a C:\Users\Public\init.cmd
file hosting potentially cross-user configurations:
@ECHO OFF
REM Add other configurations as needed
IF EXIST "%USERPROFILE%\alias.cmd" ( CALL "%USERPROFILE%\alias.cmd" )
The registry key should be updated correspondly to C:\Users\Public\init.cmd
or, using the .reg
file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="C:\\Users\\Public\\init.cmd"
If you're just going for some simple commands, you could follow these steps:
- Create a folder called C:\Aliases
- Add C:\Aliases to your path (so any files in it will be found every time)
- Create a .bat file in C:\Aliases for each of the aliases you want
Maybe overkill, but unlike the (otherwise excellent) answer from @Argyll, this solves the problem of this loading every time.
For instance, I have a file called dig2.bat with the following in it:
@echo off
echo.
dig +noall +answer %1
Your np file would just have the following:
@echo off
echo.
notepad++.exe %1
Then just add the C:\Aliases folder to your PATH environment variable. If you have CMD or PowerShell already opened you will need to restart it.
FWIW, I have about 20 aliases (separate .bat files) in my C:\Aliases directory - I just create new ones as necessary. Maybe not the neatest, but it works fine.
UPDATE: Per an excellent suggestion from user @Mav, it's even better to use %* rather than %1, so you can pass multiple files to the command, e.g.:
@echo off
echo.
notepad++.exe %*
That way, you could do this:
np c:\temp\abc.txt c:\temp\def.txt c:\temp\ghi.txt
and it will open all 3 files.
You need to pass the parameters, try this:
doskey np=notepad++.exe $*
Edit (responding to Romonov's comment) Q: Is there any way I can make the command prompt remember so I don't have to run this each time I open a new command prompt?
doskey
is a textual command that is interpreted by the command processor (e.g. cmd.exe), it can't know to modify state in some other process (especially one that hasn't started yet).
People that use doskey
to setup their initial command shell environments typically use the /K
option (often via a shortcut) to run a batch file which does all the common setup (like- set window's title, colors, etc).
cmd.exe /K env.cmd
env.cmd:
title "Foo Bar"
doskey np=notepad++.exe $*
...
Alternatively you can use cmder
which lets you add aliases just like linux:
alias subl="C:\Program Files\Sublime Text 3\subl.exe" $*