DEV Community

loading...
Cover image for Reveal the command behind an alias with ZSH

Reveal the command behind an alias with ZSH

equiman profile image Camilo Martinez ・2 min read

I love alias commands, but also always like to πŸ•΅οΈβ€β™€οΈ spy on what is the real command behind an alias. There are a lot of ways to see it:

  • Using the alias <command> on terminal
  • Using the keyboard shortcut combination ctrl+x a (not ctrl+x+a) to expand it
  • Using the globalias plugin

But all of them need an extra step, and I found a way to see it always 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


Sources:


That’s All Folks!
Happy Coding πŸ––

Buy me a cofee

Discussion (7)

pic
Editor guide
Collapse
defman profile image
Sergey Kislyakov πŸ‡·πŸ‡Ί πŸ‡ΊπŸ‡Έ

Just FYI, fish shell does that by default in a more interesting manner: it actually expands the command. So if you type e.g. ll and press space, fish would replace ll with ls -lh.

Collapse
equiman profile image
Camilo Martinez Author

Nice idea, I found a way to do it on ZSH adding the Globalias plugin.

Collapse
bhupesh profile image
Bhupesh Varshney πŸ‘Ύ

For anyone using bash. Just press Ctrl+Alt+e

Collapse
equiman profile image
Camilo Martinez Author

Good information, I didn't know it.
I found that ctrl+x a do the same for ZSH

Collapse
alexgeorgiev17 profile image
Alex Georgiev

This is really cool! Thanks for sharing it!

Collapse
meldak profile image
Irving Gabriel Ocampo

It Is so pretty, thanks so much, it is so useful.

Collapse
equiman profile image
Camilo Martinez Author

You welcome @meldak 🀘