Configure and Use Aliases in Zsh

You can think of an alias as a shortcut for a command you execute!

Now, there is a variety of ways by which you can configure and use aliases on ZSH and in this tutorial, I will be covering the following types:

  • Simple aliases (to replace long command with short name)
  • Suffix aliases (to execute a specific type of file with the desired tool)
  • Global aliases (can be used by every user on the system)
  • Function aliases (useful when you want to add parameters with multiple commands)

It may sound a bit complex but don't worry; it only takes a couple of commands to configure and improve your productivity like never before.

Configure simple aliases on ZSH

In simple aliases, you can create a short form of a long command.

Such as when you want to update the repositories and upgrade outdated packages, you use sudo apt update && sudo apt upgrade -y which is pretty long.

To add an alias, first, open the .zshrc file:

nano ~/.zshrc

Jump to the end of the line in the nano text editor by pressing Alt + / and use the following syntax to add an alias:

alias [custom-command-alias]="[command]"

As I wanted to create an alias for updating repositories and upgrade packages, I will be using the following:

alias update="sudo apt update && sudo apt upgrade -y"

Save changes and exit from the nano text editor.

Now, source the .zshrc file to activate the alias:

source .zshrc

That's it! Now, you can use the alias to execute the specified command.

Here's what it did when I used update in terminal

Configure suffix aliases on ZSH

Using the suffix alias, you can specify which tool to be used with the specific file type.

Such as if you use nano to edit text files, you are always required to use nano in the beginning of the command.

But when you use suffix alias, you no longer have to specify the tool which you want to use with a file.

First, open the .zshrc file:

nano ~/.zshrc

Jump to the end of the line in nano using Ctrl + / and add the alias in following syntax:

alias -s [extension]="preferred-tool"

I want to add the nano as a suffix to open .txt, VSCode for .py and .json files so I will add the following:

alias -s txt=nano
alias -s py=code
alias -s json=code

Save changes and exit from the text editor.

To enable the alias, source the configuration file:

source ~/.zshrc

Now, I no longer have to use nano to create/open text files:

Pretty convenient. Isn't it?

Create global aliases

All the aliases you created previously (using the above guide) will only work for the current user only.

So if you want to set the alias for every user present on the system, you can create global alias.

As always, let's start with opening the .zshrc file:

source ~/.zshrc

Go to the end of the line in the configuration file using Ctrl + / and add the global alias using the given syntax:

alias -g [custom-command-alias]="[command]"

I want to create a global alias for sudo apt update && sudo apt upgrade -y so I will be using the following:

alias -g update="sudo apt update && sudo apt upgrade -y"

Save changes and exit from the text editor.

Next, source the .zshrc file to take effect from the alias you have just created:

source ~/.zshrc

For example, here, I switched to another user named ubuntu and executed the global alias I've just created:

Create function aliases on ZSH

The function alias is also known as parametrized alias where you create alias functions in the programing language and extend the usability of the alias.

As always, let's start by opening the .zshrc config file:

nano ~/.zshrc

Jump to the end of the configuration file by pressing Ctrl + / and add the function alias using the following syntax:

[alias-name]() {
    command $parameter1 $parameter2
}

Here, I created an alias that will look for a specific text string on the man page for a specified tool and named it find_man:

find_man() {
    man $1 | grep -- $2
}

Once you are done creating the function alias, save the changes and exit from the nano text editor.

Next, source the .zshrc file to enable the alias:

source ~/.zshrc

For example, here, I used the man page to look inside the man page of the kill command and searched for the SIGTERM:

The most convenient alias. Isn't it?

Want to create Aliases for bash? Here you go

If you love to bash and want to learn how you can create an alias for bash, we have a detailed guide for that purpose:

Linux Alias Command: Set, Create and Remove Aliases
Alias command in Linux saves a lot of your time and improves your efficiency. Learn how to use this command properly and see some of the common aliases I use.

I hope you will find this guide helpful.

And if you have any queries, let me know in the comments.