Post

Search ZSH aliases

Search ZSH aliases

LINUX QUALITY OF LIFE IMPROVEMENTS #2

This nice function will search all the aliases in your ~/.zshrc config.

Your alias must be preceded by a comment, a line starting with the # character, for it to be displayed in the output

Example:

1
2
# Update the whole system (Nobara way)  <-- this line will show in the Alias column
alias update='sudo nobara-sync cli'

Step 1. open .zshrc with nano, nvim or other editor

1
nvim ~/.zshrc

Step 2. Add the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Function to list aliases and their descriptions only
show-aliases() {
    echo -e "\033[1;32mDescription\033[0m                              \033[1;36mAlias\033[0m"
    echo "------------------------------------------------------------"
    
    grep -B 1 "^alias " ~/.zshrc | \
    sed 's/--//g' | \
    awk '
    # Store the comment line
    /^#/ { comment = $0; next }
    
    # Process the alias line
    /^alias / {
        sub(/^alias /, "", $0);          # Remove "alias " prefix
        split($0, parts, "=");           # Split at =
        name = parts[1];                 # Take only the shortcut name
        
        # Print Comment (Gray) and Alias (Cyan)
        printf "\033[0;90m%-40s\033[0m \033[1;36m%s\033[0m\n", comment, name;
        
        comment = ""; # Reset for next
    }'
}

Step 3. Reload ZSH

1
source ~/.zshrc

Step 4. Test show-aliases

1
show-aliases

Output will look like this:

1
2
3
4
5
Description                              Alias
------------------------------------------------------------
# iPad screen mirroring                  ipad-mirror
# Update the whole system (Nobara way)   update

This post is licensed under CC BY 4.0 by the author.