Yoodley is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
When you install zsh on your system, the .zshrc file is created automatically. The dot (.) before a file’s name indicates that it is hidden and will not be viewable unless it is explicitly requested.
The command “ls -al” which lists all the contents of a directory, might be used to look at the file in your home directory. You’ll find the .zshrc file at the bottom of the list if you run this command in your file system’s home/user directory.
When a new instance of zsh is launched, the file contains scripts that are used to set up key bindings, functions, options, and other critical factors. It’s technically referred to as a startup file, and there are a total of five .zsh startup files.
However, .zshrc is the most important and is the only one in the home directory. The file may be readily opened, and the contents can be seen or modified using any text editor. The file’s content should not be changed erroneously, as stated in the file itself, otherwise zsh will be broken.
Where is the .zshrc file on MacOS?
If you are using the Zsh shell, the default shell for macOS Big Sur, and wanting to find the .zshrc file, you first need to know that this file is not produced by default; you must create one. If you believe you have already created one and wish to find it, follow these steps.
- Launch Spotlight Search.
- Enter Terminal and press Enter.
- Now, type cd ~ to navigate to your user folder.
- To see all the files, use ls -a.
- You should be able to see the.zshrc file; if you can’t, you don’t have it.
- To view the contents of a file, you only need to type one command.
% cat ~/.zshrc
- If the file is missing, you will receive the error shown below!
cat: /Users/code2care/.zshrc: No such file or directory
zshrc vs zprofile
Zprofile is also in the home directory and is one of the five zsh configuration files. The primary distinction between zprofile and zshrc is that zprofile holds information and configuration specific to the logging user, whereas zshrc contains additional scripts and bindings, options, and so on. Another distinction between the two files is that zshrc is loaded every time the zsh shell is started, whereas the zprofile file is loaded just once when the user logs in.
Reload zshrc
If you add aliases or functions to the zshrc file or update it, you must reload the file for the changes to take effect, or the aliases or functions you just added to the file will not operate in the shell. To reload the configuration file and make your changes effective, run the following command in the zsh shell.
$ source ~/.zshrc
Zshrc vs bashrc
Unlike the zshrc file, which contains the whole setup source of the zsh shell, .bashrc is a secret Bash configuration file contained in the home directory. The bashrc file is relatively tiny and does not contain any bash shell setup routines.
The bashrc file contains no additional information or scripts and is only used to set aliases and other environment variables for the bash shell. The zshrc, on the other hand, can be used to set not only aliases but also setup scripts and default environment variables.
Add functions to zshrc
Custom functions, such aliases for custom commands in terminals, can be added to the configuration file. Before you can utilise the file, you must first add the function definition and then source or reload it. The syntax of a function declaration is shown below, as well as how to autoload it into the zsh shell.
// 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 terminal shortcut commands that are used to run a specific command. The simplest basic “cd” command is an example of an alias. An empty cd command will always move your current working directory to “~/. (home/user)” without specifying the target landing address, regardless of which working directory you are in. Users can create their own aliases to meet their own needs. The aliases can be stored after being added to the file in a specific syntax. They’ll come in handy later.
How to create a zshrc file in macOS?
Although Zsh is the default shell on macOS, the zshrc file is not automatically located in the home directory. On macOS, the configuration and associated scripts are saved elsewhere. However, if you need to edit certain variables or add aliases to the zsh shell, you may build the file yourself and make the modifications, and it will operate exactly as you planned. To generate a zsh config file on macOS, follow the steps below.
- Launch the terminal.
- Check that you are in the “/ (home/user>)” directory.
- Type “touch /.zshrc” into the command line.
- In the directory, a new file will be generated.
- Open the file with your preferred text editor and save the changes.
- Use “source /.zshrc” to load the file into the shell.
zshrc add to path
PATH is a global environment variable that stores the directory locations of your system’s binary executable files. Enter the command “echo $PATH” in the shell to examine the value of the PATH variable on your Linux system, and your PATH addresses will be returned. The new PATH variable entries can be added to the zshrc file. Use the methods below to add a new address to the zsh shell path variable.
- Launch the terminal.
- Ascertain that you are in the </home/user> directory.
- Open the .zshrc file in your preferred text editor.
- “export PATH=add1>:add2>” should be added.
- Use ctrl + O to save the file.
- Using ctrl + X, exit the file.
NOTE – The colon separates two unique addresses, <add1> and <add2>. Multiple addresses can be included in a single line by separating them with a colon.