1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-11 00:10:08 +00:00

Add alias-finder plugin (#7768)

This commit is contained in:
SomeDer 2019-08-19 16:53:13 +01:00 committed by Marc Cornellà
parent 97c0d0a563
commit 90a0de4698
2 changed files with 92 additions and 0 deletions

View file

@ -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'
```

View file

@ -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)