How to Make a Permanent Alias in Zsh?

Linux-based operating systems rely on the functionality of the shell being used, i.e., bash, sh, zsh, etc. Each Linux distribution comes with a shell that is pre-installed on it, i.e., bash in Ubuntu. While working with Linux shells, Linux users are well aware of the aliases being used. Alias refers to the short/second name of any command to serve the same purpose. The aliases can either be permanent or temporary. 

This post will illustrate the steps to make a permanent Alias in Zsh:

Prerequisite: Install Zsh on Linux

The Zsh can be installed on various Linux distributions using the commands as per their Linux distributions:

$ sudo apt install zsh                                   #For Debian/Ubuntu
$ sudo yum install zsh                                   #For CentOS/RHEL
$ sudo dnf install zsh                                   #For Fedora
$ sudo pacman -S zsh                                     #For Arch

How to Make Permanent Alias in Zsh?

To make permanent alias in Linux, go through the following easy steps:

Step 1: Switch to Zsh 

If you are into some other shell and have not switched to Zsh, use the command: 

$ chsh -s /usr/bin/sh

After that, use the following keyword to switch to zsh: 

$ zsh

The Zsh has been activated as seen in the output. 

Step 2: Open .zshrc File 

Open the “.zshrc” which is available in the home directory with any editor such as nano. This file is hidden in the directory; if the “.zshrc” file is not available, just create the file:

% nano ~/.zshrc

Step 3: Append Alias

The next step is to add the aliases in the file, such as “l” for the “ls -a” command that displays the hidden files, “d” for the “mkdir” command that creates the directory; and “update” for the “sudo apt update” command as shown:

alias l='ls -a'
alias d='mkdir'
alias update='sudo apt update'

Save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X”.

Step 4: Execute the .zshrc File

Now, execute the “.zshrc” using the source command:

% source ~/.zshrc

The “.zshrc” file is executed.

Verify the Change

To verify that the aliases are added in the file, run the “alias” command in the terminal:

% alias

The aliases d=”mkdir”, l=” ls -a” and update = “sudo apt update” are added.

Let’s execute the commands using aliases in the terminal:

% d Henry          #d is running the mkdir command
% l                #showing the hidden file% 
update           #system update command

The aliases are working perfectly.

Conclusion

To make permanent aliases in the “zsh” shell, the “.zshrc” file is utilized. Open this file with any editor, such as nano, add the aliases, and execute the “.zshrc” file. Now, instead of writing the commands, just write the alias and get the same output as any command. This write-up has illustrated the step-by-step guide to making permanent aliases in the Zsh.