diff options
author | Marc Cornellà <marc.cornella@live.com> | 2020-02-27 22:55:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-27 22:55:30 +0100 |
commit | 18ee5dffdc57bb9219ee96b40da006704ac37df1 (patch) | |
tree | e1fe420cf18fa7a916d5d43c408c6f92ed33b62d /plugins/alias-finder | |
parent | d81cd753e0b3a845e8f3549da245dbad102a6e4c (diff) | |
parent | 368198b7616eb69b396de86d9ec4ff0f35bd72f0 (diff) | |
download | zsh-18ee5dffdc57bb9219ee96b40da006704ac37df1.tar.gz zsh-18ee5dffdc57bb9219ee96b40da006704ac37df1.tar.bz2 zsh-18ee5dffdc57bb9219ee96b40da006704ac37df1.zip |
Merge branch 'master' into clipboard
Diffstat (limited to 'plugins/alias-finder')
-rw-r--r-- | plugins/alias-finder/README.md | 46 | ||||
-rw-r--r-- | plugins/alias-finder/alias-finder.plugin.zsh | 47 |
2 files changed, 93 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..caee9b5a3 --- /dev/null +++ b/plugins/alias-finder/alias-finder.plugin.zsh @@ -0,0 +1,47 @@ +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 +} + +autoload -U add-zsh-hook +add-zsh-hook preexec preexec_alias-finder |