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

preexec_alias-finder() {
  if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
    alias-finder $1
  fi
}

46
47
autoload -U add-zsh-hook
add-zsh-hook preexec preexec_alias-finder