Reveal the command behind an alias with ZSH

I love alias commands, but also always like to ?️‍♀️ spy on what is the real command behind an alias. I found a way to see it before the command runs.

Reveal

We can use a function to search the command associated with an alias adding this s…


This content originally appeared on DEV Community and was authored by Camilo Martinez

I love alias commands, but also always like to ?️‍♀️ spy on what is the real command behind an alias. I found a way to see it before the command runs.

Reveal

We can use a function to search the command associated with an alias adding this script on ~/.zshrc file.

alias_for() {
  [[ $1 =~ '[[:punct:]]' ]] && return
  local found="$( alias ${1} )"
  if [[ -n $found ]]; then
    found=${found//\\//}                      # Replace backslash with slash
    found=${found%\'}                         # Remove end single quote
    found=${found#"${1}='"}                   # Remove alias name
    echo "\e[32m❯ \e[33m${found}\e[0m"        # Return with colors
  fi
}

expand_command_line() {
  [[ $# -eq 0 ]] && return                    # If there's no input, return. Else...
  local found_alias="$(alias_for $1)"         # Check if there's an alias for the comand.
  if [[ -n $found_alias ]]; then              # If there was
    echo ${found_alias}                       # Print it. 
  fi
}
Enter fullscreen mode Exit fullscreen mode

Now we need to run this function every time the alias is entered. Happily, ZSH has a preexec function that can be associated with a hook. Just add this script after the previous on ~/.zshrc file.

autoload -U add-zsh-hook                      # Load the zsh hook module. 
add-zsh-hook preexec expand_command_line      # Adds the hook 
Enter fullscreen mode Exit fullscreen mode

I'm not thinking to remove this hook, but you can do it with this command:

add-zsh-hook -d preexec expand_command_line # Remove it for this hook.
Enter fullscreen mode Exit fullscreen mode

Once finish, reopen all terminals or update his source running source ~/.zshrc command and now you can see those secret commands.

Example

alias

Yellow: alias
Red: command behind revealed

Bonus Track

In addition to all the aliases that can be activated with ZSH plugins, I've defined a lot more that I miss.

alias x="exit"
alias r="source ~/.zshrc"
alias hc="history -c"
alias hg="history | grep" # command to grep
alias ag="alias | grep" # command to grep

# Git
alias gcu="git config user.name \"equiman\" && git config user.email \"equiman@users.noreply.github.com\""

# npm
alias rnm="rm -rf node_modules"
alias rbn="rm -rf build node_modules"
alias rap="rm -rf build node_modules package-lock.json && npm i"

alias nrb="npm run build"
alias nrs="npm run start"
alias nrd="npm run start:dev"
alias nrt="npm run test"

# Explore (Windows with WSL) 
local OPEN="explorer.exe"
alias o="${OPEN} ." # open current folder
alias obp="${OPEN} \".\\build\"" # open build folder
alias ocr="${OPEN} \".\\coverage\\lcov-report\\index.html\"" # browse coverage
Enter fullscreen mode Exit fullscreen mode

Sources:

That’s All Folks!
Happy Coding ?

Buy me a cofee


This content originally appeared on DEV Community and was authored by Camilo Martinez


Print Share Comment Cite Upload Translate Updates
APA

Camilo Martinez | Sciencx (2021-02-22T15:33:50+00:00) Reveal the command behind an alias with ZSH. Retrieved from https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/

MLA
" » Reveal the command behind an alias with ZSH." Camilo Martinez | Sciencx - Monday February 22, 2021, https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/
HARVARD
Camilo Martinez | Sciencx Monday February 22, 2021 » Reveal the command behind an alias with ZSH., viewed ,<https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/>
VANCOUVER
Camilo Martinez | Sciencx - » Reveal the command behind an alias with ZSH. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/
CHICAGO
" » Reveal the command behind an alias with ZSH." Camilo Martinez | Sciencx - Accessed . https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/
IEEE
" » Reveal the command behind an alias with ZSH." Camilo Martinez | Sciencx [Online]. Available: https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/. [Accessed: ]
rf:citation
» Reveal the command behind an alias with ZSH | Camilo Martinez | Sciencx | https://www.scien.cx/2021/02/22/reveal-the-command-behind-an-alias-with-zsh/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.