Skip to content

Instantly share code, notes, and snippets.

@benjamine
Last active November 9, 2023 20:35
  • Star 23 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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
@VVelda
Copy link

VVelda commented Nov 15, 2014

Thank you for snippet. :-) I use it with only one change on line 60 to
echo @echo off> "%aliasfile%"
echo %commandline%>> "%aliasfile%"
because this suppress inessential echo from alias :-)

@rillig
Copy link

rillig commented Oct 29, 2016

This program does not work when installed into a directory whose name contains spaces. I installed it into %USERPROFILE%\Program Files\bin, and alias list always says Cannot find given path.

@rillig
Copy link

rillig commented Oct 29, 2016

When I run alias set 5 echo hello, world, the output is:

set
INFO: alias "5" set

The set should not appear in the output. Also, the created 5.cmd does not work, since it looks like this:

d %*

@rillig
Copy link

rillig commented Oct 29, 2016

The installation instructions should say save this script as alias.cmd instead of setalias.cmd.

@rillig
Copy link

rillig commented Oct 29, 2016

Aliases defined with this tool cannot be used in other .cmd scripts, since execution stops after the first one. For example:

five.cmd:

@echo off
one
one
one
one
one

one.cmd:

@echo off
echo one

When running five.cmd, I expect it to output 5 times a line one. Instead, only the first line is output.

@rubyFeedback
Copy link

Could someone add commands and explanations to it? I am a Linux guy, know a lot about
ruby too but windows is black magic to me. :(

@onyxcode
Copy link

this is just chef's kiss amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment