This commit is contained in:
Nicolas Canceill 2018-04-17 20:55:49 +00:00 committed by GitHub
commit 5281fdb972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -71,6 +71,32 @@ function try_alias_value() {
alias_value "$1" || echo "$1"
}
#
# Alias only if not already a command/function/alias
# Use option ALLOW_OVERRIDE_ALIASES to force overriding
#
# Arguments:
# 1. my_alias='my alias value'
# Same argument you would pass to the "alias" built-in
# Return value:
# 0 if the alias was safely created, 73 otherwise
#
function safe_alias() {
emulate -L zsh
local cmd lhs rhs
cmd=(${(s:=:)1})
lhs=$cmd[1]
rhs=$cmd[2,-1]
if (( $+commands[$lhs] || $+functions[$lhs] || $+aliases[$lhs] )); then
if [[ $ALLOW_OVERRIDE_ALIASES = "true" ]]; then
>&2 echo "WARN: override \"$lhs\" with alias \"$rhs\""
else
return 73 # os.EX_CANTCREAT
fi
fi
alias $lhs="$rhs"
}
#
# Set variable "$1" to default value "$2" if "$1" is not yet defined.
#

View File

@ -46,6 +46,11 @@ ZSH_THEME="robbyrussell"
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line to allow aliases defined in libs and plugins to
# override existing commands/functions/aliases. A warning will be printed for
# every alias overriding an existing name.
# ALLOW_OVERRIDE_ALIASES="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"