summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorSomeDer <48731521+SomeDer@users.noreply.github.com>2019-08-19 16:53:13 +0100
committerMarc Cornellà <marc.cornella@live.com>2019-08-19 17:53:13 +0200
commit90a0de4698d8ec4300bf6aa3d6847e299a05b1f5 (patch)
tree4132e52643dd495e91064d1dcf1ae2c450fd5d78 /plugins
parent97c0d0a563d2303adc64e94c78544bb4d2dbf83c (diff)
downloadzsh-90a0de4698d8ec4300bf6aa3d6847e299a05b1f5.tar.gz
zsh-90a0de4698d8ec4300bf6aa3d6847e299a05b1f5.tar.bz2
zsh-90a0de4698d8ec4300bf6aa3d6847e299a05b1f5.zip
Add alias-finder plugin (#7768)
Diffstat (limited to 'plugins')
-rw-r--r--plugins/alias-finder/README.md46
-rw-r--r--plugins/alias-finder/alias-finder.plugin.zsh46
2 files changed, 92 insertions, 0 deletions
diff --git a/plugins/alias-finder/README.md b/plugins/alias-finder/README.md
new file mode 100644
index 000000000..409f4b653
--- /dev/null
+++ b/plugins/alias-finder/README.md
@@ -0,0 +1,46 @@
+# alias-finder plugin
+
+This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
+
+To use it, add `alias-finder` to the `plugins` array of your zshrc file:
+```
+plugins=(... alias-finder)
+```
+
+## Usage
+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.
+
+## Options
+
+- Use `--longer` or `-l` to allow the aliases to be longer than the input (match aliases if they contain the input).
+- Use `--exact` or `-e` to avoid matching aliases that are shorter than the input.
+
+## Examples
+```
+$ alias-finder "git pull"
+gl='git pull'
+g=git
+```
+```
+$ alias-finder "web_search google oh my zsh"
+google='web_search google'
+```
+```
+$ alias-finder "git commit -v"
+gc="git commit -v"
+g=git
+```
+```
+$ alias-finder -e "git commit -v"
+gc='git commit -v'
+```
+```
+$ alias-finder -l "git commit -v"
+gc='git commit -v'
+'gc!'='git commit -v --amend'
+gca='git commit -v -a'
+'gca!'='git commit -v -a --amend'
+'gcan!'='git commit -v -a --no-edit --amend'
+'gcans!'='git commit -v -a -s --no-edit --amend'
+'gcn!'='git commit -v --no-edit --amend'
+```
diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh
new file mode 100644
index 000000000..2bfaad597
--- /dev/null
+++ b/plugins/alias-finder/alias-finder.plugin.zsh
@@ -0,0 +1,46 @@
+alias-finder() {
+ local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd=""
+ for i in $@; do
+ case $i in
+ -e|--exact) exact=true;;
+ -l|--longer) longer=true;;
+ *)
+ if [[ -z $cmd ]]; then
+ cmd=$i
+ else
+ cmd="$cmd $i"
+ fi
+ ;;
+ esac
+ done
+ cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep
+ if [[ $(wc -l <<< $cmd) == 1 ]]; then
+ 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
+}
+
+preexec_functions+=(preexec_alias-finder)