Aliases in Windows command prompt
I have added
notepad++.exe to my Path in Environment variables.
Now in command prompt,
notepad++.exe filename.txt opens the
filename.txt. But I want to do just
np filename.txt to open the file.
I tried using
DOSKEY np=notepad++. But it is just bringing to the forefront an already opened notepad++ without opening the file. How can I make it open the file?
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
- Run regedit and go to
Add String Value entry with the name
AutoRunand the full path of your .bat/.cmd file.
%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 command
dropbox researchpoints to the same directory as
- As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the
aliasabove. If you are in a cmd session, enter
cmdto restart cmd and reload the
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!
.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.
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
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Command Processor] "AutoRun"="C:\\Users\\Public\\init.cmd"
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
title "Foo Bar" doskey np=notepad++.exe $* ...
Read more... Read less...
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.
Given that you added notepad++.exe to your PATH variable, it's extra simple.
Create a file in your System32 folder called
np.bat with the following code:
@echo off call notepad++.exe %*
%* passes along all arguments you give the
np command to the
EDIT: You will need admin access to save files to the System32 folder, which was a bit wonky for me. I just created the file somewhere else and moved it to System32 manually.
Also, you can create an alias.cmd in your path (for example C:\Windows) with the command
@echo %2 %3 %4 %5 %6 > %windir%\%1.cmd
Once you do that, you can do something like this:
alias nameOfYourAlias commands to run
And after that you can type in comman line
this will execute
commands to run
BUT the best way for me is just adding the path of a programm.
setx PATH "%PATH%;%ProgramFiles%\Sublime Text 3" /M
And now I run sublime as
Console Aliases in Windows 10
To define a console alias, use
Doskey.exe to create a macro, or use the
doskey test=cd \a_very_long_path\test
To also pass parameters add
$* at the end:
doskey short=longname $*
AddConsoleAlias( TEXT("test"), TEXT("cd \\<a_very_long_path>\\test"), TEXT("cmd.exe"));