Aliases in Windows command prompt

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

提交回复
热议问题