Whta is .zshrc file on Mac?

ZSH Meaning

The Z Shell, commonly known as ZSH, serves as an expanded iteration of the Bourne Shell (sh). It incorporates numerous new features, offers accessibility to themes, and provides support for plugins. If you’ve been accustomed to using Bash on your MacOS, transitioning to ZSH should be seamless as it shares the same shell foundation as Bash.

Positioned as an advancement over Bash, ZSH boasts notable features such as:

  1. Plugin and Theme Support: ZSH now accommodates various plugin frameworks, enhancing its functionality.
  2. Spelling Correction: ZSH can rectify minor typing errors, and it also features approximate completion.
  3. Automatic cd: Simply typing the directory name enables automatic navigation to that directory.
  4. Recursive Path Expansion: An illustrative example of ZSH’s recursive path expansion is that ” /u/lo/b” expands to ” /usr/local/bin”.

What is a .zshrc file?

The .zshrc file, found on any MacOS where a user has created it (as these files aren’t generated by the shell by default), serves as a Z-Shell or ZSH resource file. It essentially functions as a script that executes each time you initiate the ZSH shell. The primary purpose of this file is to establish the environment for interactive shells. This encompasses the configuration of paths in ZSH, any initializations desired during shell startup, and related settings, all consolidated within the ~/.zshrc file. The tilde (~) denotes the file’s location in the current user’s home directory, while .zshrc specifically refers to the file itself.

How to create a .zshrc file?

Given that macOS doesn’t automatically include the ~/.zshrc file, which is the ZSH configuration file, you’ll need to create it manually. The /.zshrc file resides in your user’s home directory, denoted by the ‘‘.

To initiate the creation of the .zshrc file, open a Terminal or iTerm window and follow the steps outlined below. You can choose any text editor you’re comfortable with; for this process, we’ll use nano.

Open a Terminal window and execute the command:

  • nano ~/.zshrc

In the opened nano editor, set the ZSH_THEME value to your preference. For example:

  • ZSH_THEME=”yoodleyblog”

To save your changes, press CTRL + X, prompting the question:

  • Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES)?
  1. Type ‘Y’ to confirm, and you’ll see a new prompt. Take note of the file path corresponding to your local user’s path.
  2. Press the return key to save the file and return to the command line prompt in the Terminal. You can now exit the Terminal since the changes should be loaded.
  3. Close the terminal and open a new window. You should observe the ~/.zshrc settings reflected in the new window.

This process ensures the creation and customization of your .zshrc file, enabling the desired configuration for your ZSH shell.

Where is the .zshrc file located on Mac?

If you’ve already generated a .zshrc file in your ZSH shell, finding it is a straightforward process. Here’s a guide on locating your .zshrc file on MacOS:

  1. Open Spotlight Search.
  2. Type “Terminal” or an equivalent and launch it.
  3. Enter the command cd ~ to navigate to your user folder.
  4. Then, input ls -a to display all files, including hidden ones.
  5. Scan the listed files for the .zshrc file, which should be among them.
  6. To view the file’s content, use the command % cat ~/.zshrc.

If you encounter the error “cat: /Users/code2care/.zshrc: No such file or directory,” it means the file hasn’t been created. In such a scenario, refer to the previous instructions to create the .zshrc file.

How to add $PATH in a .zshrc file?

To initiate the ZSH shell on your MacOS, follow these steps:

  1. Open the Terminal application.
  2. If you are not already in the ZSH shell, type ‘zsh’ and press ‘enter’.
  3. Now, you need to include details for your $PATH variable. For example, use the following command: export PATH="$PATH:/opt/homebrew/bin/"
  4. To save the changes and exit the text editor (assuming it is Nano), press CTRL + X, followed by ‘Y’.
  5. Apply the changes in your .zshrc file by typing the command: source ~/.zshrc.

Different files in ZSH and their operative chronology

If you’ve recently transitioned to a newer MacBook, you’ll notice that your ~/.bash_profile is replaced by the improved ~/.zprofile, complemented by an additional ~/.zshrc file.

Referencing the ZSH documentation reveals several files situated in the home directory, denoted as . $HOME or ~/. These files include:

  • .zprofile: The login shell for ZSH
  • .zshenv: Handling environment variables in ZSH
  • .zshrc: The interactive shell in ZSH
  • .zlogin: Serving as the login shell (equivalent to .zprofile)
  • .zlogout: Executed when the shell exits

The question arises about how to configure these files in the correct sequence and which Startup/Shutdown files are essential when establishing the ZSH shell environment on MacOS.

Let’s delve into the practical configuration of these files and understand their significance within the ZSH environment on MacOS.

.zprofile and .zlogin

.zlogin and .zprofile serve the same purpose of setting the environment for login shells, with the only distinction being the timing of their loading.

.zlogin is influenced by CSH’s login, while .zprofile draws inspiration from Bash’s .bash_profile. The choice between them is flexible, but given Bash’s historical prominence until Mojave, opting for .zprofile is recommended.

.zshrc is the immediate successor to .zprofile, focusing on configuring interactive shell environments. It’s where you handle parameters like $PATH, $PROMPT, aliases, and functions—essentially, “set it and forget it” elements applicable to both login and interactive shells.

