From e224d133f3c0a89ff27f7164d5a653fce7508a4d Mon Sep 17 00:00:00 2001 From: an-selm Date: Wed, 24 Dec 2014 21:40:21 +0300 Subject: [PATCH 1/5] Rework alias command to handle duplicates * Removed unnecessary perl script --- bin/alias.bat | 13 ++++++++----- scripts/clean_aliases.pl | 36 ------------------------------------ 2 files changed, 8 insertions(+), 41 deletions(-) delete mode 100644 scripts/clean_aliases.pl diff --git a/bin/alias.bat b/bin/alias.bat index c9eeec600..9f54ebb35 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -1,4 +1,7 @@ @echo off + +set ALIASES="%CMDER_ROOT%\config\aliases" + if ["%1"] == ["/?"] goto:p_help if ["%1"] == ["/reload"] goto:p_reload if ["%2"] == [""] echo Insufficient parameters. & goto:p_help @@ -14,15 +17,15 @@ if not ["%_temp%"] == ["%_temp2%"] ( goto:eof ) -echo %* >> "%CMDER_ROOT%\config\aliases" -doskey /macrofile="%CMDER_ROOT%\config\aliases" -perl "%CMDER_ROOT%\scripts\clean_aliases.pl" -echo Alias created +:: replace already defined alias +findstr /b /v /i "%_temp%=" %ALIASES% >> %ALIASES%.tmp +echo %* >> %ALIASES%.tmp && type %ALIASES%.tmp > %ALIASES% & @del /f /q %ALIASES%.tmp +doskey /macrofile=%ALIASES% endlocal goto:eof :p_reload -doskey /macrofile="%CMDER_ROOT%\config\aliases" +doskey /macrofile=%ALIASES% echo Aliases reloaded goto:eof diff --git a/scripts/clean_aliases.pl b/scripts/clean_aliases.pl deleted file mode 100644 index c7cf699d8..000000000 --- a/scripts/clean_aliases.pl +++ /dev/null @@ -1,36 +0,0 @@ -# Cmder adds aliases to its aliases file without caring for duplicates. -# This can result in the aliases file becoming bloated. This script cleans -#the aliases file. -use Env; - -my %aliases; -my $alias_file = $CMDER_ROOT . "/config/aliases"; - -# First step -# Read the aliases file line by line, and put every entry in -# a dictionary. The newer aliases being the last, the new will -# always be kept over the old. -open (my $file_handle, '<', $alias_file) or die "cannot open '$alias_file' $!"; -while(my $line = <$file_handle>) -{ - if ($line =~ /([^=\s<>]+)=(.*)/) - { - $aliases{ $1 } = $2; - } - else - { - print "Invalid alias: $line" - } -} -close($file_handle); - - -# Second step -# Write back the aliases. Sort them to make the file look nice. -open(my $file_handle, '>', $alias_file) or die "cannot open '$alias_file' $!"; -foreach my $key (sort keys %aliases) -{ - print $file_handle "$key=$aliases{ $key }\n"; -} -close($file_handle); - From 0ae09e9a4aed4af58f2d3ba5ab993906d113e50b Mon Sep 17 00:00:00 2001 From: an-selm Date: Wed, 24 Dec 2014 22:14:59 +0300 Subject: [PATCH 2/5] Add /d option to alias command that removes existing alias * Remove separate unalias script and add `unalias` alias ^) for `alias /d` --- bin/alias.bat | 8 ++++++++ bin/unalias.bat | 28 ---------------------------- config/aliases | 1 + 3 files changed, 9 insertions(+), 28 deletions(-) delete mode 100644 bin/unalias.bat diff --git a/bin/alias.bat b/bin/alias.bat index 9f54ebb35..b9b027c9d 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -4,6 +4,8 @@ set ALIASES="%CMDER_ROOT%\config\aliases" if ["%1"] == ["/?"] goto:p_help if ["%1"] == ["/reload"] goto:p_reload +:: /d flag for delete existing alias +if ["%1"] == ["/d"] goto:p_del %* if ["%2"] == [""] echo Insufficient parameters. & goto:p_help ::validate alias setlocal @@ -24,6 +26,12 @@ doskey /macrofile=%ALIASES% endlocal goto:eof +:p_del +findstr /b /v /i "%2=" %ALIASES% >> %ALIASES%.tmp +type %ALIASES%.tmp > %ALIASES% & @del /f /q %ALIASES%.tmp +doskey /macrofile=%ALIASES% +goto:eof + :p_reload doskey /macrofile=%ALIASES% echo Aliases reloaded diff --git a/bin/unalias.bat b/bin/unalias.bat deleted file mode 100644 index 8dc855a9a..000000000 --- a/bin/unalias.bat +++ /dev/null @@ -1,28 +0,0 @@ -@echo off -if ["%1"] == ["/?"] goto:p_help -if ["%1"] == [""] echo Insufficient parameters. & goto:p_help - -setlocal - -:: Check if alias exists -doskey /macros | findstr /b %1= >NUL || goto :p_not_found - -:: Remove alias from current shell -doskey %1= - -:: Remove alias from aliases file -copy /y "%CMDER_ROOT%\config\aliases" "%TEMP%\aliases.prev" >NUL -type "%TEMP%\aliases.prev" | findstr /b /v %1= > "%CMDER_ROOT%\config\aliases" -echo Alias removed - -endlocal -goto:eof - -:p_not_found -echo Alias not defined. -goto:eof - -:p_help -echo.Usage: -echo. unalias name -echo. For more information, read DOSKEY/? diff --git a/config/aliases b/config/aliases index 7ef57f8a9..29d94a380 100644 --- a/config/aliases +++ b/config/aliases @@ -4,3 +4,4 @@ ls=ls --color $* pwd=cd clear=cls history=cat %CMDER_ROOT%\config\.history +unalias=alias /d $1 From 30ddd53288d0d2c8b64d7e2948504dd475b4ebc3 Mon Sep 17 00:00:00 2001 From: an-selm Date: Wed, 24 Dec 2014 22:16:26 +0300 Subject: [PATCH 3/5] `alias` command without arguments now outputs all defined aliases --- bin/alias.bat | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/alias.bat b/bin/alias.bat index b9b027c9d..7dc23e95f 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -2,6 +2,7 @@ set ALIASES="%CMDER_ROOT%\config\aliases" +if ["%*"] == [""] echo Use /? for help & echo. & goto :p_show if ["%1"] == ["/?"] goto:p_help if ["%1"] == ["/reload"] goto:p_reload :: /d flag for delete existing alias @@ -37,6 +38,10 @@ doskey /macrofile=%ALIASES% echo Aliases reloaded goto:eof +:p_show +type %ALIASES% || echo No aliases found at %ALIASES% +goto :eof + :p_help echo.Usage: echo. alias name=full command From 01849c336516bcc505605752cb3142f9948b92af Mon Sep 17 00:00:00 2001 From: an-selm Date: Wed, 24 Dec 2014 22:20:16 +0300 Subject: [PATCH 4/5] Fix quotation for aliases variable --- bin/alias.bat | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bin/alias.bat b/bin/alias.bat index 7dc23e95f..d7e023f70 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -1,6 +1,6 @@ @echo off -set ALIASES="%CMDER_ROOT%\config\aliases" +set ALIASES=%CMDER_ROOT%\config\aliases if ["%*"] == [""] echo Use /? for help & echo. & goto :p_show if ["%1"] == ["/?"] goto:p_help @@ -21,25 +21,25 @@ if not ["%_temp%"] == ["%_temp2%"] ( ) :: replace already defined alias -findstr /b /v /i "%_temp%=" %ALIASES% >> %ALIASES%.tmp -echo %* >> %ALIASES%.tmp && type %ALIASES%.tmp > %ALIASES% & @del /f /q %ALIASES%.tmp -doskey /macrofile=%ALIASES% +findstr /b /v /i "%_temp%=" "%ALIASES%" >> "%ALIASES%.tmp" +echo %* >> "%ALIASES%.tmp" && type "%ALIASES%.tmp" > "%ALIASES%" & @del /f /q "%ALIASES%.tmp" +doskey /macrofile="%ALIASES%" endlocal goto:eof :p_del -findstr /b /v /i "%2=" %ALIASES% >> %ALIASES%.tmp -type %ALIASES%.tmp > %ALIASES% & @del /f /q %ALIASES%.tmp +findstr /b /v /i "%2=" "%ALIASES%" >> "%ALIASES%.tmp" +type "%ALIASES%".tmp > "%ALIASES%" & @del /f /q "%ALIASES%.tmp" doskey /macrofile=%ALIASES% goto:eof :p_reload -doskey /macrofile=%ALIASES% +doskey /macrofile="%ALIASES%" echo Aliases reloaded goto:eof :p_show -type %ALIASES% || echo No aliases found at %ALIASES% +type "%ALIASES%" || echo No aliases found at "%ALIASES%" goto :eof :p_help From dba00b9175c5e90d7a28d562d8dc117f7516c46f Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Thu, 8 Jan 2015 20:43:45 +0300 Subject: [PATCH 5/5] Adds info about command options --- bin/alias.bat | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/alias.bat b/bin/alias.bat index d7e023f70..1dcfdc5ba 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -44,7 +44,12 @@ goto :eof :p_help echo.Usage: -echo. alias name=full command +echo. alias [/reload] [/d] [name=full command] +echo. /reload Reload the aliases file +echo. /d Delete an alias (must be followed by the alias name) +echo. +echo. If alias is called with any parameters, it will display the list of existing aliases. +echo. In the command, you can use the following notations: echo. $* allows the alias to assume all the parameters of the supplied command. echo. $1-$9 Allows you to seperate parameter by number, much like %%1 in batch. echo. $T is the command seperator, allowing you to string several commands together into one alias.