Whta is .zshrc file on Mac?

Whta is .zshrc file on Mac?

Asenqua Tech is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

ZSH Meaning

ZSH or the Z Shell is an extended version of the Bourne Shell (sh), with the incorporation of myriad new features, accessibility to themes and support for plugins. If you had been using Bash on your MacOS so far, switching to ZSH should be a breeze since it is based on the same shell as Bash. 

Coming off as an upgrade to Bash, ZSH’s major feature highlights are:

  • Plugin and theme support, which now allows ZSH to include different plugin frameworks.
  • Spelling correction which allows ZSH to fix minor mistakes while typing. ZSH also has approximate completion. 
  • Automatic cd, which allows you to just type the name of the directory.
  • Recursive path expansion. An example of recursive path expansion in ZSH would be: “/u/lo/b” expands to “/usr/local/bin”

What is a .zshrc file?

A .zshrc file is a Z-Shell or ZSH resource file located on any MacOS wherein the user has createda .zshrc file (since these files are not created by the shell by default). The file is more or less a script that runs whenever you start the ZSH shell. The main function of the file is to set the environment for interactive shells. By this, we mean,all paths you have set in ZSH, the initializations you want performed at the start-up of the shell, etc., are put in ~/.zshrc. The ~ is a file located in the current user’s home directory, whereas the .zshrc is the file called .zshrc, itself.

How to create a .zshrc file?

Since  ~/.zshrc (includes the .zshrc file) does not exist by default in macOS, you will need to create one.  In ~/.zshrc, ~/ simply refers to your user’s home directory and the .zshrc is the ZSH file configuration itself. 

To create a .zshrc file, you will need to open up a Terminal or iTerm window and create a file using the steps mentioned below. You can use whichever text editor you feel comfortable with; we used nano for the procedure.

Once you have created a window in the Terminal, use the command:

nano ~/.zshrc

Now, set the ZSH_Theme value according to your liking, for example, ZSH_THEME=”yoodleyblog”

To save these changes made, hit CTRL + X, which in turn will prompt the following:

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

Type ‘Y’ in the prompt and you will receive a new prompt. Once that happens, note the file path that shall match with your local user’s path.

Hit the return button and your file will now be saved. This will also take you back to the command line prompt in the Terminal which you can exit since the changes should be loaded by now.

Exit the terminal and open a new window and you should see the ~/.zshrc settings loaded onto it.

Where is the .zshrc file located on Mac?

Given that you have already created a .zshrc file in your ZSH shell, locating it is an easy task. 

Follow the steps below to locate your .zshrc file on your MacOS:

  • Open Spotlight Search
  • Type Terminal or an equivalent of the same and open it.
  • Now, type cd ~. Typing this command will move to your user folder/
  • Next, type ls -a, which will show you all the files available.
  • From amongst these files, look for the .zshrc file, which should be ;listed amongst the rest of the files that are displayed upon typing ls -a.
  • To view the content of the file, type the following command: 

% cat ~/.zshrc

  • If you haven’t created the file and are looking for it, you will get the error “cat: /Users/code2care/.zshrc: No such file or directory”. 
  • In this case, follow the steps mentioned in the segment above to create a .zshrc file.

How to add $PATH in a .zshrc file?

  • Open Terminal on your MacOS
  • In the Terminal, type zsh followed by the ‘enter’ button to enter the ZSH shell if you are not on the ZSH shell already.
  • Next, you are required to add your $PATH variable details. An example for the same would be: export PATH=”$PATH:/opt/homebrew/bin/”
  • To save the file and exit the text editor (considering it is Nano), press CTRL + X, followed by ‘Y’.
  • Next, you need to type in the command source ~/.zshrc in order for changes to be applied in your .zshrc file.

Different files in ZSH and their operative chronology

If you have recently switched to a newer Macbok, you already know that your ~/.bash_profile will be completely replaced by the better ~/.zprofile with an added ~/.zshrc file. 

When you look at the ZSH documentation , you will come across a number of files that are located in the home directory that goes as . $HOME or ~/. These files are:

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

The question is how should you configure these files in the right chronology and what Startup/Shutdown files should be used when setting up the ZSH shell environment?

Let’s look at the operative configuration of these file and what these files mean in ZSH on MacOS: 

.zprofile and .zlogin

.zlogin and .zprofile set the environment for login shells, and are practically more or less the same thing. The only difference that sets them apart is the time when they get loaded.

