Created
August 16, 2024 11:17
-
-
Save bdavidxyz/798f69c7f56c474595e049387c605272 to your computer and use it in GitHub Desktop.
This file contains hidden or 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. Heavily inspired by https://gist.github.com/benjamine/5992592 | |
:: | |
:: Installation: | |
:: | |
:: - create a folder for your aliases (eg: ```c:\cmd-aliases```) | |
:: - add that folder to your PATH variable | |
:: - save this script as alias.cmd on that folder | |
:: - run "alias" to see usage | |
:: | |
:: author: David Boureau <[email protected]> | |
:: | |
@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 | |
for %%f in (%~dp0*.cmd) do ( | |
if not "%%~nf"=="alias" ( | |
echo. | |
echo ---- %%~nf ---- | |
type "%%f" | |
) | |
) | |
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