mirror of
https://github.com/cmderdev/cmder.git
synced 2025-02-25 14:50:22 +08:00
Merge pull request #862 from daxgames/better_aliases
Enhanced alias.bat to allow file storage path
This commit is contained in:
commit
d5a1ce628c
125
bin/alias.bat
125
bin/alias.bat
@ -1,64 +1,131 @@
|
|||||||
@echo off
|
@echo off
|
||||||
|
|
||||||
set ALIASES=%CMDER_ROOT%\config\aliases
|
|
||||||
setlocal
|
if "%aliases%" == "" (
|
||||||
:: handle quotes within command definition, e.g. quoted long file names
|
set ALIASES=%CMDER_ROOT%\config\user-aliases.cmd
|
||||||
set _x="%*"
|
)
|
||||||
set _x=%_x:"=%
|
|
||||||
|
setlocal enabledelayedexpansion
|
||||||
|
|
||||||
|
if "%~1" == "" echo Use /? for help & echo. & goto :p_show
|
||||||
|
|
||||||
:: check command usage
|
:: check command usage
|
||||||
if ["%_x%"] == [""] echo Use /? for help & echo. & goto :p_show
|
|
||||||
if ["%1"] == ["/?"] goto:p_help
|
rem #region parseargument
|
||||||
if ["%1"] == ["/reload"] goto:p_reload
|
goto parseargument
|
||||||
:: /d flag for delete existing alias
|
|
||||||
if ["%1"] == ["/d"] goto:p_del %*
|
:do_shift
|
||||||
:: if arg is an existing alias, display it
|
shift
|
||||||
if ["%2"] == [""] (
|
|
||||||
doskey /macros | findstr /b %1= && goto:eof
|
:parseargument
|
||||||
echo Insufficient parameters. & goto:p_help
|
set currentarg=%~1
|
||||||
|
|
||||||
|
if /i "%currentarg%" equ "/f" (
|
||||||
|
set aliases=%~2
|
||||||
|
shift
|
||||||
|
goto :do_shift
|
||||||
|
) else if /i "%currentarg%" == "/reload" (
|
||||||
|
goto :p_reload
|
||||||
|
) else if "%currentarg%" equ "/?" (
|
||||||
|
goto :p_help
|
||||||
|
) else if /i "%currentarg%" equ "/d" (
|
||||||
|
if "%~2" neq "" (
|
||||||
|
if "%~3" equ "" (
|
||||||
|
:: /d flag for delete existing alias
|
||||||
|
call :p_del %~2
|
||||||
|
shift
|
||||||
|
goto :eof
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) else if "%currentarg%" neq "" (
|
||||||
|
if "%~2" equ "" (
|
||||||
|
:: Show the specified alias
|
||||||
|
doskey /macros | findstr /b %currentarg%= && exit /b
|
||||||
|
echo insufficient parameters.
|
||||||
|
goto :p_help
|
||||||
|
) else (
|
||||||
|
:: handle quotes within command definition, e.g. quoted long file names
|
||||||
|
set _x=%*
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rem #endregion parseargument
|
||||||
|
|
||||||
|
if "%aliases%" neq "%CMDER_ROOT%\config\user-aliases.cmd" (
|
||||||
|
set _x=!_x:/f %aliases% =!
|
||||||
|
|
||||||
|
if not exist "%aliases%" (
|
||||||
|
echo ;= @echo off>"%aliases%"
|
||||||
|
echo ;= rem Call DOSKEY and use this file as the macrofile>>"%aliases%"
|
||||||
|
echo ;= %%SystemRoot%%\system32\doskey /listsize=1000 /macrofile=%%0%%>>"%aliases%"
|
||||||
|
echo ;= rem In batch mode, jump to the end of the file>>"%aliases%"
|
||||||
|
echo ;= goto:eof>>"%aliases%"
|
||||||
|
echo ;= Add aliases below here>>"%aliases%"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
:: validate alias
|
:: validate alias
|
||||||
for /f "delims== tokens=1" %%G in ("%_x%") do set alias=%%G
|
for /f "delims== tokens=1,2 usebackq" %%G in (`echo "%_x%"`) do (
|
||||||
set _temp=%alias: =%
|
set alias_name=%%G
|
||||||
|
set alias_value=%%H
|
||||||
|
)
|
||||||
|
|
||||||
if not ["%_temp%"] == ["%alias%"] (
|
:: leading quotes added while validating
|
||||||
|
set alias_name=%alias_name:~1%
|
||||||
|
|
||||||
|
:: trailing quotes added while validating
|
||||||
|
set alias_value=%alias_value:~0,-1%
|
||||||
|
|
||||||
|
::remove spaces
|
||||||
|
set _temp=%alias_name: =%
|
||||||
|
|
||||||
|
if not ["%_temp%"] == ["%alias_name%"] (
|
||||||
echo Your alias name can not contain a space
|
echo Your alias name can not contain a space
|
||||||
endlocal
|
endlocal
|
||||||
goto:eof
|
exit /b
|
||||||
)
|
)
|
||||||
|
|
||||||
:: replace already defined alias
|
:: replace already defined alias
|
||||||
findstr /b /v /i "%alias%=" "%ALIASES%" >> "%ALIASES%.tmp"
|
findstr /b /v /i "%alias_name%=" "%ALIASES%" >> "%ALIASES%.tmp"
|
||||||
echo %* >> "%ALIASES%.tmp" && type "%ALIASES%.tmp" > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
|
echo %alias_name%=%alias_value% >> "%ALIASES%.tmp" && type "%ALIASES%.tmp" > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
|
||||||
doskey /macrofile="%ALIASES%"
|
doskey /macrofile="%ALIASES%"
|
||||||
endlocal
|
endlocal
|
||||||
goto:eof
|
exit /b
|
||||||
|
|
||||||
:p_del
|
:p_del
|
||||||
findstr /b /v /i "%2=" "%ALIASES%" >> "%ALIASES%.tmp"
|
set del_alias=%~1
|
||||||
|
findstr /b /v /i "%del_alias%=" "%ALIASES%" >> "%ALIASES%.tmp"
|
||||||
type "%ALIASES%".tmp > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
|
type "%ALIASES%".tmp > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
|
||||||
|
doskey %del_alias%=
|
||||||
doskey /macrofile=%ALIASES%
|
doskey /macrofile=%ALIASES%
|
||||||
goto:eof
|
goto:eof
|
||||||
|
|
||||||
:p_reload
|
:p_reload
|
||||||
doskey /macrofile="%ALIASES%"
|
doskey /macrofile="%ALIASES%"
|
||||||
echo Aliases reloaded
|
echo Aliases reloaded
|
||||||
goto:eof
|
exit /b
|
||||||
|
|
||||||
:p_show
|
:p_show
|
||||||
type "%ALIASES%" || echo No aliases found at "%ALIASES%"
|
doskey /macros|findstr /v /r "^;=" | sort
|
||||||
goto :eof
|
exit /b
|
||||||
|
|
||||||
:p_help
|
:p_help
|
||||||
echo.Usage:
|
echo.Usage:
|
||||||
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.
|
||||||
echo. If alias is called with any parameters, it will display the list of existing aliases.
|
echo. alias [options] [alias=full command]
|
||||||
|
echo.
|
||||||
|
echo.Options:
|
||||||
|
echo.
|
||||||
|
echo. /d [alias] Delete an [alias].
|
||||||
|
echo. /f [macrofile] Path to the [macrofile] you want to store the new alias in.
|
||||||
|
echo. Default: %cmder_root%\config\user-aliases.cmd
|
||||||
|
echo. /reload Reload the aliases file. Can be used with /f argument.
|
||||||
|
echo. Default: %cmder_root%\config\user-aliases.cmd
|
||||||
|
echo.
|
||||||
|
echo. If alias is called with no parameters, it will display the list of existing aliases.
|
||||||
|
echo.
|
||||||
echo. In the command, you can use the following notations:
|
echo. In the command, you can use the following notations:
|
||||||
echo. $* allows the alias to assume all the parameters of the supplied command.
|
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. $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.
|
echo. $T is the command seperator, allowing you to string several commands together into one alias.
|
||||||
echo. For more information, read DOSKEY/?
|
echo. For more information, read DOSKEY/?
|
||||||
|
exit /b
|
||||||
|
8
vendor/aliases.example
vendored
8
vendor/aliases.example
vendored
@ -1,8 +0,0 @@
|
|||||||
e.=explorer .
|
|
||||||
gl=git log --oneline --all --graph --decorate $*
|
|
||||||
ls=ls --show-control-chars -F --color $*
|
|
||||||
pwd=cd
|
|
||||||
clear=cls
|
|
||||||
history=cat %CMDER_ROOT%\config\.history
|
|
||||||
unalias=alias /d $1
|
|
||||||
vi=vim $*
|
|
29
vendor/init.bat
vendored
29
vendor/init.bat
vendored
@ -74,14 +74,30 @@ for /f "usebackq" %%x in ( `dir /b *.bat *.cmd 2^>nul` ) do (
|
|||||||
)
|
)
|
||||||
popd
|
popd
|
||||||
|
|
||||||
:: make sure we have an example file
|
:: Allows user to override default aliases store using profile.d
|
||||||
if not exist "%CMDER_ROOT%\config\aliases" (
|
:: scripts run above. Note: If overriding default aliases file
|
||||||
echo Creating intial aliases in %CMDER_ROOT%\config\aliases
|
:: in profile.d the aliases must also be loaded in profile.d.
|
||||||
copy "%CMDER_ROOT%\vendor\aliases.example" "%CMDER_ROOT%\config\aliases" > null
|
if not defined aliases (
|
||||||
|
set aliases=%CMDER_ROOT%\config\user-aliases.cmd
|
||||||
)
|
)
|
||||||
|
|
||||||
:: Add aliases
|
:: Using default cmder user-aliases.cmd store.
|
||||||
doskey /macrofile="%CMDER_ROOT%\config\aliases"
|
if "%aliases%" == "%CMDER_ROOT%\config\user-aliases.cmd" (
|
||||||
|
:: make sure we have an example file
|
||||||
|
if not exist "%aliases%" (
|
||||||
|
echo Creating intial aliases in "%aliases%"...
|
||||||
|
copy "%CMDER_ROOT%\vendor\user-aliases.cmd.example" "%aliases%"
|
||||||
|
)
|
||||||
|
|
||||||
|
:: Update old 'aliases' to new self executing 'user-aliases.cmd'
|
||||||
|
if exist "%CMDER_ROOT%\config\aliases" (
|
||||||
|
echo Updating old "%CMDER_ROOT%\config\aliases" to new format...
|
||||||
|
type "%CMDER_ROOT%\config\aliases" >> "%aliases%" && del "%CMDER_ROOT%\config\aliases"
|
||||||
|
)
|
||||||
|
|
||||||
|
:: Add aliases to the environment
|
||||||
|
call "%aliases%"
|
||||||
|
)
|
||||||
|
|
||||||
:: See vendor\git-for-windows\README.portable for why we do this
|
:: See vendor\git-for-windows\README.portable for why we do this
|
||||||
:: Basically we need to execute this post-install.bat because we are
|
:: Basically we need to execute this post-install.bat because we are
|
||||||
@ -102,6 +118,7 @@ if defined CMDER_START (
|
|||||||
cd /d "%CMDER_START%"
|
cd /d "%CMDER_START%"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if exist "%CMDER_ROOT%\config\user-profile.cmd" (
|
if exist "%CMDER_ROOT%\config\user-profile.cmd" (
|
||||||
rem create this file and place your own command in there
|
rem create this file and place your own command in there
|
||||||
call "%CMDER_ROOT%\config\user-profile.cmd"
|
call "%CMDER_ROOT%\config\user-profile.cmd"
|
||||||
|
15
vendor/user-aliases.cmd.example
vendored
Normal file
15
vendor/user-aliases.cmd.example
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
;= @echo off
|
||||||
|
;= rem Call DOSKEY and use this file as the macrofile
|
||||||
|
;= %SystemRoot%\system32\doskey /listsize=1000 /macrofile=%0%
|
||||||
|
;= rem In batch mode, jump to the end of the file
|
||||||
|
;= goto:eof
|
||||||
|
;= Add aliases below here
|
||||||
|
e.=explorer .
|
||||||
|
gl=git log --oneline --all --graph --decorate $*
|
||||||
|
ls=ls --show-control-chars -F --color $*
|
||||||
|
pwd=cd
|
||||||
|
clear=cls
|
||||||
|
history=cat %CMDER_ROOT%\config\.history
|
||||||
|
unalias=alias /d $1
|
||||||
|
vi=vim $*
|
||||||
|
cmderr=cd /d "%CMDER_ROOT%"
|
Loading…
x
Reference in New Issue
Block a user