Zsh Profile

Projected Time

About 1 hour

Prerequisites

Here are links to lessons that should be completed before this lesson:

Motivation

With knowledge about dot profile, you can customize your computer’s environment.

The .zshrc`* file is a personal initialization file for configuring the user environment (Sample .zshrc file/template). Below are a few things that make configuring .zshrc easier and faster:

Objectives

Participants will be able to:.

Materials

Video tutorials The following video tutorials below are the same with zsh, but create these settings by editing the .zshrc file. - Learn Zsh in 80 Minutes - Oh My Zsh - Command Line Power User - Zsh: Syntax Highlighting, vi-mode, Autocomplete, more - Command Line Power User: Free online video tutorial course using Zsh.

Lesson

Apart from having a home directory to create and store files, users need an environment that gives them access to the tools and resources. When a user logs in to a system, the user’s work environment is determined by the initialization files. These initialization files are defined by the user’s startup shell, which can vary depending on the release. The default initialization files in your home directory enable you to customize your working environment.

Environmental Variables

Environmental variables are available whenever you open up a terminal shell. Your system already includes many useful ones. For example $HOME. You can see that it is the path to your user’s home directory if you “echo” it. Type

echo "$HOME"

and you’ll see something like /Users/david. You can combine the variable with other commands like

cd $HOME

to change to your home directory. You can add your own environmental variables. Make a directory called scripts in our home directory.

mkdir $HOME/scripts

. Open ~/.zshrc and add the following: export SCRIPTS="$HOME/scripts". The export command is saying that you want to make SCRIPTS available anytime time .zshrc file is loaded. Since .zshrc is loaded each time you open up new terminal shell its always available. Close the shell and open a new terminal shell. To use an environmental variable you need to prepend it with the dollar sign. Change your directory to scripts using your environmental variable:

cd $SCRIPTS

The $PATH Environmental Variable

The PATH environmental variable is a collection of files that are always available without needing to source them directly. Type the follow command and hit enter:

echo $PATH

You will see a list of paths separated by a colon (you may have different results):/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin. A useful thing to do is add files to your PATH environmental variable so they are available at anytime.

echo "HELLO WORLD"
chmod a+x ~/scripts/hello_world
hello_world

and you will see HELLO WORLD. It doesn’t matter where you are at in the file system because you’ve added the file’s directory, scripts to your path so its always available to you. Lots of helpful libraries are written using scripts in this way.

Aliases

Please refer to (Moving to zsh: Alias and Functions)[https://scriptingosx.com/2019/07/moving-to-zsh-part-4-aliases-and-functions/] for more information.

It’s often helpful to make commands for yourself that are short cuts. Zsh aliases allows you to set a shortcut command for a longer command. For example what if you want to change to your directory but you don’t want to type cd ~/scripts. What if you could just type cdscr instead? Aliases can be defined in your .zshrc file.

An alias has the following structure:

alias [alias_name]="[command_to_alias]"

A new alias always starts on a new line with the alias keyword. You define the shortcut command you want to use with the alias name, followed by an equal sign. In quote you type the full command you want to run. This is illustrated below with the cd command being customised to cdscr.

Let’s make a file called aliases in our scripts folder, type the following:

alias cdscr='cd "$HOME/scripts"'

Next let’s add the following to our ~/.zshrc:

source "$HOME/scripts/aliases"

When we type source and a file after it, it’s saying, import all of the aliases in our file so we can call them by name no matter where we are in our file system. That’s why we do the same when we make changes to our .zshrc.

When we open a new terminal window after changing our .zshrc the system essentiall calls source .zshrc before we do anything.

Let’s test our alias. - Save & open a new Terminal window or source ~/.zshrc - type cdsrc and hit enter. You should now be in ~/scripts

Nano Tool

Nano is a Linux command line text editor. It is relatively easy to learn and use. However, it is entirely operated from the keyboard so you will have to learn some keyboard commands. The good news is that the keyboard commands are pretty quick and easy to learn. Things to to take note of when working with nano tool commands:

(Optional) For more detailed info on how to use the nano tool, visit these sites below:

Change the terminal prompt

You can change the value of your system’s environmental files to change how your termimal prompt appears.

The $PS1 variable sets what you see. Add the following to your .zshrc change what your prompt displays: export PS1="\u@\h "

Save & open a new Terminal window or source ~/.zshrc to reload this. Notice how the prompt now displays something simliar to david@Davids-MacBook-Pro now.

(Optional:) More info on - (Moving to zsh: Customizing the zsh Prompt)[https://scriptingosx.com/2019/07/moving-to-zsh-06-customizing-the-zsh-prompt/].

Common Mistakes & Misconceptions

Sometimes people think they need to have a different set of files to configure and customize like Bash and other shells. Zsh only requires one file, .zshrc, to have all these configurations.

Independent Practice

Moving to Zsh: A comprehensive guide to Zsh specifically for Mac users from the basics, configuration, to advanced scripting.

Check for understanding