.zlogin is a derivative of CSH’s login, while .zprofile is based on the Bash’s .bash_profile. Which one should you use? Although you can use either of them, we are of the opinion that since Bash used to be the default shell for everything until the coming of Mojave, it is ideal to stick with .zprofile.

.zshrc

.zshrc file is the file that gets loaded right after .zprofile. Setting the environment for interactive shells, .zshrc is typically a path where you work on parameters such as $PATH, $PROMPT, aliases, and functions; in gist, “set it and forget it” parameters and functions that you would like to have in both login and interactive shells.

.zshenv (Optional)

.zshenv is an optional file where you set environment variables. It is read first and read every time file and is geared more towards advanced users. This is why we call it optional. If having a $PATH, $PAGER, or $EDITOR variables are important for scripting that get called by launchd, .zshenv is an important file, otherwise you would be fine overlooking it.

Since the variables mentioned above run under non-interactive shells, anything with .zprofile or .zshrc won’t be loaded without .zshenv. One workaround for not using .zshenv would be to set your $PATH variables in the script manually. One added advantage of doing this is portability.

.zlogout (Optional)

Although .zlogout is optional, it is a very useful file. The file is read when you log out of a session and works great for cleaning up, such as resetting the Terminal Window Title when you leave.

Order of Operations

Below we have enlisted the order of operation or the order in which these files are read in the ZSH shell. 

Note: These files are first read from the system-wide file and then followed by reading from the files present in your home directory.

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

How to fix “zsh: command not found”?

If you are facing the “zsh: command not found” error in ZSH, there are multiple ways to fix it depending on what really caused the error. Thus, let us first understand the multiple reasons that could be the source to the occurence of the error, before we get to the solutions.

Why does the error occur?

More than getting an easy fix to the error, it is important to know what causes the error. The most common reasons why you are facing “zsh: command not found” error in ZSH on your Mac are:

  • the command syntax was entered incorrectly, as in misspelled or invalid
  • the users $PATH is incomplete, or $PATH has been erroneously set, reset, or cleared – this is the most common reason to see a ‘command not found’ message
  • the command you are attempting to run is not installed
  • the command was deleted, or, worse, the system directory was deleted or modified.

Fixing “zsh: command not found”:

  • “Command not found” because command isn’t installed? Use HomeBrew

“Command not found” error is very common and it basically occurs because the command is not installed in the MacOS. Homebrew packages come with wget, htop and other useful Unix commands, so it is recommended to install Homebrew to fix this error. You can get easy access to command line utilities if you spend time in the Terminal.

  • “Command not found” because a system directory is missing? Restore the missing system files

Deleting a system file from MacOS is quite a common and frequent mistake that happens with the users experimenting with the RM/SRM commands or a Wildcard. 

To fix this error a user just needs to restore from a backup that they must have created before performing an experiment, or else they have to reinstall the system software itself.

  • Executed an invalid command

For example:

% cls

zsh: command not found: cls

In the above example, cls is used but it is not a command that is used to clear the screen in Linux/ Mac OS based operating systems. Hence you get the error for an invalid command.

  • Executed command is misspelled, 

For example:

% gerp ‘myfile.txt’

zsh: command not found: gerp

In the above example command grep is misspelled as gerp which causes the error.

  • The command package is installed and not set in the $PATH environment. 

For example:

% mvn

zsh: command not found: mvn

In the above example the user gets the error even if the external command package is installed, because it is not set in the $PATH environment variable. Hence it is necessary to set an external command package in the $PATH environment variable to fix the error.

  • The command path is not set in the .zshrc file.

One needs to set the $PATH variable permanently, in the .zshrc file, otherwise the $PATH variable will be available only till the Terminal session.

How to customise ZSH with open source tools?

One of the greatest perks offered by ZSH over Bash is the customisation support. Below we have enlisted the steps you can use to customise ZSH with open source tools:

  • Step 1: Install Oh My Zsh

Oh My Zsh, an open source community driven framework for managing Zsh configuration, it comes with a number of helpful functions, plugins and themes.

You can install it with the following:

$ 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; in order to install Powerlevel10k, you need to install custom fonts from your terminal. 

To download the Custom Fonts, go to Powerlevel10 GitHub and search for fonts in the README section. The process of installing custom fonts varies for different operating system links Linux, Windows and Mac OS. Mostly the installation process is just a matter of a few simple clicks. 

  • Step 3: Install the Powerlevel10k theme

Run the following command:

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

Once it’s done, use a text editor like Vim to open a ~/.zshrc configuration file and set a line ZSH_THEME=”powerlevel10k/powerlevel10k .

Finally, save the file. 

  • Step 4: Finalize your Powerlevel10k setup

