Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 22, 2021 03:33 pm GMT

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}
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 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
Enter fullscreen mode Exit fullscreen mode

Sources:

Thats All Folks!
Happy Coding

Buy me a cofee


Original Link: https://dev.to/equiman/reveal-the-command-behind-an-alias-with-zsh-4d96

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To