-
-
Save gimntut/8fdc5eb9452311702cae24d1721c9b27 to your computer and use it in GitHub Desktop.
Aliases for windows command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: | |
:: Aliases for windows command line | |
:: | |
:: Installation: | |
:: | |
:: - create a folder for your aliases (eg: ```c:\cmd-aliases```) | |
:: - add that folder to your PATH variable | |
:: - save this script as setalias.cmd on that folder | |
:: - run "alias" to see usage | |
:: | |
:: author: Benjamin Eidelman <beneidel@gmail.com> | |
:: | |
@echo off | |
set operation=%1 | |
set aliasname=%2 | |
set aliasfile=%~dp0%2.cmd | |
IF "%~1"=="" GOTO help | |
IF /I "%~1"=="list" GOTO listaliases | |
IF /I "%~1"=="set" GOTO setalias | |
IF /I "%~1"=="get" GOTO getalias | |
IF /I "%~1"=="delete" GOTO deletealias | |
IF /I "%~1"=="here" GOTO setaliashere | |
:help | |
echo. Usage: | |
echo. alias list - list available cmd aliases | |
echo. alias set [name] [command line] - set an alias | |
echo. alias get [name] - show an alias | |
echo. alias delete [name] - delete alias | |
echo. alias here [name] [command line] - create alias cmd on cwd | |
exit /B | |
:listaliases | |
dir /B %~dp0*.cmd | |
exit /B | |
:setaliashere | |
set aliasfile=%2.cmd | |
:setalias | |
if "%aliasname%"=="alias" ( | |
echo ERROR: cannot set this alias | |
exit /B 1 | |
) | |
echo %1 %2> "%aliasfile%" | |
for %%a in ("%aliasfile%") do set /a length=%%~za | |
set /a length=length-1 | |
set commandline=%* | |
setlocal enableDelayedExpansion | |
call set commandline=!commandline:~%length%! | |
set commandline=%commandline% %%* | |
echo %commandline%> "%aliasfile%" | |
echo INFO: alias "%aliasname%" set | |
exit /B | |
:getalias | |
if exist %aliasfile% ( | |
type %aliasfile% | |
) ELSE ( | |
echo ERROR: alias not found | |
exit /B 1 | |
) | |
exit /B | |
:deletealias | |
if /I "%aliasname%"=="alias" ( | |
echo ERROR: cannot delete this alias | |
exit /B 1 | |
) | |
if exist %aliasfile% ( | |
del %aliasfile% | |
echo INFO: alias deleted | |
) ELSE ( | |
echo INFO: alias not found | |
) | |
exit /B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment