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.
If you are on REHL systems, use the command:
For Arch and Arch-based distros, use Pacman
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.
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/.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:
- 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).
- 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.
- 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.
- 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:
The first time you run ZSH, you will have to set up the configuration file. That will typically look something like this:
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.
compinit
_comp_options+=(globdots)
You can also add the commands above in a single line and separate them with a semicolon as:
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:
Or, use wget
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:
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!!