From 90a0de4698d8ec4300bf6aa3d6847e299a05b1f5 Mon Sep 17 00:00:00 2001 From: SomeDer <48731521+SomeDer@users.noreply.github.com> Date: Mon, 19 Aug 2019 16:53:13 +0100 Subject: [PATCH] Add alias-finder plugin (#7768) --- plugins/alias-finder/README.md | 46 ++++++++++++++++++++ plugins/alias-finder/alias-finder.plugin.zsh | 46 ++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 plugins/alias-finder/README.md create mode 100644 plugins/alias-finder/alias-finder.plugin.zsh 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)