Productivity is the key to success. Shaving off even a few seconds here and there can make a big difference in overall efficiency in the fast-paced world.
This tutorial will show you how to create aliases in ZSH that can boost your productivity by automatically expanding specific commands. By the end of this tutorial, you will learn to create and use aliases to save time and keystrokes.
Read on and increase productivity with ZSH aliases!
Prerequisite
This tutorial houses hands-on demonstrations. To follow along, you will need a Linux server running ZSH. This tutorial uses Ubuntu 20.04, but any modern Linux distribution should work.
Creating Simple ZSH Aliases
When you feel tired of manually typing the same commands many times over, creating an alias will do you a favor. An alias is a user-defined shortcut for a command or set of commands you can invoke like any other command.
The basic syntax for defining an alias is as follows: name
is the alias’s name, and ‘command’
is the command the alias represents.
alias name='command'
The simplest type of alias consists of a single word, like the ls
command, which lists the contents of a directory. But you can create an alias for the ls
command to include options by default.
To see how a simple ZSH alias works:
1. Run the ls
command below to list all contents of the working directory.
ls
2. Next, run the below command, which does not provide output but creates an alias called ls
that runs the ls -la
command when invoked.
💡 Just so you know, creating aliases on the terminal does not provide output throughout this tutorial.
This short alias saves you time from having to type the full ls -la
command whenever you list the contents of a directory. alias ls='ls -la'
alias ls='ls -la'
💡 When you set an alias, ensure it is long enough to be unique (at least two characters or more). For example, writing
ls
instead of justl
is a good idea. Doing so prevents accidentally expanding the alias as you type out the letter l.
3. Now, run the ls
alias without options to list all contents in the working directory, including the hidden ones.
ls
Notice below that you get more contents in the output since the ls
alias represents the ls -la
command.
Making Aliases Permanent
You have seen how the magic of running commands by only invoking a short alias works, and that is a good start. But remember, creating an alias right on the terminal is temporary and only works on the current shell session.
To make an alias permanent, add the alias to your ~/.zshrc file, which is read each time a new shell is started. As a result, any aliases defined in that file will be available in each new shell.
1. Open the ~/.zshrc file in your preferred text editor.
2. Next, add your alias to the end of the file, save the changes and close the editor.
alias ls='ls -la'
3. Run the below source
command, which does not provide output, but sources the ~/.zshrc
file to make the alias available in your current shell. source ~/.zshrc
source ~/.zshrc
4. Now, close your current shell session and open a new one.
5. Lastly, run the alias
command to list all currently defined aliases in your shell. alias
alias
As you can see below, the ls
alias you created is listed in the output. At this point, as you open a current shell, running the ls
alias interprets the ls -la
command.
Creating ZSH Suffix Aliases
If creating an alias for a command works, can you create one for a default program? Yes! A suffix alias lets you register a specific extension with one particular tool.
For example, you can use a suffix alias to tell ZSH to open all .txt files with the Neovim (Nvim) text editor instead of the default one (nano or vim).
The syntax for defining a suffix alias is as follows where:
extension
is the file extension to register.program
is the program’s name where a file opens with the registered extension.
alias -s extension=program
💡 From this point to the end of the tutorial, you can run the commands/code on the terminal or put them in the ~/.zshrc file to make them permanent.
Run the following command to create a suffix alias
called txt
. This alias sets nvim
as the default program for opening .txt files.
alias -s txt=nvim
Now, type the name of the text file and press Enter to open the file. This example opens the *zsh-alias.txt
file
zsh-alias.txt
Notice that you did not have to specify which text editor to open the text file since your alias tells ZSH that Nvim is the default text editor.
The zsh-alias.txt file opens in Nvim instead of the default text editor, as shown below.
Creating a ZSH Global Alias
The aliases you have created are only available for the current user. But if you wish to make your aliases available to other users, you must create ZSH global aliases. A global alias is just like a user alias but is available to all users on a system.
Perhaps you want all users on your system to use the same text editor. You can set a global alias for the text editor program in this case.
The syntax for defining a global alias is as follows where global_alias
is the name of the alias and path/to/command
is the command you want to run when the alias is invoked.
alias -g global_alias='path/to/command'
Run the below command to create a global alias called NANO
, which sets Nvim (/usr/bin/nvim
) as the default text editor for all users on your system.
💡 The best practice in creating global aliases is writing the alias name in all caps. Doing so makes the global aliases stand out from other aliases, especially when viewing the list of aliases.
alias -g NANO=/usr/bin/nvim
💡 Note that a global alias is aggressive, as it overwrites an alias with the same name you already specified.
Now switch to a different user, and run the NANO
alias to open a text file (*zsh-alias-global.txt*
).
NANO zsh-alias-global.txt
You can see below that the text file opens in Nvim regardless of the user logged in.
Creating Function ZSH Aliases
When dealing with a complex set of commands, more than a simple ZSH alias is required. But worry not! Instead, create a function ZSH alias that points to a specific shell function.
The syntax for defining a function alias is as follows.
alias name-of-alias="command1 commands2 ... "
Run the code below, which does not provide output, but defines a ZSH function alias (cmp
), which compares two numbers. This code uses “if-else” statements to compare the two numbers and print the result.
cmp() {
if [[ "$1" -gt "$2" ]]; then
echo "$1 is greater than $2"
elif [[ "$1" -eq "$2" ]]; then
echo "$1 is equal to $2"
else
echo "$1 is less than $2"
fi
}
Now, invoke the compare function (cmp
) alias to compare two numbers (10
and 20
).
cmp 10 20
Below, the cmp
function alias compares the numbers and prints a message result accordingly.
Unsetting or Deleting Aliases
Perhaps you no longer need an alias previously set by you or another user. If so, you can unset or delete that alias, avoiding confusion about which alias does which.
You can use the unalias
command to remove aliases in ZSH. The syntax for the unalias command is as follows. Replace name
with the name of the alias you want to remove.
To unset or delete an alias:
Run the following command to list all currently defined aliases.
alias
Note the name of the alias to delete from the list.
Listing all currently defined aliases
Next, run the unalias
command to delete your target alias, in this case, ls
. This command does not provide output, but you will later verify the deletion went through.
unalias ls
Finally, run the ls
command below to list all contents of the working directory.
ls
This time, you will see that output is much shorter since running the ls
command no longer include the -la
options by default.
Conclusion
Productive ZSH users heavily use aliases to minimize the amount of typing just to run the same commands. The good news is that, in this tutorial, you have learned how to create a ZSH alias in different types and set global aliases (system-wide).
Now, why not use ZSH aliases in your scripts to automate complex or repetitive tasks?