Aliases in Windows command prompt

后端 未结 16 2496
故里飘歌
故里飘歌 2020-11-22 10:47

I have added notepad++.exe to my Path in Environment variables.

Now in command prompt, notepad++.exe filename.txt opens the filename

相关标签:
16条回答
  • 2020-11-22 11:26

    First, you could create a file named np.cmd and put it in the folder which in PATH search list. Then, edit the np.cmd file as below:

    @echo off
    notepad++.exe
    
    0 讨论(0)
  • 2020-11-22 11:29

    You want to create an alias by simply typing:

    c:\>alias kgs kubectl get svc
    
    Created alias for kgs=kubectl get svc
    

    And use the alias as follows:

    c:\>kgs alfresco-svc
    
    NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
    alfresco-svc   ClusterIP   10.7.249.219   <none>        80/TCP    8d
    

    Just add the following alias.bat file to you path. It simply creates additional batch files in the same directory as itself.

      @echo off
      echo.
      for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
      echo @echo off > C:\Development\alias-script\%1.bat
      echo echo. >> C:\Development\alias-script\%1.bat
      echo %ALL_BUT_FIRST% %%* >> C:\Development\alias-script\%1.bat
      echo Created alias for %1=%ALL_BUT_FIRST%
    

    An example of the batch file this created called kgs.bat is:

    @echo off 
    echo. 
    kubectl get svc %* 
    
    0 讨论(0)
  • 2020-11-22 11:34

    This solution is not an apt one, but serves purpose in some occasions.

    First create a folder and add it to your system path. Go to the executable of whatever program you want to create alias for. Right click and send to Desktop( Create Shortcut). Rename the shortcut to whatever alias name is comfortable. Now, take the shortcut and place in your folder.

    From run prompt you can type the shortcut name directly and you can have the program opened for you. But from command prompt, you need to append .lnk and hit enter, the program will be opened.

    0 讨论(0)
  • 2020-11-22 11:36

    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"
    
    0 讨论(0)
  • 2020-11-22 11:36

    Alternatively you can use cmder which lets you add aliases just like linux:

    alias subl="C:\Program Files\Sublime Text 3\subl.exe" $*
    
    0 讨论(0)
  • 2020-11-22 11:36

    Since you already have notepad++.exe in your path. Create a shortcut in that folder named np and point it to notepad++.exe.

    0 讨论(0)
提交回复
热议问题