DEV Community

vuong ⛈️
vuong ⛈️

Posted on

Search alias or find alias in zsh plugins

I got back to Zsh after having some months playing Fish. I found Fish was so convenience to me, but I really want to get back to use Bash as default for some work stuffs, for example working with Bash. Some other stuffs like using Bash programs or HEREDOC aren't available, not too important but sometimes searching for Fish-targetted things was also not fun.

Then I felt missing function auto completion of Fish, that I can easily search a Fish function and see its original command. It's very helpful because I built a lot of alias for cli stuffs like kubectl, git, docker, github cli, ... Made my works being faster, I didn't have to type too much.

I know in Zsh, there is a way to have useful aliases is using zsh plugins, but I have to deal with cheatsheets of every plugin to learn and remember them. There is a way for it, bookmarking. But I though that a Bash function to do offline searching on plugins folder would be very simple and just need to scan README.md of the specified plugin and grep the keyword.

# Gist: https://gist.github.com/vuon9/bdeb471d9cbd6f8c0fac23a9f291196a
function zpa() {
    # Command: zpa
    # Options:
    # [1]: plugin name, e.g: git, brew, kubectl, docker, etc.
    # [2]: grep string, e.g: gc, 'kubectl exec', 'get pods', etc.
    # [3] (optional): -a (if you want to search in all official plugins)

    # Examples:
    # zpa git gc: Search alias gc in git plugin to see what is it
    # zpa kubectl 'kubectl': I want to learn if there is an alias of 'git clone' is existed

    installedPlugins=("${plugins[@]}")
    if [ "$3" = "-a" ]; then
        installedPlugins=($(ls $ZSH/plugins))
    fi

    for plugin in $installedPlugins
    do
        if [ "$1" = $plugin ]; then
            bat $ZSH/plugins/$plugin/README.md | grep "$2"
        fi
    done
}
Enter fullscreen mode Exit fullscreen mode

Examples:
Given

  • I enabled kubectl plugin for zsh
  • I haven't enabled brew plugin for zsh

When & Then
Image description

Look at the screenshot, I found some clues if Zsh plugins supported for my search keywords.

Hope it helps and happy coding!

Top comments (1)

Collapse
 
vuong profile image
vuong ⛈️

I've also created a nicer version here as cli tool with Go github.com/vuon9/zshs