Zshrc File Explained in 10 Easy to Understand Points

zshrc

.Zshrc is a configuration file that contains the commands that run the zsh shell, just like the .bashrc file that contains commands for the bash shell. It contains scripts that run whenever an interactive zsh session is launched. In Linux, the file is stored in the home/user directory as a hidden file. The file can be edited to customize the zsh experience, but it is not advised to customize it. The file is also hidden and stored in the user’s home directory in macOS. The file is automatically created when the Zsh shell is installed on a system. Today, we will look at the .zshrc file in quite some detail.

What is Zsh?

Zsh is short for Z shell. It is an interactive login shell initially designed in the 1990s for the Unix operating system. The zsh is built on top of Bash and hence inherit all the original features and function of the Bash shell and also has many other of its own. It is the default shell on macOS and also comes preinstalled on some of the Linux distributions like Kali Linux but is not enabled by default. Zsh can be easily installed on all the distributions of Linux using the respective package manager. Use the following commands to install zsh on your distribution of Linux.

$ sudo pacman -Sy zsh
// Use on Arch

$ sudo apt-get install zsh
// Use on debian/ubuntu

$ sudo dnf install zsh
// Use on Fedora
zshrc installation
Installing zsh on Arch-Linux.

.zshrc file

The .zshrc file is automatically created when the zsh is installed on your system. The dot (.) before the file’s name indicates that the file is hidden and won’t be visible unless specified. You could yourself look at the file in your home directory by the command “ls -al,” which lists all the directory content. Use this command in your file system’s home/user directory, and you will find the .zshrc file at the bottom of the list.

The file contains scripts that are used to set up key bindings, functions, options, and other important parameters that are used when a new instance of zsh is launched. Technically it is known as a startup file, and there are five zsh startup files in total. But, .zshrc is of more importance and is the only one that is located in the home directory. The file can be easily opened, and its content can be viewed or altered using the text editor of your choice. As mentioned in the file itself, the file’s content should not be altered incorrectly, or it could end up breaking zsh.

Zshrc file opened on the mousepad.
Zshrc file opened on the mousepad.

zshrc vs zprofile

Zprofile is also one of the five zsh setup files and is also located in the home directory. The main difference between the zshrc and zprofile is that zprofile contains information and configuration regarding the logging user while zshrc contains other scripts and bindings, options, etc. Another notable difference between both files is that zshrc is sourced whenever a new instance of the zsh shell is launched, while the zprofile file is sources once when the user is logged in.

Reload zshrc

When you add to the zshrc file or make changes to it, you need to reload the file to make the changes effective, or the aliases or functions you just added to the file won’t work in the shell. Run the following command in the zsh shell to reload the configuration file and make your changes effective.

$ source ~/.zshrc

Zshrc vs bashrc

.bashrc is a Bash configuration file located in the home directory and is hidden, unlike the zshrc file, which contains the entire setup source of the zsh shell. The bashrc file is relatively small in size and doesn’t contain any setup scripts for the bash shell. The bashrc is only used to set aliases and other environment variables for the bash shell but does not contain any additional information or scripts. The zshrc, on the other hand, can not only be used to set aliases but also includes the setup scripts and default environment variables.

An unedited bashrc file.
An unedited bashrc file.

Add functions to zshrc

Like aliases for custom commands in the terminals, custom functions can also be added to the configuration file. You need to add the function definition in the file and then source or reload the file before you actually use it. The syntax of function definition and how to autoload it into the zsh shell are shown below.

// Syntax of function definition

function func_name(args){
    //
    // body of the function
    //
    return return_obj //return value
    }

Add the following statements to the file to make the function autoload, so you won’t have to source them every time manually.

fpath=(~/functions $fpath)       // address to file containing functions
autoload function_1 function_2   //name of the functions

zshrc file alias

Aliases are shortcut commands that are used to execute a specific command in the terminal. An example of an alias is the most basic “cd” command. No matter which working directory you are in, an empty cd command will always change your present working directory to the “~/. (home/user)” without specifying the target landing address. Users can set their own aliases as per their custom requirements. The aliases can be added to the file in a specified syntax and saved. They can be used later.

The syntax for adding aliases to the file.
The syntax for adding aliases to the file.

How to create zshrc file in macOS

Zsh might be the default shell on macOS, but the zshrc file is not found in the home directory by default. The configuration and other scripts are saved elsewhere on macOS. But if you need to change some variables or add aliases to the zsh shell, you could create the file yourself and add changes to it, and it would work just the same as you intended. Follow the given steps to create a zsh config file on macOS.

  • Open the terminal.
  • Make sure you are in the “~/ (home/<user>)” directory.
  • Enter the command ” touch ~/.zshrc“.
  • A new file will be created in the directory.
  • Open and edit the file with the text editor of your choice and save the changes.
  • Load the file in the shell, using “source ~/.zshrc

zshrc add to path

PATH is a global environment variable that holds the directory locations of the binary executable files in your system. To see the value of the PATH variable on your Linux system, enter the command “echo $PATH” in the shell, and your PATH addresses will be returned. The new entries for the PATH variables can be added to the zshrc file. Employ the following steps to enter a new address to the zsh shell path variable.

  • Open the terminal.
  • Make sure you are in the /home/<user> directory.
  • Open the .zshrc file using the text editor of your choice (we used Nano here).
  • Add ” export PATH=<add1>:<add2>
  • Write the file using ctrl + O.
  • Exit the file using ctrl + X.

NOTE – <add1> and <add2> are two distinct addresses separated by the colon. You could add multiple addresses in a single line by separating them by a colon.

FAQs on zshrc

How to launch a zsh shell from Bash?

Just enter “zsh” in the bash terminal and hit enter.

What are all the zsh startup files?

There are five zsh startup files: .zshrc, .zshprofile, .zshenv, .zshlogin, .zshlogout.

Where is the .zshrc file located in macOS?

In macOS, too, the zshrc file is located in the “~/ (home/<user>)” directory. It is hidden and is not visible unless specified in the command or file manager.

Conclusion

.zshrc is a configuration file needed to set up the zsh shell. We compared the file with similar files such as zprofile and bashrc to understand that it is far more complicated and vital than both. The file can be edited safely to utilize the zsh shell as per the user’s need, such as adding PATH addresses and aliases. Other than that, the file should be altered around and let as it is.

Leave a Comment

Your email address will not be published.

Scroll to Top