zsh

How to configure and use aliases in ZSH


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:

alias <flag> <alias_name>=”command

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.

nano ~/.zshrc

Next, create aliases in the form:

alias <custom-alias>=”<command>

For example, let us set up a few aliases for working with Git Repositories:

# git aliases
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

source ~/.zshrc

Now in the terminal, to initialize a directory as a git repository, use the command:

$ ginit
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:

alias -s extension=”preferred-tool”

For example, the following aliases define two file types and the corresponding tools to open them:

# suffix aliases
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:

info.txt

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:

alias -g [alias]=”command

For example, an alias for grep command:

# global aliases
alias -g gp="grep"

You can then pipe commands to grep using the created alias:

$ ps aux | gp root
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:

<alias-name>() {
    command $param $param2
}

For example, an alias that search the man page and grep for a specific information.

# param alias
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:

search_man wget continue

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list