Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

@VVelda 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

This comment has been minimized.

Copy link

@rillig 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

This comment has been minimized.

Copy link

@rillig 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

This comment has been minimized.

Copy link

@rillig rillig commented Oct 29, 2016

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

@rillig

This comment has been minimized.

Copy link

@rillig 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

This comment has been minimized.

Copy link

@rubyFeedback rubyFeedback commented Jul 21, 2021

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. :(

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