Learning Zsh - Oh My Zsh!

Chanyang·2024년 10월 21일

Let's configure and learn more about zshell and Oh My Zsh!

After installing zsh and checking it's version type via

zsh --version

I then wanted to configure a theme. Currently I am running dracula theme across the board on my IDEs.

What is Oh My Zsh and what does it have to do with zsh?

Zsh is a shell, just like bash or fish, which interprets commands and runs them. Oh My Zsh is a framework built on top of zsh that is structured to allow it to have plugins and themes, as well as providing what we think are the best settings from the start. You can use zsh without Oh My Zsh, but you can't use Oh My Zsh if you don't have zsh.

I then had a question...

How do Zsh Configuration files work?

I wanted to get used to navigating through terminal as I love to type and access directories faster than clicking through a mouse.

"The more I type, the more I practice, proficiency will come with time"

Where to start?

According to FreeCodeCamp's article on Zsh configuration files...
Zsh config files are kept in the user's home directoy and are named with a dot as the first character to keep them hidden by default

Zsh recognizes four different configuration files in the user's home directory

~/ .zshenv
~/ .zprofile
~/ .zshrc
~/ .zlogin

I will understand the differences between the files

How is the Shell used?

Let's consider various shell uses
1. interactive
2. non-interactive

login or non-login

  1. On macOS, each new terminal session is treated as a login shell, so opening any terminal window starts an interactive login session. Also, a system administrator who connects to a remote server via SSH initiates an interactive login session.
  2. If a terminal window is already open and you run the command zsh to start a subshell, it will be interactive and non-login
  3. automated shell scripts run without login or any user prompting. These are non-interactive and non-login
  4. few people ever encounter a non-interactive login shell session. This would require starting a script with a special flag or piping output of a command into an SSH connection

How do the Configuration Files Work?

The use cases above necessitate different shell configurations, which explains why Zsh supports four different configuration files. Here's how the configuration files are used:

  • ~/ .zshenv : This is loaded universally for all types of shell sessions(interactive or non-interactive, login or non-login. It is the only configuration file that gets loaded for non-interactive and non-login scripts like cron jobs. However, macOS overrides this for PATH settings for interactive shells.
  • ~/ .zprofile: Loaded for login shells (both interactive and the rare non-interactive sessions). MacOS uses this to set up the shell for any new terminal window. Subshells that start within the terminal window inherit settings but don't load ~/ .zprofile again.
  • ~/ .zshrc: Loaded only for interactive shell sessions. It is loaded whenever you open a new terminal window or launch a subshell from a terminal window.
  • ~/ .zlogin: Only used for login shell configurations, loaded after .zprofile. This is loaded whenever you open a new terminal window.

How to Use Each File

Let's consider which configuration files I should use

  • ~/ .zshenv: It is universally loaded, so you could use it to configure the shell for automated processes like cron jobs. However, it is best to explicitly set up environmental variables for automated processes in scripts and leave nothing to chance. As a beginner, I will not use this configuration file. Few experienced macOS developers use it.
  • ~/ .zprofile: Homebrew recommends setting the PATH variable here. There's a reason PATH should be set in ~/ .zprofile and not the universal ~/ .zshenv file: the macOS runs a utility path_helper(from /etc/zprofile) that sets the PATH order before ~/. zprofile is loaded.
  • ~/ .zshrc: This is the configuration file that most developers use. Use it to set aliases and a custom prompt for the terminal window. You can also use it to set the PATH(which many people do) but ~/ .zprofile is preferred.
  • ~/ .zlogin: This is rarely used. Only important in managing the order of initalization tasks for login shells in complex environments. It can be used to display messages or system data.

How to Avoid Complications

These configurations may appear complicated. It made sense in the early days of computing to start time-consuming processes at login and not have them repeat when a new terminal was launched.

MacOS now launches any new terminal window as a login shell, loading both ~/ .zprofile and ~/ .zshrc files without concern for the shell startup time. So why not use one Zsh configuration file? Answer is a bow to history, plus configuration customization for the experts.

The key advantage of the ~/ .zprofile file (versus ~/ .zshenv) is that it sets environment variables such as PATH without override from macOS. The ~/ .zshrc file could be used for the same but, by convention and design, is intended for customizing the look and feel of the interactive terminal.

Keep It Simple

Simple guidelines, current best practice is as follows

  • Use ~/ .zprofile to set the PATH and EDITOR environment variables.
  • Use ~/ .zshrc for aliases and a custom prompt, tweaking the appearance and behavior of the terminal.
  • If you write automated shell scripts, check and set environment variables in the script.

Understanding the zshrc File

the zshrc file is a configuration file for Zsh, a popular shell used in Unix-like operating systems, including Mac OS. It is read everytime a new terminal session is started, which means any changes you make to this file will be applied to all future terminal sessions.

By default, the zshrc file is located in your home directory. If it does not exist, you may need to create a new zshrc file.

The zshrc file can be used to customize various aspects of your terminal environment. For instance, you can change the command prompt, set environment variables, define aliases for long commands, and much more

It is important to understand that incorrect modifications can cause issues with your terminal. Always make sure to back up current zshrc file before making any changes.

Finding the zshrc File

Using the Terminal:Open the terminal and type the following command:
ls -a ~
This command lists all files and directories in your home directory, including hidden ones. If the zshrc file exists, you should see it in the output.

Opening the zshrc File

You can open the zshrc file with any text editor, such as nano, vim, or even a GUI-based editor like Sublime Text or Visual Studio Code.

I will open it with nano:
nano ~/ .zshrc
If the file does not exist, this command will create a new one.

Editing the zshrc File: Once you have opened the zshrc file, you can start customizing your terminal environment. Here are some examples of what you can do:

  • Changing the Command Prompt: The command prompt is the text that appears before each command you type in the terminal. By default, it shows the name of your computer and the current directory. You can change it to anything you like by adding the following line to your zshrc file:
    PROMPT="My Prompt >"

Saving and Applying Changes

After editing the zshrc file, save your changes and exit the text editor. If you are using nano, press Control + X, then Y, and Enter.

To apply the changes, you need to restart your terminal or run the following command:
source ~/.zshrc
This command tells Zsh to read and execute the commands in the zshrc file

Conclusion

Editing the zsrhc file is a powerful way to customize my terminal environment on Mac as I see fit. If I want to change the command prompt, set the environment variables, or define aliases for long commands, I can edit the zshrc file.

profile
Internet-taught Frontend Dev

0개의 댓글