summaryrefslogtreecommitdiff
path: root/plugins/alias-finder
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2026-01-04 22:47:54 -0800
committerTuowen Zhao <ztuowen@gmail.com>2026-01-04 22:47:54 -0800
commit2aa4cb7a52b28722816ecfd55f3b06293332c55c (patch)
treef02a9f3d59d109c70caf932a24e43368994e0e8c /plugins/alias-finder
parent7e951c254e779ff0620537cf43ca69dd878387b4 (diff)
parentd23d3ea69fdb839088e6e5589557cce77b34aaf8 (diff)
downloadzsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.tar.gz
zsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.tar.bz2
zsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.zip
Merge remote-tracking branch 'github/master'HEADmaster
Diffstat (limited to 'plugins/alias-finder')
-rw-r--r--plugins/alias-finder/README.md39
-rw-r--r--plugins/alias-finder/alias-finder.plugin.zsh14
2 files changed, 49 insertions, 4 deletions
diff --git a/plugins/alias-finder/README.md b/plugins/alias-finder/README.md
index 6c87c723a..b24f8d4ac 100644
--- a/plugins/alias-finder/README.md
+++ b/plugins/alias-finder/README.md
@@ -2,7 +2,7 @@
This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
-## Usage
+## Setup
To use it, add `alias-finder` to the `plugins` array of your zshrc file:
```
@@ -11,6 +11,8 @@ plugins=(... alias-finder)
To enable it for every single command, set zstyle in your `~/.zshrc`.
+If the user has installed `rg`([ripgrep](https://github.com/BurntSushi/ripgrep)), it will be used because it's faster. Otherwise, it will use the `grep` command.
+
```zsh
# ~/.zshrc
@@ -22,6 +24,41 @@ zstyle ':omz:plugins:alias-finder' cheaper yes # disabled by default
As you can see, options are also available with zstyle.
+## Usage
+
+When you execute a command alias finder will look at your defined aliases and suggest shorter aliases you could have used, for example:
+
+Running the un-aliased `git status` command:
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ git status
+
+gst='git status' # <=== shorter suggestion from alias-finder
+
+On branch main
+Your branch is up-to-date with 'origin/main'.
+nothing to commit, working tree clean
+```
+
+Running a shorter `git st` alias from `.gitconfig` that it suggested :
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ git st
+gs='git st' # <=== shorter suggestion from alias-finder
+## main...origin/main
+```
+
+Running the shortest `gs` shell alias that it found:
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ gs
+ # <=== no suggestions alias-finder because this is the shortest
+## main...origin/main
+```
+
+![image](https://github.com/ohmyzsh/ohmyzsh/assets/19378/39642750-fb10-4f1a-b7f9-f36789eeb01b)
+
+
### Options
> In order to clarify, let's say `alias a=abc` has source 'abc' and destination 'a'.
diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh
index 5fdfbc835..6f24c7089 100644
--- a/plugins/alias-finder/alias-finder.plugin.zsh
+++ b/plugins/alias-finder/alias-finder.plugin.zsh
@@ -36,14 +36,22 @@ alias-finder() {
# make filter to find only shorter results than current cmd
if [[ $cheaper == true ]]; then
cmdLen=$(echo -n "$cmd" | wc -c)
- filter="^'{0,1}.{0,$((cmdLen - 1))}="
+ if [[ $cmdLen -le 1 ]]; then
+ return
+ fi
+
+ filter="^'?.{1,$((cmdLen - 1))}'?=" # some aliases is surrounded by single quotes
fi
- alias | grep -E "$filter" | grep -E "=$finder"
+ if (( $+commands[rg] )); then
+ alias | rg "$filter" | rg "=$finder"
+ else
+ alias | grep -E "$filter" | grep -E "=$finder"
+ fi
if [[ $exact == true ]]; then
break # because exact case is only one
- elif [[ $longer = true ]]; then
+ elif [[ $longer == true ]]; then
break # because above grep command already found every longer aliases during first cycle
fi