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
}
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
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.
Once finish, reopen all terminals or update his source running source ~/.zshrc
command and now you can see those secret commands.
Example
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
Sources:
That’s All Folks!
Happy Coding ?
This content originally appeared on DEV Community and was authored by Camilo Martinez

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.