If you use Linux as a daily driver, you can relate to spending a lot of time working with the terminal to navigate the file systems, update and install packages, etc. A terminal is an invaluable, irreplaceable tool.
No matter how often you use the Linux shell, sometimes you may encounter commands that are too long than necessary. Typing out such commands can become time-consuming and inefficient, especially if you regularly use the command. That’s where command aliases come into play. They allow us to create nicknames of shortcuts for long commands that we use often.
This tutorial will show you how to set up aliases on ZSH, a better and improved alternative to the default BASH shell. To follow along with this tutorial, ensure you have ZSH installed as well as the oh-my-zsh framework.
Without wasting time, let us get started and discuss ZSH aliases:
ZSH Aliases
ZSH aliases are configured in the .zshrc file located in the user’s home directory. They are loaded on shell startup, but you can force-reload them by sourcing the .zshrc file.
The general syntax for the ZSH alias is:
ZSH has four main types of aliases.
1: Simple Aliases
Simple aliases are a short form of a long command. To set up a simple alias, edit the ~/.zshrc file using your text editor and add an alias at the bottom. It is good to keep all your aliases in a single section of the file to avoid confusion and ease of edit.
Next, create aliases in the form:
For example, let us set up a few aliases for working with Git Repositories:
alias ginit="git init ."
alias gadd="git add ."
alias gc="git commit -m 'Initial Commit'"
To load the changes, start a new terminal session or source the config file using the command
Now in the terminal, to initialize a directory as a git repository, use the command:
Initialized empty Git repository in /home/debian/Repo/.git/
2: Suffix Aliases
Suffix aliases help register a specific file extension and the preferred tool for launching it. We define them using the -s flag and follows a syntax:
For example, the following aliases define two file types and the corresponding tools to open them:
alias -s txt=neovim
alias -s py=geany
alias -s json=code
It is good to ensure that you have the tools you specify already installed and up to date. To create a new text file such as info.txt, enter the command as:
That will create the file and open it with the specified tool.
3: Global Aliases
Global aliases are very similar to simple aliases, but you can use them in more than one command. Global variables are used as aliases for commands that take pipe input. Global variables are defined using the -g flag using the syntax as:
For example, an alias for grep command:
alias -g gp="grep"
You can then pipe commands to grep using the created alias:
root 1 0.0 0.0 8940 268 ? Ssl 21:25 0:00 /init
root 7 0.0 0.0 8940 200 tty1 Ss 21:25 0:00 /init
4: Parametrized Aliases
You can also create aliases with parameters, which allows you to expand an aliases’ functionality. Aliases with parameters are defined as normal functions in most programming languages, followed by the command and its corresponding parameters. The general syntax is as:
command $param $param2
}
For example, an alias that search the man page and grep for a specific information.
search_man() {
man $1 | grep -- $2
}
In the above alias, we can call the command man, followed by the name of the tool we want the manual for and the specific information we want to grep.
For example, to grep for the resume option in wget, we can use the command:
Parameterized aliases are very flexible and customizable to suit a wide selection of needs.
Bonus
ZSH customization and alias creation can take some time, making it unsuitable for a quick config. If you are looking for a quick setup with themes, plugins, and aliases, consider using oh-my-zsh.
The following link shows some of the aliases that come bundled with oh-my-zsh for easier workflow. Have fun.
Conclusion
Aliases are a key part of using a shell, and ZSH provides you with high-level customization for the aliases you can create. Experiment and create personal aliases for an easier workflow.
Thank you for reading.