Whta is .zshrc file on Mac?

ZSH Meaning

ZSH, also known as the Z Shell, represents an extended version of the Bourne Shell (sh), incorporating numerous enhancements, theme accessibility, and plugin support. If you’ve been using Bash on your MacOS, transitioning to ZSH should be seamless since it shares the same shell foundation as Bash.

Positioned as an advancement beyond Bash, key features of ZSH include:

  • Plugin and Theme Support: ZSH now accommodates various plugin frameworks, offering enhanced customization options through plugin and theme support.
  • Spelling Correction and Approximate Completion: ZSH features spelling correction, automatically rectifying minor typing errors. Additionally, it includes approximate completion for improved typing efficiency.
  • Automatic cd: With automatic cd in ZSH, you can simply type the name of the directory to navigate to it without explicitly using the “cd” command.
  • Recursive Path Expansion: ZSH introduces recursive path expansion, exemplified by scenarios like “/u/lo/b” expanding to “/usr/local/bin.” This feature streamlines directory navigation with concise inputs.

What is a .zshrc file?

The .zshrc file serves as a Z-Shell (ZSH) resource file present on MacOS, specifically in instances where a user has manually created it, as these files are not generated by the shell by default. This file functions as a script that executes every time the ZSH shell is initiated. Its primary purpose is to establish the environment for interactive shells. This includes configuring paths, specifying initializations to be carried out during shell startup, and other relevant settings—all encapsulated within the /.zshrc file. Here, the tilde () denotes the current user’s home directory, while .zshrc represents the file named .zshrc itself.

How to create a .zshrc file?

As macOS does not inherently include the /.zshrc file, which encompasses the ZSH file configuration, you must create one. In this context, “/” denotes your user’s home directory. To initiate the creation of a .zshrc file, open a Terminal or iTerm window and follow the steps outlined below. Feel free to use any text editor you are comfortable with; for this process, we utilized nano.

After opening a Terminal window, execute the command:

bash
nano ~/.zshrc

In this newly created file, set the ZSH_Theme value as per your preference, for instance, ZSH_THEME=”yoodleyblog”.

To save the changes, press CTRL + X, prompting the following:

java
Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES)?

Respond with ‘Y’ to confirm, leading to a subsequent prompt. At this point, note the file path corresponding to your local user’s path.

Press the return key, saving your file and returning to the command line prompt in the Terminal. You can now exit the Terminal since the changes should be loaded.

Close the terminal and open a new window; you should observe the ~/.zshrc settings applied to it.

Where is the .zshrc file located on Mac?

Once you have successfully created a .zshrc file in your ZSH shell, locating it becomes a straightforward process. To find your .zshrc file on MacOS, follow these steps:

  • Open Spotlight Search.
  • Type “Terminal” or an equivalent term and open the terminal.
  • Type “cd ~” to navigate to your user folder.
  • Next, type “ls -a” to display all available files.
  • Among these files, locate the .zshrc file, which should be listed along with the others displayed by “ls -a.”
  • To view the content of the file, use the following command:
    shell
    % cat ~/.zshrc

If you haven’t created the file and are searching for it, you will encounter the error “cat: /Users/code2care/.zshrc: No such file or directory.” In this case, refer to the steps outlined in the earlier segment to create a .zshrc file.

How to add $PATH in a .zshrc file?

Launch the Terminal on your MacOS.

If you are not already in the ZSH shell, type “zsh” and press ‘enter’ to enter the ZSH shell.

Add details to your $PATH variable. For example:

bash
export PATH="$PATH:/opt/homebrew/bin/"

To save the changes and exit the text editor (assuming it is Nano), press CTRL + X, followed by ‘Y’.

To apply the changes in your .zshrc file, type the command:

bash
source ~/.zshrc

Different files in ZSH and their operative chronology

If you’ve recently transitioned to a newer MacBook, you may have noticed that your ~/.bash_profile is replaced entirely by the more advanced ~/.zprofile, accompanied by an additional ~/.zshrc file.

Upon reviewing the ZSH documentation, you’ll encounter several files located in the home directory denoted as . $HOME or ~/. These files include:

  • .zprofile – the login shell for ZSH
  • .zshenv – environment variables in ZSH
  • .zshrc – the interactive shell in ZSH
  • .zlogin – the login shell (equivalent to .zprofile)
  • .zlogout – executed when the shell exits

The question arises: How should you 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 functional configuration of these files and their significance within the ZSH environment on MacOS:

.zprofile and .zlogin

Both .zlogin and .zprofile establish the environment for login shells and are essentially similar. The only distinction lies in the time when they are loaded.

