Beginner to Zsh on Linux

Zsh has become one of the most popular shells for the Linux operating system. In this article, we show how to install zsh and oh-my-zsh framework on Linux and how to modify the themes and plugins of oh-my-zsh.
Install Zsh
First, let’s check if zsh is already installed on the machine using the following command.
$ which zsh
If it returns the path to zsh, like ‘/bin/zsh’, then zsh is already installed, and we can skip the installation step. Otherwise, the return would be ‘/usr/bin/which: no zsh …’, where zsh need to be installed.
With root access
The easiest way to install zsh is using package managers. The commands are shown below.
# on Ubuntu
$ sudo apt install zsh
# on CentOS
$ sudo dnf install zsh
For other systems, corresponding commands can be found here.
Without root access
Zsh can also be installed for a user. First, we need to download the zsh source files here. Then, we extract the package, enter the directory, and run the following commands to finish the installation.
./configure --prefix=$HOME
make
make install
Finally, zsh binary is installed at $HOME/bin, which should in the $PATH variable.
We can check if the installation is successful by checking the version of zsh.
$ zsh --version
zsh 5.7.1 (x86_64-redhat-linux-gnu)
Config Zsh
Though we have installed zsh successfully, zsh is not our default shell. Because we still need to type ‘zsh’ in the shell each time we want to use it. Configuration is needed.
With root access
The default shell can be changed by chsh command.
chsh -s $(which zsh)
Without root access
We can convert the default shell by modifying the configuration files. First, we need to check our current (default) shell using the following command.
$ echo $0
It returns the name or path of the current shell, such as ‘-bash’, ‘-tcsh’, ‘/bin/csh’, ‘/bin/ksh’ and others.
What we need to do is to modify the startup script of the current default shell, so that the default shell calls zsh each time we log in, and exit just after we log out from zsh. The startup script file is ‘~/.bash_profile’ for bash and ‘~/.profile’ for sh and ksh. We need to append the following line to the end of the script.
[ -f $HOME/bin/zsh ] && exec $HOME/bin/zsh -l
However, if the default shell is csh or tcsh, we need to modify ‘~/.cshrc’ or ‘~/.login’ by appending the line shown as below.
if ( -f ~/bin/zsh ) exec ~/bin/zsh -l
In this way, zsh will be executed automatically the next time we log in to the machine.
$ echo $0
zsh
Install oh-my-zsh
Oh-my-zsh is a delightful framework for managing zsh configuration, themes and plugins. We can install oh-my-zsh using the command below.
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
oh-my-zsh is installed in ‘~/.oh-my-zsh’. And a configuration file ‘~/.zshrc’ is also created.
Change the Theme
‘~/.oh-my-zsh/themes/’ contains all built-in themes of oh-my-zsh, and we can also add our custom theme files to ‘~/.oh-my-zsh/custom/themes/’. If we want to change the theme, we need to get the file name of the theme ‘xxx.zsh-theme’ and put the theme file prefix into the ‘~/.zshrc’ file.
ZSH_THEME='xxx'
All changes in ‘.zshrc’ would take place the next time loggin in. Or we can manually source the configuration file to apply the changes by the following command.
$ source ~/.zshrc
Add plugins
‘~/.oh-my-zsh/plugins/’ contains all built-in plugins of oh-my-zsh, and we can also add our custom theme files to ‘~/.oh-my-zsh/custom/plugins/’. We take the ‘zsh-autosuggestions’ plugin as an example. First, all files of the plugin should be put into ‘~/.oh-my-zsh/custom/plugins/’ using the following command.
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
Then, similarly, we need to modify the ‘~/.zshrc’ file by appending the ‘zsh-autosuggections’ to the plugin list.
plugin = (xxx zsh-autosuggestions)
Also, we need to source the ‘~/.zshrc’ file after modification.
In conclusion, zsh and oh-my-zsh framework are installed and we are able to change the themes and plugins. Have fun!