From 8ee263a82851d6b306c031fdd63ddfd427ee131f Mon Sep 17 00:00:00 2001 From: Robby Russell Date: Fri, 27 Nov 2026 19:29:14 -0800 Subject: [PATCH] fix(alias-finder): suggest aliases when the command starts with an alias The automatic hook searched the command exactly as typed, so `k get pods` never matched `kgp='kubectl get pods'`. It now searches the alias-expanded command zsh passes to preexec ($3). When the command started with an alias, only aliases that cover more of the command are suggested, so typing `gst` alone still prints nothing. Closes #12040 Co-Authored-By: Claude Opus 5.5 --- plugins/alias-finder/README.md | 2 ++ plugins/alias-finder/alias-finder.plugin.zsh | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) 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