An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
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 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 grepalias ag="alias | grep" # command to grep# Gitalias gcu="git config user.name \"equiman\" && git config user.email \"[email protected]\""# npmalias 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 folderalias obp="${OPEN} \".\\build\"" # open build folderalias ocr="${OPEN} \".\\coverage\\lcov-report\\index.html\"" # browse coverage
Sources:
Thats All Folks!
Happy Coding
Original Link: https://dev.to/equiman/reveal-the-command-behind-an-alias-with-zsh-4d96
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To