Skip to main content

How to use alias

· 6 min read
Dani Vorobiev
Front End Engineer

What is an alias?

An alias is an alternative name for a command or a group of commands. It allows you to run an instruction using a custom command in your command line interpreter. Using aliases can improve productivity and make the command line easier to use. Setting aliases for repetitive tasks or very complex commands can simplify your workflow and make it more efficient. Shell shortcuts, Git, project-specific aliases or NPM commands are some of the use cases where you can benefit from setting aliases.

You don’t need to go overboard creating aliases for everything. Focus on how often you use a command and how long it takes to run. If it’s something you rarely use, there’s no need to optimize it. But if it’s a command you run every day, multiple times, creating an alias can save you time and effort. I have lots of clever aliases, but the ones that I enjoy the most are very basic: npm run, start, dev, serve, test and git commands. Use the Pareto rule and optimize the 20%

You can set an alias like this:

alias ll='ls -alF'

If you want to persist your aliases over the current session, you need to store them in your shell config file, e.g.:~/.bashrc or ~/.zshrc. This file loads every time you open a new terminal session and it modifies your $PATH variable in the runtime.

If you edit your .zshrc file you will need to open a new terminal or do source ~/.zshrc to reload the shell cofiguration. If you are using OMZsh you can do it by running omz reload.

If you want to change the location of .zshrc you need to set it up in .zprofile file.

If you don't want to use an already cluttered .zshrc file and prefer to keep your aliases in a separate file, you can place them in the directory stored in your ZSH_CUSTOM system variable. Typically, this variable stores .oh-my-zsh/custom, so you can create a .zsh file there and name it as you prefer (e.g., custom_aliases.zsh). You can store all your aliases in this file just as you would in .zshrc, and your system will load them when .zshrc is sourced. This way you separate concerns and keep you personal aliases separated from all the other setups present in .zshrc.

If you don't use OhMyZsh, by default ZSH only loads specific files (~/.zshrc ~/.zprofile ~/.zshenv ~/.zlogin ~/.zlogout), and you cannot add directly a custom alias file. Instead, you need to write the logic to source the file you want to import. In your .zshrc you can add imports like this:

source ~/.zsh/aliases.zsh
source ~/.zsh/functions.zsh

If you want to add more files in the future, and don't want to constantly modify your .zshrc you can do a for loop and import everything in a certain directory:

# Source all personal config files
for file in ~/.zsh/*.zsh; do
source "$file"
done

You can list all currently defined aliases using the command:

alias

Or you can check a specific alias:

alias ll

Alternatively, you can use type ll to know which command will be run. This command shows you if it's a shell built in, an alias or a binary.

Unsetting an Alias You can remove an alias with the unalias command:

unalias ll

How do aliases work?

Alias expansion:

  1. Tokenization
  2. Alias lookup: The shell first checks if the first word of your command matches an alias in some of your configuration files. If it does, it replaces that word with the alias's definition. This process is known as alias expansion.
  3. Command execution

Alias precedence

By default, alias expansion is disabled in non-interactive shells (like in scripts) to avoid unexpected behavior. In interactive shells, however, alias expansion is enabled.

The order of precedence is:

  • Shell functions (highest precedence)
  • Aliases
  • Built-in commands (e.g., echo, cd)
  • External commands found in $PATH (executables)

So, if you have both a shell function and an alias with the same name, the shell function will take precedence, not the alias.

Aliases in OMZsh

One of the greatest benefits of using OMZsh is all the predefined aliases that it provides. Some of the aliases and scripts you can use in OMZsh are:

🔹 General Command Shortcuts

AliasExpands ToDescription
llls -lhLong format with human-readable sizes
lals -lhaShow hidden files too
lls -lLong format without hidden files
..cd ..Move up one directory
...cd ../..Move up two directories
....cd ../../..Move up three directories
cclearClear terminal
mdmkdir -pCreate a directory and parent directories if needed
rdrmdirRemove empty directory

🔹 Other Useful Aliases

AliasExpands ToDescription
brewubrew updateUpdate Homebrew and installed packages
npminpm installInstall dependencies in a Node.js project
pingping -c 5Ping with 5 packets instead of infinite
yarnyarnRun Yarn package manager

🔹 Git Aliases (enabled by default in OMZ)

AliasExpands ToDescription
gstgit statusShow current repo status
gcogit checkoutCheckout a branch
gcmsggit commit -mCommit with message
gpgit pushPush changes
glgit pullPull latest changes
gagit addAdd files to staging
gaagit add --allAdd all files to staging
gcmgit commit -mCommit with message
gcbgit checkout -bCreate new branch and switch to it
gbgit branchList branches
gdgit diffShow changes

Your Git aliases are located in ~/.oh-my-zsh/plugins/git/git.plugin.zsh and you can find the full list at: OMZ Git aliases.