zsh

How to configure and setup zshrc for ZSH

We have all performed numerous tasks from the terminal. From OS installations, configuration and updates, to package installs and such. We have also built projects and launched them to servers running various Operating systems using a terminal. Thus, to say that using a terminal has become part of our lives would be an understatement.However, using the default terminal can get old quickly. Fortunately, you can make a few tweaks to make the terminal fit your needs and look pleasing to you. In this simple and quick tutorial, I will show you how to customize your terminal using ZSH. Let us get started:

What is ZSH?

Z shell, commonly known as ZSH, is a powerful command-line interpreter for UNIX and UNIX-Like systems. It is built on top of the popular BASH shell and is free, open-source, and updated regularly. ZSH is powerful and customizable, allowing you to perform tasks with minimal fuss and seamless workflow. It also offers a great shell scripting language with great improvements.

To find out more about ZSH, consider the official page.

Now, since there’re many other shell options to choose from, including the popular BASH, why should you choose ZSH?

Why ZSH

The following are some of the key reasons why choosing ZSH can be an advantage:

  • It is highly customizable.
  • It provides you with interactive tools to make your work easier.
  • You do not need to learn a new scripting language as long as you known bash.
  • It supports additional frameworks such as oh-my-zsh.
  • It is easier to work with and has a large community behind it for support.

ZSH Your Shell

Now let us begin installing and configuring our shell using ZSH. It is good to note that we will be configuring the shell ourselves. If you want to learn how to configure ZSH using a framework using oh-my-zsh, skip to the section shown.

Installing ZSH

The very first step is to install ZSH on your system. Because ZSH is popular, it is available in major Linux distribution repositories. On Debian and Debian-based systems, start by updating the repos and install.

sudo apt-get update sudo apt-get install zsh -y

If you are on REHL systems, use the command:

sudo yum update sudo yum install zsh

For Arch and Arch-based distros, use Pacman

sudo pacman -Sy sudo pacman -S zsh

ZSH as Default

To make ZSH your default shell, you can use the command chsh and set the zsh executable path. You will need to use a new terminal session for changes to take effect.

chsh Password:
Changing the login shell for debian Enter the new value, or press ENTER for the default        
    Login Shell [/bin/bash]: /usr/bin/zsh

Running ZSH

Once you have ZSH installed and set as the default shell, you will need to run it and set a few configurations. When you run ZSH, you’ll notice that it has five main configuration files. They include:

$ZDOTDIR/.zshenv
$ZDOTDIR/.zprofile
$ZDOTDIR/.zshrc
$ZDOTDIR/.zlogin
$ZDOTDIR/.zlogout

By default, if the variable $ZDOTDIR is not specified, the value of the $HOME variable gets used instead. All the files get read in order upon startup and shell shutdown:

  1. ZSH starts by reading the .zshenv file unless -f argument is specified when launching the shell session. This file should only contain the user’s environment variables. Thus, it should not contain commands that attach stdin/stdout streams (TTY).
  2. The next file is the .zprofile which contains commands executed on shell login; this file is similar to .zlogin. According to standards, the .zprofile should not contain any commands that alter the shell environment; you should only use it to run external commands.
  3. The next file is .zshrc that contains the shell configurations and commands. It is sourced in interactive shells and contains aliases, key bindings, variables, and functions.
  4. The final file is .zlogout, which gets read when the shell session closes. You can use it to set up commands executed when the shell exits.

In this tutorial, we will only work with the .zshrc file to configure various options. Let us now run ZSH and see how to configure it to suit our needs. Start by running the command:

$ zsh

The first time you run ZSH, you will have to set up the configuration file. That will typically look something like this:

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should make your use of the shell easier.
  You can:
 
(q)  Quit and do nothing.  The function will be run again next time.
 
(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function from being run again.
 
(1)  Continue to the main menu.
 
(2)  Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand if so desired).
 
--- Type one of the keys in parentheses ---

From the above, you can select several options to configure zsh. You can select 1, which will take you to the main menu, where you’ll have a few configuration options. However, I recommend selecting 2, which creates a .zshrc file in your home directory and adds the recommended configurations. We will edit this manually.

Configuring ZSH

In most cases, ZSH will use the configuration file located in .zshrc. However, you can manually create the files discussed previously for easier and managed customization. I will not get into that for this tutorial. Some of the configuration you may find useful when working with ZSH are:

ZSH Autocomplete

The auto-completion feature offered by ZSH is one of the improvements of the shell compared to others. To enable autocompletion on ZSH, add the following config entries to your .zshrc file.

autoload -Uz compinit
compinit
_comp_options+=(globdots)

You can also add the commands above in a single line and separate them with a semicolon as:

autoload -Uz compinit; compinit; _comp_options+=(globdots;

The above entry will allow ZSH to load the autoload command, which loads a file containing shell commands and calls compinit as a function. The final entry allows ZSH to autocomplete hidden files.

Customize your Shell

By default, the ZSH prompt is not very pleasing. You can use Prompt Expansions provided by ZSG documentation to do this. To avoid confusion for new ZSH users, I will not get into details about how to do this. Please refer to the link provided below to learn more.

How to Customize ZSH with Oh-my-zsh

Customizing ZSH from scratch is tedious and can take a long time to setup. However, if you need more control and fine-tuned shell, it is the best option to go. However, this tutorial does not seek to document how to configure ZSH options like bindings, functions, plugins, etc. For those who want a quick setup and customize ZSH very fast, I will show you how to use a popular ZSH framework known as oh-my-zsh. Oh-my-zsh is simple and provides many functions, plugins, and themes that allow you to customize and fine-tune your shell quickly.

Install oh-my-zsh

To install oh-my-zsh, use the commands below:

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

Or, use wget

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

The above command will clone oh-my-zsh and execute the installation script for easy setup. Note that this will create a new .zshrc file and add all its configurations. It will also create a backup copy. You can use the .zshrc file to customize the shell, such as setting themes, enabling plugins, etc.

Enable Theme

To enable, disable, or change the theme in oh-my-zsh, edit the .zshrc file and set the name of your theme in the entry ZSH_THEME=”name.” All themes are in the .oh-my-zsh/themes directory. If you do not want a theme, leave the entry as empty ZSH_THEME=””

You can view a list of all themes in this page.

Enable plugin

Oh-my-zsh contains a wide collection of plugins that you can use to expand ZHS’s functionality. To enable them, add the name of the plugin into the array:

plugins=(plugin1, plugin2, plugin2…etc)

This resource has a list of all plugins provided by oh-my-zsh

Conclusion

In this tutorial, we covered ZSH and its functionality to expand the power of the shell. It’s good to note that ZSH is highly customizable, and its full power is beyond the scope of a single tutorial. Consider reading the documentation and experimenting with ZSH until you find the right config for you. If you are looking for a quick set up consider using a ZSH framework instead. Happy ZSHing!!

About the author

John Otieno

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list