.zlogin is influenced by CSH’s login, whereas .zprofile is modeled after Bash’s .bash_profile. The question of which one to use arises. While either can be utilized, our recommendation leans towards .zprofile. Given that Bash was the default shell for everything until the advent of Mojave, sticking with .zprofile is considered ideal.

.zshrc

The .zshrc file is loaded immediately after .zprofile. It serves the purpose of configuring the environment for interactive shells. Typically, .zshrc is the designated space for working on parameters such as $PATH, $PROMPT, aliases, and functions—essentially, these are “set it and forget it” parameters and functions meant for both login and interactive shells.

.zshenv (Optional)

.zshenv is an optional file specifically designed for configuring environment variables. It is read first and is accessed each time a file is read. This file caters more to advanced users, hence its optional designation. Its relevance comes into play when essential variables like $PATH, $PAGER, or $EDITOR are crucial for scripts executed by launchd. If these variables aren’t pivotal for your use case, you can comfortably skip .zshenv.

Notably, since the mentioned variables operate under non-interactive shells, .zprofile or .zshrc won’t load without .zshenv. A workaround, if you choose not to use .zshenv, is to manually set your $PATH variables in the script. An additional benefit of this approach is enhanced portability.

.zlogout (Optional)

While .zlogout is not obligatory, it proves to be highly beneficial. This file is accessed during the logout of a session and is particularly useful for cleanup tasks, such as resetting the Terminal Window Title upon departure.

Order of Operations

The sequence in which the ZSH shell reads the following files is outlined below. It’s important to note that these files are initially read from the system-wide file and subsequently from the files located in your home directory.

  • .zshenv
  • .zprofile
  • .zshrc
  • .zlogin
  • .zlogout

How to fix “zsh: command not found”?

If you encounter the “zsh: command not found” error in ZSH, resolving it depends on identifying the specific cause of the error. Therefore, let’s first explore the various reasons that might lead to this error before delving into the solutions.

Why does the error occur?

Understanding the cause of the error is more crucial than finding a quick fix. The most prevalent reasons behind encountering the “zsh: command not found” error in ZSH on your Mac include:

  • Incorrect command syntax, such as misspelled or invalid input.
  • An incomplete or erroneously set, reset, or cleared $PATH for the user, which is a frequent cause of the ‘command not found’ message.
  • The absence of the installed command you are attempting to run.
  • Deletion of the command or, more critically, modifications to the system directory.

Fixing “zsh: command not found”:

Encountering a “Command not found” error can stem from various issues, and addressing them requires different approaches:

  1. Command Not Installed? Use Homebrew: If the command is not installed on MacOS, the Homebrew package manager can be a solution. Homebrew provides packages with utilities like wget and htop. Installing Homebrew grants easy access to a variety of command-line tools.
  2. Missing System Directory? Restore System Files: Accidentally deleting a system file is common, often occurring during experiments with RM/SRM commands or wildcards. To fix this error, restore the missing system files from a backup created before the experiment, or reinstall the system software.
  3. Invalid Command Execution: Example:
    bash
    % cls
    zsh: command not found: cls

    Executing an invalid command, such as ‘cls’ in the example, results in the error. Ensure the command is valid and recognized by the system.

  4. Misspelled Command: Example:
    bash
    % gerp ‘myfile.txt’
    zsh: command not found: gerp

    A misspelled command, like ‘gerp’ instead of ‘grep,’ leads to the error. Verify the correct spelling of the command.

  5. Installed Command Not in $PATH: Example:
    bash
    % mvn
    zsh: command not found: mvn

    Even if the external command package is installed, the error may occur if it’s not set in the $PATH environment variable. Adjust the $PATH to include the command package.

  6. Command Path Not Set in .zshrc File: The $PATH variable should be set permanently in the .zshrc file to ensure its availability beyond the current Terminal session. This prevents the “Command not found” error.

How to customise ZSH with open source tools?

ZSH offers exceptional customization support, surpassing Bash in this aspect. Below are the steps to customize ZSH using open-source tools:

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

bash
$ 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 need to install custom fonts from your terminal. Download the Custom Fonts from the Powerlevel10k GitHub by referring to the README section. The installation process may vary for different operating systems like Linux, Windows, and Mac OS, but it is generally a simple procedure.

Step 3: Install the Powerlevel10k Theme Run the following command to install the Powerlevel10k theme:

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

Once the installation is complete, use a text editor like Vim to open the ~/.zshrc configuration file. Add the line ZSH_THEME="powerlevel10k/powerlevel10k" and save the file.

Step 4: Finalize Your Powerlevel10k Setup Open a new terminal. If the Powerlevel10k configuration wizard doesn’t appear, run p10k configure to launch it. If you have followed all the steps correctly, the icons and symbols should display correctly. Make sure to change the default font to MesloLG NF for the complete setup.

 

Leave a Comment