Aliases in Windows command prompt

后端 未结 16 2495
故里飘歌
故里飘歌 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:18

    Console Aliases in Windows 10

    To define a console alias, use Doskey.exe to create a macro, or use the AddConsoleAlias function.

    doskey

    doskey test=cd \a_very_long_path\test
    

    To also pass parameters add $* at the end: doskey short=longname $*

    AddConsoleAlias

    AddConsoleAlias( TEXT("test"), 
                     TEXT("cd \\<a_very_long_path>\\test"), 
                     TEXT("cmd.exe"));
    

    More information here Console Aliases, Doskey, Parameters

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

    If you'd like to enable aliases on per-directory/per-project basis, try the following:

    1. First, create a batch file that will look for a file named aliases in the current directory and initialize aliases from it, let’s call it make-aliases.cmd

      @echo off
      if not exist aliases goto:eof
      echo [Loading aliases...]
      for /f "tokens=1* delims=^=" %%i in (aliases) do (
         echo   %%i ^<^=^> %%j
         doskey %%i=%%j
      )
      doskey aliases=doskey /macros
      echo   --------------------
      echo   aliases ^=^> list  all
      echo   alt+F10 ^=^> clear all
      echo [Done]
      
    2. Then, create aliases wherever you need them using the following format:

      alias1 = command1
      alias2 = command2
      ...
      

      for example:

      b = nmake
      c = nmake clean
      r = nmake rebuild
      
    3. Then, add the location of make-aliases.cmd to your %PATH% variable to make it system-wide or just keep it in a known place.

    4. Make it start automatically with cmd.

      • I would definitely advise against using HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun for this, because some development tools would trigger the autorun script multiple times per session.

      • If you use ConEmu you could go another way and start the script from the startup task (Settings > Startup > Tasks), for example, I created an entry called {MSVC}:

        cmd.exe /k "vcvars64 && make-aliases",

        and then registered it in Explorer context menu via Settings > Integration> with Command: {MSVC} -cur_console:n, so that now I can right-click a folder and launch a VS developer prompt inside it with my aliases loaded automatically, if they happen to be in that folder.

        Without ConEmu, you may just want to create a shortcut to cmd.exe with the corresponding command or simply run make-aliases manually every time.

    Should you happen to forget your aliases, use the aliases macro, and if anything goes wrong, just reset the current session by pressing Alt+F10, which is a built-in command in cmd.

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

    Expanding on roryhewitt answer.

    An advantage to using .cmd files over DOSKEY is that these "aliases" are then available in other shells such as PowerShell or WSL (Windows subsystem for Linux).

    The only gotcha with using these commands in bash is that it may take a bit more setup since you might need to do some path manipulation before calling your "alias".

    eg I have vs.cmd which is my "alias" for editing a file in Visual Studio

    @echo off
    if [%1]==[] goto nofiles
    start "" "c:\Program Files (x86)\Microsoft Visual Studio 
    11.0\Common7\IDE\devenv.exe" /edit %1
    goto end
    :nofiles
    start "" "C:\Program Files (x86)\Microsoft Visual Studio 
    11.0\Common7\IDE\devenv.exe" "[PATH TO MY NORMAL SLN]"
    :end
    

    Which fires up VS (in this case VS2012 - but adjust to taste) using my "normal" project with no file given but when given a file will attempt to attach to a running VS opening that file "within that project" rather than starting a new instance of VS.

    For using this from bash I then add an extra level of indirection since "vs Myfile" wouldn't always work

    alias vs='/usr/bin/run_visual_studio.sh'
    

    Which adjusts the paths before calling the vs.cmd

    #!/bin/bash
    cmd.exe /C 'c:\Windows\System32\vs.cmd' "`wslpath.sh -w -r $1`"
    

    So this way I can just do

    vs SomeFile.txt
    

    In either a command prompt, Power Shell or bash and it opens in my running Visual Studio for editing (which just saves my poor brain from having to deal with VI commands or some such when I've just been editing in VS for hours).

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

    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 $*
    ...
    
    0 讨论(0)
  • 2020-11-22 11:22

    Naturally, I would not rest until I have the most convenient solution of all. Combining the very many answers and topics on the vast internet, here is what you can have.

    • Loads automatically with every instance of cmd
    • Doesn't require keyword DOSKEY for aliases
      example: ls=ls --color=auto $*

    Note that this is largely based on Argyll's answer and comments, definitely read it to understand the concepts.

    How to make it work?

    1. Create a mac macro file with the aliases
      you can even use a bat/cmd file to also run other stuff (similar to .bashrc in linux)
    2. Register it in Registry to run on each instance of cmd
        or run it via shortcut only if you want

    Example steps:

    %userprofile%/cmd/aliases.mac
    ;==============================================================================
    ;= This file is registered via registry to auto load with each instance of cmd.
    ;================================ general info ================================
    ;= https://stackoverflow.com/a/59978163/985454  -  how to set it up?
    ;= https://gist.github.com/postcog/5c8c13f7f66330b493b8  -  example doskey macrofile
    ;========================= loading with cmd shortcut ==========================
    ;= create a shortcut with the following target :
    ;= %comspec% /k "(doskey /macrofile=%userprofile%\cmd\aliases.mac)"
    
    alias=subl %USERPROFILE%\cmd\aliases.mac
    hosts=runas /noprofile /savecred /user:QWERTY-XPS9370\administrator "subl C:\Windows\System32\drivers\etc\hosts" > NUL
    
    p=@echo "~~ powercfg -devicequery wake_armed ~~" && powercfg -devicequery wake_armed && @echo "~~ powercfg -requests ~~ " && powercfg -requests && @echo "~~ powercfg -waketimers ~~"p && powercfg -waketimers
    
    ls=ls --color=auto $*
    ll=ls -l --color=auto $*
    la=ls -la --color=auto $*
    grep=grep --color $*
    
    ~=cd %USERPROFILE%
    cdr=cd C:\repos
    cde=cd C:\repos\esquire
    cdd=cd C:\repos\dixons
    cds=cd C:\repos\stekkie
    cdu=cd C:\repos\uplus
    cduo=cd C:\repos\uplus\oxbridge-fe
    cdus=cd C:\repos\uplus\stratus
    
    npx=npx --no-install $*
    npxi=npx $*
    npr=npm run $*
    
    now=vercel $*
    
    
    ;=only in bash
    ;=alias whereget='_whereget() { A=$1; B=$2; shift 2; eval \"$(where $B | head -$A | tail -1)\" $@; }; _whereget'
    
    history=doskey /history
    ;= h [SHOW | SAVE | TSAVE ]
    h=IF ".$*." == ".." (echo "usage: h [ SHOW | SAVE | TSAVE ]" && doskey/history) ELSE (IF /I "$1" == "SAVE" (doskey/history $g$g %USERPROFILE%\cmd\history.log & ECHO Command history saved) ELSE (IF /I "$1" == "TSAVE" (echo **** %date% %time% **** >> %USERPROFILE%\cmd\history.log & doskey/history $g$g %USERPROFILE%\cmd\history.log & ECHO Command history saved) ELSE (IF /I "$1" == "SHOW" (type %USERPROFILE%\cmd\history.log) ELSE (doskey/history))))
    loghistory=doskey /history >> %USERPROFILE%\cmd\history.log
    
    ;=exit=echo **** %date% %time% **** >> %USERPROFILE%\cmd\history.log & doskey/history $g$g %USERPROFILE%\cmd\history.log & ECHO Command history saved, exiting & timeout 1 & exit $*
    exit=echo **** %date% %time% **** >> %USERPROFILE%\cmd\history.log & doskey/history $g$g %USERPROFILE%\cmd\history.log & exit $*
    
    ;============================= :end ============================
    ;= rem ******************************************************************
    ;= rem * EOF - Don't remove the following line.  It clears out the ';'
    ;= rem * macro. We're using it because there is no support for comments
    ;= rem * in a DOSKEY macro file.
    ;= rem ******************************************************************
    ;=
    

    Now you have three options:

    • a) load manually with shortcut

      create a shortcut to cmd.exe with the following target :
      %comspec% /k "(doskey /macrofile=%userprofile%\cmd\aliases.mac)"

    • b) register just the aliases.mac macrofile

    • c) register a regular cmd/bat file to also run arbitrary commands
      see example cmdrc.cmd file at the bottom

    note: Below, Autorun_ is just a placeholder key which will not do anything. Pick one and rename the other.

    Manually edit registry at this path:

    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
      Autorun    REG_SZ    doskey /macrofile=%userprofile%\cmd\aliases.mac
      Autorun_    REG_SZ    %USERPROFILE%\cmd\cmdrc.cmd
    

    Or import reg file:

    %userprofile%/cmd/cmd-aliases.reg
    Windows Registry Editor Version 5.00
    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
    "Autorun"="doskey /macrofile=%userprofile%\\cmd\\aliases.mac"
    "Autorun_"="%USERPROFILE%\\cmd\\cmdrc.cmd"
    
    %userprofile%/cmd/cmdrc.cmd you don't need this file if you decided for b) above
    :: This file is registered via registry to auto load with each instance of cmd.
    :: https://stackoverflow.com/a/59978163/985454
    
    @echo off
    doskey /macrofile=%userprofile%\cmd\aliases.mac
    
    :: put other commands here
    
    0 讨论(0)
  • 2020-11-22 11:25

    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 %*
    

    The %* passes along all arguments you give the np command to the notepad++.exe command.

    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.

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