Renya Karasuma
Renya Karasuma

Reputation: 1060

Create alias for a specific command in cmd

Is there anyway to create an alias to cd PATH command in cmd?

For example, instead of typing cd C:\John\Pictures I'd like to type just pictures in cmd and press Enter and it should simply take me to C:\John\Pictures.

Is that possible and how?

Upvotes: 17

Views: 25412

Answers (3)

double-beep
double-beep

Reputation: 5504

You will need to use the doskey command which creates aliases. For example:

doskey note = "C:\Windows\System32\notepad.exe"
note

creates a macro to open Notepad, then calls it. The macro name (note in the above example) must be valid (e.g. no spaces are allowed, may begin with an underscore).

You can also use parameters:

doskey note = "C:\Windows\System32\notepad.exe" $1
note "C:\Users\....\textfile.txt"

By default, doskey macros are only saved for the current session. You can work around this limitation in two ways:

  1. Save the macros in a text file, then load them each time you need them:

    A command like:

    doskey /macros > %TEMP%\doskey-macros.txt
    

    Will save all the current macro definitions into a text file.


    Then to restore all the macros at a later date:

    doskey /macrofile=%TEMP%\doskey-macros.txt
    
  2. After saving the macros in a text file as shown above, instead of loading them every time, run:

    reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"%TEMP%\doskey-macros.txt\"" /f
    

    so that the macros are set each time you open the cmd.

    See this SuperUser answer for more information.


Note: You cannot use command aliases in a batch file.

Upvotes: 26

studio1057
studio1057

Reputation: 157

Easiest way:
Open Notepad in admin mode
go to C:\Windows\System32
New file
Contents: dir %1
Save it here as ls.bat
This will give you ls

Same way, git status %1 saved as gs.bat would give you gs instead of having to type out git status all the time. In the newer releases of windows there are many different ways to do this and they are (hopefully) getting simpler and easier, but this method will take care of commands you would type lots of times every day. Just make sure to keep a backup of the files you've created, somewhere outside System32, because some windows updates will wipe them out.

Upvotes: 1

rbento
rbento

Reputation: 11608

Here is an alternative for Windows 10:

1. Create a file called init.cmd and save it to your user folder

C:\Users\YourName\init.cmd

@echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey jp=cd C:\John\Pictures
doskey l=dir /a $*

2. Register it to be applied automatically whenever cmd.exe is executed

In the Command Prompt, run:

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f

3. Restart the Command Prompt

Now close/open the Command Prompt and the aliases will be available.

To unregister, run:

reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun

Upvotes: 21

Related Questions