90a0de4698d8ec4300bf6aa3d6847e299a05b1f5

Author
SomeDer <48731521+SomeDer@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Add alias-finder plugin (#7768)

Diff

  1diff --git a/plugins/alias-finder/README.md b/plugins/alias-finder/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..409f4b6536210d74caa3efee77ffc5a930da728b
  4--- /dev/null
  5+++ b/plugins/alias-finder/README.md
  6@@ -0,0 +1,46 @@
  7+# alias-finder plugin
  8+
  9+This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
 10+
 11+To use it, add `alias-finder` to the `plugins` array of your zshrc file:
 12+```
 13+plugins=(... alias-finder)
 14+```
 15+
 16+## Usage
 17+To see if there is an alias defined for the command, pass it as an argument to `alias-finder`. This can also run automatically before each command you input - add `ZSH_ALIAS_FINDER_AUTOMATIC=true` to your zshrc if you want this.
 18+
 19+## Options
 20+
 21+- Use `--longer` or `-l` to allow the aliases to be longer than the input (match aliases if they contain the input).
 22+- Use `--exact` or `-e` to avoid matching aliases that are shorter than the input.
 23+
 24+## Examples
 25+```
 26+$ alias-finder "git pull"
 27+gl='git pull'
 28+g=git
 29+```
 30+```
 31+$ alias-finder "web_search google oh my zsh"
 32+google='web_search google'
 33+```
 34+```
 35+$ alias-finder "git commit -v"
 36+gc="git commit -v"
 37+g=git
 38+```
 39+```
 40+$ alias-finder -e "git commit -v"
 41+gc='git commit -v'
 42+```
 43+```
 44+$ alias-finder -l "git commit -v"
 45+gc='git commit -v'
 46+'gc!'='git commit -v --amend'
 47+gca='git commit -v -a'
 48+'gca!'='git commit -v -a --amend'
 49+'gcan!'='git commit -v -a --no-edit --amend'
 50+'gcans!'='git commit -v -a -s --no-edit --amend'
 51+'gcn!'='git commit -v --no-edit --amend'
 52+```
 53diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh
 54new file mode 100644
 55index 0000000000000000000000000000000000000000..2bfaad597e26e4f1dd0d95eb745887c45844fd6e
 56--- /dev/null
 57+++ b/plugins/alias-finder/alias-finder.plugin.zsh
 58@@ -0,0 +1,46 @@
 59+alias-finder() {
 60+  local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd=""
 61+  for i in $@; do
 62+    case $i in
 63+      -e|--exact) exact=true;;
 64+      -l|--longer) longer=true;;
 65+      *) 
 66+        if [[ -z $cmd ]]; then
 67+          cmd=$i
 68+        else
 69+          cmd="$cmd $i"
 70+        fi
 71+        ;;
 72+    esac
 73+  done
 74+  cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep
 75+  if [[ $(wc -l <<< $cmd) == 1 ]]; then
 76+    while [[ $cmd != "" ]]; do
 77+      if [[ $longer = true ]]; then
 78+        wordStart="'{0,1}"
 79+      else
 80+        wordEnd="$"
 81+        multiWordEnd="'$"
 82+      fi
 83+      if [[ $cmd == *" "* ]]; then
 84+        local finder="'$cmd$multiWordEnd"
 85+      else
 86+        local finder=$wordStart$cmd$wordEnd
 87+      fi
 88+      alias | grep -E "=$finder"
 89+      if [[ $exact = true || $longer = true ]]; then
 90+        break
 91+      else
 92+        cmd=$(sed -E 's/ {0,1}[^ ]*$//' <<< $cmd) # removes last word
 93+      fi
 94+    done
 95+  fi
 96+}
 97+
 98+preexec_alias-finder() {
 99+  if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
100+    alias-finder $1
101+  fi
102+}
103+
104+preexec_functions+=(preexec_alias-finder)