Windows 10,将条目添加到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor。
为了完整起见,这里有一个模板来说明可能有用的别名类型。
@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\
@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
c:\>alias kgs kubectl get svc
Created alias for kgs=kubectl get svc
并使用别名如下:
c:\>kgs alfresco-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alfresco-svc ClusterIP 10.7.249.219 <none> 80/TCP 8d
只需将下面的alias.bat文件添加到您的路径。它只是在与自身相同的目录中创建额外的批处理文件。
@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%
@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]
Then, create aliases wherever you need them using the following format:
alias1 = command1
alias2 = command2
...
例如:
b = nmake
c = nmake clean
r = nmake rebuild
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.
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.
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你不需要这个文件,如果你决定b)以上
:: 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
cls
echo Hi Qwerty, how are you today?
@echo off
set path=D:\website_development\laragon\bin\php\php-8.1.2-Win32-vs16-x64/php8 D:\website_development\laragon\bin\composer/composer.phar
set args=%*
set command=%path% %args%
%command%