.zshenv, an optional file, caters to advanced users, primarily dealing with environment variables. It is read first and on every file read, making it crucial for non-interactive shells. While optional, it becomes essential for scripting tasks involving variables like $PATH, $PAGER, or $EDITOR, especially if utilized by launchd.

.zlogout, also optional, proves beneficial during logout sessions, facilitating cleanup tasks such as resetting the Terminal Window Title upon exit.

For clarity, here’s the sequence in which these files are read in the ZSH shell, with the system-wide file taking precedence before reading from the user’s home directory:

.zshenv → .zprofile → .zshrc → .zlogin → .zlogout

How to fix “zsh: command not found”?

When encountering the “zsh: command not found” error in ZSH, resolving it involves exploring various potential causes before implementing specific solutions. Hence, it’s crucial to initially identify the diverse reasons that could lead to the occurrence of this error.

Why does the error occur?

Beyond finding a quick fix for the error, understanding the root cause is crucial. The most common reasons behind the “zsh: command not found” error in ZSH on your Mac include:

  1. Incorrect Command Syntax: The command syntax was entered incorrectly, such as being misspelled or invalid.
  2. Incomplete or Incorrect $PATH: The user’s $PATH is incomplete, or $PATH has been erroneously set, reset, or cleared, which is a frequent cause for the ‘command not found’ message.
  3. Command Not Installed: The command you are trying to run is not installed on your system.
  4. Deleted or Modified System Directory: The command was deleted, or worse, the system directory was deleted or modified.

To address the “zsh: command not found” error:

  • Command Not Installed: If the error is due to the command not being installed, use HomeBrew to install it. Homebrew packages include useful Unix commands like wget and htop.
  • Missing System Directory: If a system directory is missing due to accidental deletion, restore the missing system files from a backup or consider reinstalling the system software.
  • Invalid or Misspelled Command: If an invalid or misspelled command is executed, correct the command or use the correct syntax.
  • $PATH Environment Issue: Ensure that the external command package is installed and set in the $PATH environment variable. Also, verify that the command path is set in the .zshrc file for permanent access.

Understanding and addressing these different scenarios will help resolve the “zsh: command not found” error effectively.

How to customise ZSH with open source tools?

ZSH offers unparalleled customization capabilities compared to Bash, and below, we outline the steps to personalize ZSH using open-source tools:

Step 1: Install Oh My Zsh

Oh My Zsh is a community-driven framework for managing Zsh configuration. It comes with numerous helpful functions, plugins, and themes. Install it with the following command:

$ sh -c “$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)”

Step 2: Install Powerlevel10k Fonts

Powerlevel10k is a MIT-Licensed Zsh theme. To install Powerlevel10k, you’ll need to install custom fonts. Visit Powerlevel10k’s GitHub page and locate the fonts in the README section. The installation process varies for different operating systems (Linux, Windows, and macOS), typically involving a straightforward setup with a few clicks.

Step 3: Install the Powerlevel10k Theme

Execute the following command to install the Powerlevel10k theme:

git clone –depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k”

After the installation, open the ~/.zshrc configuration file using a text editor like Vim. Add the line ZSH_THEME="powerlevel10k/powerlevel10k" and save the file.

Step 4: Finalize Your Powerlevel10k Setup

Open a new terminal. If you don’t see the Powerlevel10k configuration wizard, run p10k configure to launch it. Confirm that icons and symbols display correctly if all steps were followed accurately. Finally, change the default font to MesloLG NF for the complete setup.

Read About: Now.gg Clash Royale


Q. How to create and delete files in ZSH?

In the Terminal, you can effortlessly create and delete files and folders with specific names and types using simple commands.

  • To create an empty file with a specified name and type, use the command touch FileName.type.
  • For creating a new folder, employ the command mkdir FolderName.
  • To delete a file located in the current directory, use rm FileName.type.
  • If you wish to remove an empty folder from the current directory, use the command rmdir FolderName.

It’s essential to exercise caution, as deleted files and folders are irrecoverable. Deleting the wrong file or folder can lead to severe consequences for your system.

For removing a folder along with all its contents, the command rm -r FolderName can be utilized. Remember to handle these commands with care to avoid unintended consequences.

Q. How to copy and move files and folders in ZSH?

In ZSH, copying and moving files and folders involve specifying a source path and a target path.

  • To copy the “index.html” file from the “project” source path to the “new-project” target path, use the command cp /project/index.html /new-project/.
  • For copying the entire “project” folder, including its contents, to the “new-project” target folder, use the command cp -r /project/ /new-project/.
  • To directly move the “index.html” file to the “new-project” folder, utilize the command mv /project/index.html /new-project/.
  • For renaming the “index.html” file within the “project” folder to “index2.html”, use the command mv /project/index.html /project/index2.html.

Q. How does basic navigation in ZSH work?

The pwd command displays the current directory path.

The ls command lists the folders and files in the current directory.

The cd command is used to access different directories.

cd / takes you to the root directory.

cd ~ returns to the home directory.

cd /Users/ navigates to the users’ directory.

cd FolderName/OtherFolderName moves to the specified folder path.


Leave a Comment