What is Z Shell Terminal?
Z shell (Zsh) is a powerful Unix command interpreter (shell) with lots of interactive features. It is the default shell of MacOS, also available for Linux and Windows users. Just like the Linux-native Bash shell, Zsh is also a scripting language that allows you to write your own scripts and automate tasks.
In this guide you are going to install Zsh with its configuration management framework Oh My Zsh, and start using some of its amazing productivity tweaks, like:
- Improved Directory Navigation
- Advanced Tab Auto-completion
- Enhanced History Search
- Custom Themes
- Custom Plugins
- And more!
Zsh vs Bash differences
Both Zsh and Bash are open-source command-line interpreters that are widely adopted. Zsh is backwards compatible to Bash with nearly identical syntax, making a transition between the two not complicated at all. Most of the commands and interaction patterns you have learned for bash will also work in Zsh.
Speaking about the differences, Zsh has much more interactive customization features than Bash. Users have many more possibilities to personalize their shell by setting up a theme and tapping into a vast ecosystem of Zsh plugins that introduce many little interactivity tweaks.
Although incremental gains may look small at first glance, all those little productivity tweaks accumulate, and you are quickly taken to the next level or command-line performance.
Oh My Zsh Community
Oh My Zsh is an open-source, community-driven framework for managing your Zsh configuration. With more than 140 pre-built themes and 300 plugins it is a great place to start customizing your Zsh terminal and unleash your new command-line superpowers.
To follow along you will need:
- A working Ubuntu 20.04 machine
- A basic understanding of the command-line interface
Install Zsh on Ubuntu 20.04
Before starting any installation, it is always a good idea to update your software repositories. Open your good old Bash terminal and use the following command:
sudo apt update
After this is done, you are ready to install Zsh from official Ubuntu repositories. We are here using
-a option to automatically agree to all installation prompts:
sudo apt install -y zsh
You can make sure that Zsh is installed by looking at the path to its binary file:
The output should look like this:
As you have successfully installed Zsh, you can now make it your default shell. Use the
chsh command with
-c option and the path to Zsh binary to do so:
sudo chsh -s /usr/bin/zsh
This will modify the
/etc/passwd users file to load Zsh as your default shell every time you login to a terminal session with your username. After restarting your terminal feel free to check your current shell by printing the
$SHELL environment variable to your terminal:
The output should look like this:
Congrats, Zsh is now your default terminal shell.
Install Oh My Zsh Framework
Even though you are now using Zsh as your default shell, it is not that different from Bash yet. You can start extending Zsh from scratch on your own, but it’s a better idea to tap into the Oh My Zsh ecosystem and experiment with what has already been created by the community.
Oh My Zsh installation takes just a few steps. First, run the apt install command to make sure you have curl installed on your Ubuntu system:
sudo apt install curl
Either you will have curl installed, or you will be assured that it is already on your system. To start the installation, download and run the Oh My Zsh installation script from its official GitHub repository:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
It takes just a few seconds and voilà! you have Oh My Zsh installed.
Use Basic Oh My Zsh Functionality
There are many great productivity tweaks that you can start using right away after installing Oh My Zsh. Let‘s review some of its functionality.
Working With Directories in Zsh
A lot of time is spent in the terminal navigating through your directories. Being able to enhance basic navigation is the first superpower you are going to get from the Oh My Zsh framework.
Directory Path Expansion
There‘s no longer a need to type in the full directory path in order to use it. You may enter the first few letters of the directory names and press
TAB to expand your hint into a full path. For instance, type
/e/3.8 and press
TAB to have it expanded into
Swift Directory Access
You can now omit the
cd command and type the path to your preferred directory directly in the terminal to get into it. For instance, type
~ to cd into your home directory or
/ to cd into your root directory.
Directory Access History
Zsh keeps a history of the accessed directories. You may check it by using the d command that is aliased for
dirs -v | head -10 to see 10 latest directories you were in:
Use the associated number access a directory from your history cache. For isntance, type 3 and press
ENTER to get into
Advanced Tab Autocompletion in Zsh
Another powerful Zsh feature is its tab completion system. It tries to suggest available commands according to the command-line context. For instance, it may suggest your available scripts, environment variables, commands and even command options.
system and press
TAB to get a list of auto suggestions. You may hit TAB again to start navigating through the available programs. Use
arrow keys to navigate through the list:
systemctl, put a
space and then hit
TAB to get a list of available systemctl commands with their descriptions. Hit
TAB again to start navigating through the list:
systemctl - and hit
TAB a few times to navigate through a list of available systemctl options with their descriptions:
And if you want to see some true magic happening, type
systemctl stop, put a
space, and hit
TAB to see a list of all available Linux processes that are currently running on your machine and are eligible for systemctl stop command:
It’s easy to start using advanced tab autocompletion, and the productivity boost is significant. Make it another tool in your command-line arsenal.
Enhanced History Search
Typically, you would use
CTRL + R to invoke reverse history search. It allows you to search for any word in your command string in your command history. Press
CTRL + R and type a substring
oh-my to find the last command with this substring:
You may keep pressing
CTRL + R to go backwards through history. Alternatively, use
CTRL + S to go forwards.
Zsh introduces one little tweak to this functionality. If you remember the beginning of a command that you are looking for, type it in and use
DOWN keys to traverse back and forth through the history filtered by your given substring. For instance, enter
apt and hit
UP to find the last command beginning with this substring.
Install a Theme for Zsh
The first thing you notice when someone is using Zsh is its unique appearance. A theme is a predefined prompt configuration that adds distinctive colors and some useful information to your terminal.
Oh My Zsh comes with more than 140 predefined themes for you to choose from. Check
~/.oh-my-zsh/themes directory to see the full list of installed themes:
Let’s now change the default “robbyrussel” theme to something more beautiful and informative. To do so you need to open your Zsh configuration file:
And change the
ZSH_THEME variable by setting its value to your preferred theme, for instance:
Save the .zshrc configuration file and load it again:
You can now see a new theme has been loaded:
This theme includes your username
loving-mole, and current directory
~ in your prompt.
And if you move to a directory where Git has been initiated, you will automatically see which branch you are on:
Not a revolution, but it will spare you a few unnecessary
git branch commands every now and then.
Enable Zsh Plugins
A Zsh plugin is a set of useful aliases and functions designed to make you more productive when using your favorite command-line tools. Oh My Zsh comes with more than 300 pre-built plugins that are available out-of-the-box.
By default, pre-built plugins are disabled. You may find a list of locally installed plugins in the
~/.oh-my-zsh/plugins directory. In order to enable a plugin in Oh My Zsh, modify the
plugins line of the
.zshrc configuration file by adding names of your preferred plugins separated by spaces:
plugins=(plugin_1 plugin_2 plugin_3 …)
Let’s now enable and test some of the pre-built Oh My Zsh plugins.
Remember directory access history? This plugin improves navigation through your directories even further. It adds keyboard bindings to allow you navigate through the directory history even easier. Start by adding dirhisotry to the configuration file:
Save the file and reload zsh configuration:
It is now possible to use the following key bindings to navigate through your directory history:
ALT + LEFT– go to previous directory
ALT + RIGHT– go to next directory
ALT + UP– move into parent directory
ALT + DOWN– move to the first child directory
Many Oh My Zsh plugins are just a bunch of practical aliases that were created by experienced system administrators. History plugin provides a couple of convenient aliases for the history command. Turn it on by modifying the Zsh configuration file:
And you will have the following aliases available:
h(history) – prints your command history
hs(history | grep) – use grep to search your command history
his(history | grep -i) use grep to do case-sensitive command history search
Colored Man Pages Plugin
How often do you go through the help pages of a specific command-line utility? The Colored Man Pages Plugin makes you a faster reader by coloring different commands and their arguments to make them immediatelly noticable in the document. Enable it by modifying the Zsh configuration file:
After loading the configuration file, try using man grep to see the changed grep manual:
JSON Tools Plugin
JSON serves as the de facto data standard for web development, and you may often need to use it directly in your terminal, especially when testing new API calls. JSON Tools Plugin makes this experience much more convenient. Try this plugin out by editing the Zsh configuration file:
You may now pipe your JSON data into an appropriate jsontool. For instance, a default JSON dump is a mess that is hardly readable. Try fetching a list of all dog breeds from the famous Dog API service just to see how bad it is:
Let’s pipe this JSON dump through the pretty print jsontool:
curl https://dog.ceo/api/breeds/list/all | pp_json
Now even humans can read it.
Install Custom Zsh Plugins
Aside from the default Zsh plugins that are shipped with Oh My Zsh, there are many other popular plugins that you can install on your own to further expand your Zsh functionality and increase your productivity.
One of the most popular custom Zsh plugins is zsh-autosuggestions. It suggests commands based on your history and available completions. Follow the custom installation guide to add it on your Oh My Zsh framework.
After installing the plugin start typing a command and autosuggest will generate a suggestion for you:
To accept the suggestion, hit
There are many additional options and key bindings that you may setup for autosuggestions. Check the official documentation if you like this plugin and want to customize it further.
Syntax Highlighting Plugin
Zsh Syntax Highlighting Plugin enables highlighting of commands whilst they are typed into a Zsh terminal prompt. It is helpful when you are writing long commands where it is easy to make syntax errors. Follow the custom installation guide to add it on your Oh My Zsh framework.
After installing the plugin type in a slightly modified command for fetching a pretty list of dog breeds to see syntax highlighting in action:
As you can see, your commands are now colorful and more informative. Legitimate commands are put in green, unfamiliar commands are red, command options are white, and strings are yellow. Less syntax errors guaranteed!
Last, but not least, is the Autojump Zsh Plugin. It gives you an even faster way to navigate your filesystem. Autojump keeps its own history file with a weighted list of your visited directories. It then tries to guess the full path of the directory you want to visit by the given directory name and its weight.
Follow the custom installation guide to add autojump on your Oh My Zsh framework.
After installing autojump and using it for a while you will populate your autojump history file. You may check it with
autojump -s command:
As you can see, some of the directories were visited more and have a higher weight.
Let’s say you are now in your home directory and would like to jump into the
After you execute the command, you are automatically taken into the ../warehouse directory with the highest historical weight:
Try jumping back and forth to get used and integrate autojump into your everyday work if it works for you.
Configure Zsh Further
After you had installed Oh My Zsh a startup configuration file
.zshrc was created in your home directory. It is a shell startup file where you put your commands, aliases, functions, options, and key bindings. This file is loaded automatically when you initiate a new terminal session.
We have already made changes to the
.zshrc file when adding custom themes and plugins. Let’s now make some additional changes to solidify your Zsh environment even further. Open the configuration file and start editing:
You may want to enable automatic Oh My Zsh updates. Uncomment the following line to update Oh My Zsh as soon as the new version is available:
zstyle ':omz:update' mode auto
It’s also handy to have a time stamp for every history command. Uncomment the following line and choose your date format to enable this feature:
This will put a timestamp next to every command that is showed by the history command:
Vim Key Bindings
If you are an avid Vim user, enable Vim key bindings. This will put you in the insert mode by default when you open your terminal. Hit ESC button to enter the command mode instead. You may enable Vim key bindings with the following line:
Bear in mind that this option will disable the default emacs key bindings. For instance, you won’t be able to use
CTRL + R to enter the reverse search mode anymore. However, you can redefine missing emacs key bindings yourself with the following pattern:
bindkey [keys] [function]
To restore the reverse search functionality with Vim key bindings add the following line:
bindkey '^r' history-incremental-search-backward
This option will re-enable
CTRL + R key combination for invoking backwards history search.
You may also add your own aliases to
.zshrc file. For instance, let’s add the following alias to make it more convenient to access the zshrc file:
alias zconf="vim ~/.zshrc"
Now every time you type zconf into the terminal, you will automatically
vim into the
When you finish making changes to the
.zshrc file, don’t forget to load it into your shell. The source command will read and execute commands from a specified file and load them into your current shell environment:
You may keep modifying your Zsh environment further and add your own tweaks. There are no limits to how you can change your command line environment, since Zsh is also a powerful scripting language. If you decide to go down this rabbit hole, brace yourself and take look at the official zsh documentation.