alias-finder.plugin.zsh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. alias-finder() {
  2. local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd=""
  3. for i in $@; do
  4. case $i in
  5. -e|--exact) exact=true;;
  6. -l|--longer) longer=true;;
  7. *)
  8. if [[ -z $cmd ]]; then
  9. cmd=$i
  10. else
  11. cmd="$cmd $i"
  12. fi
  13. ;;
  14. esac
  15. done
  16. cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep
  17. if (( $(wc -l <<< $cmd) == 1 )); then
  18. while [[ $cmd != "" ]]; do
  19. if [[ $longer = true ]]; then
  20. wordStart="'{0,1}"
  21. else
  22. wordEnd="$"
  23. multiWordEnd="'$"
  24. fi
  25. if [[ $cmd == *" "* ]]; then
  26. local finder="'$cmd$multiWordEnd"
  27. else
  28. local finder=$wordStart$cmd$wordEnd
  29. fi
  30. alias | grep -E "=$finder"
  31. if [[ $exact = true || $longer = true ]]; then
  32. break
  33. else
  34. cmd=$(sed -E 's/ {0,1}[^ ]*$//' <<< $cmd) # removes last word
  35. fi
  36. done
  37. fi
  38. }
  39. preexec_alias-finder() {
  40. if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
  41. alias-finder $1
  42. fi
  43. }
  44. autoload -U add-zsh-hook
  45. add-zsh-hook preexec preexec_alias-finder