Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Thanks.

like image 907
Romonov Avatar asked Dec 11 '13 22:12

Romonov


People also ask

What is alias in CMD?

An alias lets you create a shortcut name for a command, file name, or any shell text. By using aliases, you save a lot of time when doing tasks you do frequently. You can create a command alias.

Does Windows have an alias command?

In Windows, you can create aliases using the DOS command: doskey, which is useful for creating macros. You can read more about the doskey command here. Similar to the alias command, the doskey command stays in effect only during the session.

What is the command to list aliases?

Linux Alias Syntax[option] : Allows the command to list all current aliases. [name] : Defines the new shortcut that references a command. A name is a user-defined string, excluding special characters and 'alias' and 'unalias', which cannot be used as names.


2 Answers

To add to josh's answer,

you may make the alias(es) persistent with the following steps,

  1. Create a .bat or .cmd file with your DOSKEY commands.
  2. Run regedit and go to HKEY_CURRENT_USER\Software\Microsoft\Command Processor
  3. 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 command dropbox research points to the same directory as research.
  • As Rivenfall pointed out, it is a good idea to include a command that allows for convenient editing of the alias.cmd file. See alias above. If you are in a cmd session, enter cmd to restart cmd and reload the alias.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" 
like image 119
Argyll Avatar answered Oct 02 '22 08:10

Argyll


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 $* ... 
like image 34
josh poley Avatar answered Oct 02 '22 09:10

josh poley