diff --git a/plugins/alias-finder/README.md b/plugins/alias-finder/README.md index b24f8d4ac..d90a04b16 100644 --- a/plugins/alias-finder/README.md +++ b/plugins/alias-finder/README.md @@ -58,6 +58,8 @@ Running the shortest `gs` shell alias that it found: ![image](https://github.com/ohmyzsh/ohmyzsh/assets/19378/39642750-fb10-4f1a-b7f9-f36789eeb01b) +It also looks through aliases you type, suggesting only ones that cover more of the command. With the kubectl plugin, `k get pods` suggests `kgp`, while `kgp` on its own suggests nothing. + ### Options diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh index b2b5e4e75..2821d344e 100644 --- a/plugins/alias-finder/alias-finder.plugin.zsh +++ b/plugins/alias-finder/alias-finder.plugin.zsh @@ -65,9 +65,20 @@ alias-finder() { preexec_alias-finder() { # TODO: Remove backward compatibility (other than zstyle form) - zstyle -t ':omz:plugins:alias-finder' autoload && alias-finder $1 || if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then - alias-finder $1 + zstyle -t ':omz:plugins:alias-finder' autoload || [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]] || return + + # $3 has aliases expanded, so `k get pods` can suggest `kgp` + local -a words=(${(z)1}) found=(${(f)"$(alias-finder $3)"}) keep + local used=${aliases[$words[1]]} line + + # If an alias was typed, only suggest aliases that cover more of the command + if [[ -n "$used" ]]; then + for line in $found; do + (( ${#${(Q)line#*=}} > ${#used} )) && keep+=("$line") + done + found=($keep) fi + (( $#found )) && print -rl -- $found } autoload -U add-zsh-hook