Now open a new terminal; if you don’t see the Powerlevel10k configuration wizard just run p10k configure to get the configuration wizard.

The icons and symbols will show correctly if you have followed all the steps correctly. Now you just need to change the default font to MeslowLG NF.

 

FAQs

Q. How to create and delete files in ZSH?

Files and folders with specific name and type can be created, and deleted in the Terminal. 

touch FileName.type creates an empty new file with specified name and type.

mkdir FolderName it will create a new folder with the name given in command.

rm FileName.type this command will delete the file referred to the current folder.

rmdir FolderName it will revive the empty folder from the current directory.

Remember that the deleted files and folder cannot be recovered later, so be careful before deleting something. Deleting a wrong file or folder can cause serious damage to your system.

rm -r FolderName command can remove a folder with all the files and folders it contains.

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

Copying and moving files and folders in ZSH requires a source path and a target path.

cp /project/index.html /new-project/ command will copy the “index.html” folder from the source path “project” to the target path “new-project”.

cp -r /project/ /new-project/ command will copy the whole “project” folder along with the folders and files in it to the new folder “new-project”.

mv /project/index.html /new-project/ command will directly move the “index.html” file to the “new-project” folder.

mv /project/index.html /project/index2.html command will rename the “index.html” file.

Q. How does basic navigation in ZSH work?

pwd command prints our current path.

ls command shows the list of folders and files in a current directory.

cd command can give access to other directories.

cd / command can navigate to the root directory.

cd~ command brings back to the home directory.

cd /Users/ command will navigate to the users directory.

cd FolderName/OtherFolderName command will navigate to the corresponding folder.

 

Similar Posts

1,183 Comments

  1. Pharmacie en ligne sans ordonnance [url=http://pharmacieenligne.icu/#]Pharmacies en ligne certifiГ©es[/url] Acheter mГ©dicaments sans ordonnance sur internet

  2. online BlackJack Five Star Casino Rentals is open Mon, Tue, Wed, Thu, Fri, Sat, Sun. We’ve got themed slots to fit your every mood–over 1,300 state-of-the-art machines–that’s over 1,300 different ways to win, and win big! No, although early in the history of Rising Star it did sail, it is now permanently docked. As with other Indiana riverboat casinos, the Rising Star used to set sail (as the Grand Victoria II), but that is no longer the case. In 2002, Indiana lawmakers passed legislation allowing riverboat casinos to remain docked. How is Five Star Casino Rentals rated? Blackjack is the most well-known game in the casino and is played around the world. It remains one of the most popular table games in any casino. Everyone at the table plays against the dealer. When dealt 2 cards of the same value, players may split the pair to become separate hands and receive additional cards on each hand. The objective of the game is to draw cards with a total closer to 21 than the dealer’s total – without going over 21.
    http://star-wiki.win/index.php?title=Jocuri_pacanele_gratis_dice
    Jackpot Wheel falls under the category of the so-called hybrid casinos. Along with the standard payment methods, it also accepts bitcoin. The minimum deposit is $20. The minimum withdrawal is $100, which is a bit on the higher side given the fact that most online casinos allow players to withdraw a minimum of $10 or $20 at worst. The online casino has limited and straightforward banking options. Their game catalog contains more than 200 online casino games ranging from video slots, video poker to table games, live casinos. Jackpot Wheel Casino is a tested-and-proved entertainment packed and bonus filled casino that has something for every gambler regardless of whether you are a beginner, intermediate or expert gambler. The casino has an amazing collection of state-of-the-art games that are all stacked in your favour. The casino is reputable amongst the top gamblers as a site for high rollers who like winning it big.

  3. medication from mexico pharmacy [url=http://mexicanph.com/#]mexican rx online[/url] mexican border pharmacies shipping to usa

  4. تقدم شركة كايرو هايتس مجموعة من أكبر المشروعات السكنية المتميزة وشقق للبيع بالقاهرة الجديدة والتجمع الخامس والعديد من الوحدات السكنية الراقية بعقارات بيت الوطن والتى تتميز بالخصوصية والهدوء والإطلالة الفريدة على حدائق ومساحات خضراء.
    تختار كايرو هايتس بالقاهرة الجديدة مشروعاتها بعناية وبمواقع استراتيجية بحيث تكون قريبة من مناطق الخدمات والمراكز الترفيهية وأشهر النوادى والمؤسسات التعليمية الهامة.
    شقق للبيع ببيت الوطن بالتقسيط

Leave a Reply

Your email address will not be published. Required fields are marked *