How to Setup ZSH and Oh-my-zsh on Linux
The Z Shell or zsh is an interactive UNIX shell and powerful command-line interpreter for scripting language including shell scripting. Zsh has been developed since 1990 by 'Paul Fastad', and the 'zsh' moniker is derived from the Yale professor named Zong Shao.
The Z shell has become one of the most popular shells for Linux operating system. It's rich in features and easy to configure and customize. Following are some key zsh features:
- Command auto-complete
- Improved variable handling
- Spelling correction
- Shared command history
- Kill tab completion
- Environment variable easy setup
Oh-my-zsh is an open source framework for managing ZSH, the Z shell. There is some framework for the Z shell, like prezto, Zgen, Antigen etc [HIMANSHU: Not sure if we need this line]. In this tutorial, we will use 'oh-my-zsh' for our zsh configuration.
Oh-my-zsh is a community-driven framework with a lot of features. It comes with a customizable theme and has a rich catalogue of plugins aimed at system admins and developers.
In this tutorial. we will show you how to install the Z shell or zsh on Linux Ubuntu and CentOS. And then configure the oh-my-zsh framework for managing zsh. We will also show you how to change the zsh theme and enable some plugins.
- Linux - Ubuntu 16.04 or CentOS 7
- Root privileges
What we will do
- Install and configure ZSH
- Install and configure Oh-my-zsh framework
- Change default theme
- Enable oh-my-zsh plugins
Step 1 - Install and configure ZSH
In this step, we will install the Z shell from the repository, and then configure a user to use the Z shell as default theme. Basically, the default shell on Ubuntu and CentOS is bash, so we will configure a root user to use zsh as the default shell.
To install zsh from the repository, use the following commands.
apt install zsh
yum install zsh
After the installation is complete, change the default shell of the root user to zsh with the chsh command below.
HIMANSHU: I checked that the Chsh command is available on Ubuntu as well. Plus the 'which' command mentioned in the Ubuntu section below won't change the login shell. So the command mentioned under 'On Ubuntu' needs to be fixed I think.
chsh -s /bin/zsh root
Now logout from the root user, log in again, and you will get the zsh shell.
Check the current shell used with the command below.
The output should be zsh. Here's the result on Ubuntu.
And here's the result on CentOS.
The Z shell zsh has been installed.
Step 2 - Install and configure Oh-my-zsh framework
So the Z shell is now installed on the system. Next we want to install the oh-my-zsh framework for managing the Z shell zsh. Oh-my-zsh provides an installer script for installing the framework, and we need to install some other required packages, including wget for downloading the installer script and Git for downloading oh-my-zsh shell from GitHub.
So the first step is to install wget and git on the system. Here are the commands you need to run:
apt install wget git
yum install wget git
Now download the installer script and execute it.
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh
The result/output should be similar to the one shown below.
So, oh-my-zsh is installed in the home directory '~/.oh-my-zsh'.
Next, we need to create a new configuration for zsh. As with the Bash shell, which has a configuration named '.bashrc', for zsh, we need a '.zshrc' configuration file. It's available in the oh-my-zsh templates directory.
Copy the template .zshrc.zsh-template configuration file to the home directory .zshrc and apply the configuration by running the source command, as shown below.
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
Oh-my-zsh is now installed on the system, and the Z shell has been configured for using the oh-my-zsh framework with default configuration.
Following result is on Ubuntu.
And here's what you'll see on CentOS.
Step 3 - Change default themes
The default .zshrc configuration that's provided by oh-my-zsh is using 'robbyrusell' theme. In this step, we will edit the configuration and change the default theme.
The Oh-my-zsh framework provides many themes for your zsh shell, head to the link below to take a look at the available options.
Alternatively, you can go to the 'themes' directory and see the list of available themes.
In order to change the default theme, we need to edit the .zshrc configuration file. Edit the configuration with the vim editor.
Pick one zsh theme - let's say 'risto' theme.
Then change the 'ZSH_THEME' line 10 with 'risto' theme as below.
Save and exit.
Now, reload the configuration .zshrc and you will see that 'risto' theme is currently used as your shell theme.
Result on Ubuntu.
Result on CentOS.
So this way, you can apply a new oh-my-zsh theme.
Step 4 - Enable Oh-my-zsh plugins
Oh-my-zsh offers awesome plugins. There are a lot of plugins for our environment, aimed at developers, system admins, and everyone else.
Default plugins are in the 'plugins' directory.
In this step, we will tweak zsh using the 'oh-my-zsh' framework by enabling some plugins. In order to enable the plugins, we need to edit the .zshrc configuration file.
Edit .zshrc configuration file.
Go to the 'plugins' line 54 and add some plugins that you want to enable inside the bracket (). For example, here's the change I made in my case:
plugins=(git extract web-search yum git-extras docker vagrant)
Following is the result when using the 'extract' plugin - you can extract zip and tar file using the 'extract' command.
Yum command - yum info with only 'yp' command.
Vagrant plugin for command autocompletion.
To conclude, the Z shell, as well as the oh-my-zsh framework, have been installed. In addition, oh-my-zsh default theme has been changed with some plugins enabled.