Skip to content

Instantly share code, notes, and snippets.

@kanatzidis
Forked from benjamine/alias.cmd
Created January 14, 2017 17:38
Show Gist options
  • Save kanatzidis/4905a1ff711576f4e171d21bf173d465 to your computer and use it in GitHub Desktop.
Save kanatzidis/4905a1ff711576f4e171d21bf173d465 to your computer and use it in GitHub Desktop.
Aliases for windows command line
::
:: 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