Skip to content

Instantly share code, notes, and snippets.

@matthewd
Created January 29, 2012 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewd/1697002 to your computer and use it in GitHub Desktop.
Save matthewd/1697002 to your computer and use it in GitHub Desktop.
Check for unused aliases
check_an_alias() {
alias_name="$1"
typed_cmd=(${(zQ)2})
alias_result=(${(zQ)3})
while [[ "${alias_result[1]}" = "noglob" ]] || [[ "${alias_result[1]}" = "nocorrect" ]]; do
shift alias_result
done
if [[ "${#alias_result}" -gt "${#typed_cmd}" ]]; then
return 1
fi
while [[ "${#alias_result}" -gt 0 ]]; do
if [[ "${alias_result[1]}" != "${typed_cmd[1]}" ]]; then
return 1
fi
shift alias_result typed_cmd
done
return 0
}
check_for_aliases() {
alias -r | while IFS='=' read alias_name alias_result; do
if [[ "${#alias_name}" -lt "${#alias_result}" ]]; then
output_result="$alias_result"
if check_an_alias "$alias_name" "$1" "$alias_result"; then
(
echo "${fg_bold[magenta]}What are you, looking for RSI?!?${reset_color}"
echo " ${fg_no_bold[green]}${alias_name}${reset_color}=${fg_no_bold[red]}${output_result}${reset_color}"
echo
) >&2
fi
fi
done
}
autoload colors; colors
add-zsh-hook preexec check_for_aliases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment