ZSH — Interactive Command Shell for Linux + Oh-My-Zsh
Greetings!

Today I’ll tell you and show you how to install and configure a great interactive command shell — Zsh. With proper configuration, this shell greatly simplifies life working in the command line and makes the process even enjoyable)

TLDR

BASH
# install Z-Shell and helper utilities
sudo apt update && sudo apt install -y zsh fzf git curl

# check that everything installed correctly on our system
whereis {zsh,fzf,git,curl}

# install Oh-My-Zsh - answer yes
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# install the zsh-autosuggestions plugin
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# install the fast-syntax-highlighting plugin
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting

# install the cmdtime plugin
git clone https://github.com/tom-auger/cmdtime ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/cmdtime

# change the theme to agnoster
sed -i 's/^ZSH_THEME=.*/ZSH_THEME="agnoster"/' ~/.zshrc

# disable Oh-My-Zsh auto-updates
sed -i "s/^#.*omz:update.*disabled/zstyle ':omz:update' mode disabled/" ~/.zshrc

# enable the installed plugins
sed -i 's/^plugins=\(.*\)/plugins=\(git zsh-autosuggestions fast-syntax-highlighting fzf cmdtime\)/' ~/.zshrc
# set colors for the fzf plugin (this is all one command)
echo "export FZF_DEFAULT_OPTS=$FZF_DEFAULT_OPTS'
--color=fg:#e5e9f0,bg:#3b4252,hl:#81a1c1
--color=fg+:#e5e9f0,bg+:#3b4252,hl+:#81a1c1
--color=info:#eacb8a,prompt:#bf6069,pointer:#b48dac
--color=marker:#a3be8b,spinner:#b48dac,header:#a3be8b'\n\n\
$(cat ~/.zshrc)" > ~/.zshrc

# apply the changes made
source ~/.zshrc

# for a minimalist prompt
echo "prompt_context() [ ]" >> ~/.zshrc && source ~/.zshrc

## configuring Oh-My-Zsh for the root user

# copy our user's files into the root user's directory
sudo cp -r {~/.oh-my-zsh,~/.zshrc} /root/

# change root's default shell
sudo chsh -s /usr/bin/zsh

# switch to root to check
sudo -s
Click to expand and view more

Preface

Zsh (Z-Shell) is an extended command shell for Unix-like operating systems, providing additional functions and capabilities compared to a more standard shell such as Bash. Zsh supports autocompletion, extended output formatting, powerful configuration, and other advanced features to improve productivity and convenience when working with the command line.

If you’ve read my previous posts, you probably know that I carry out all command-line manipulations in the Linux Mint distribution environment, currently version 21, Cinnamon edition. This article’s material is no exception.

Just in case, here are a few materials on this topic:

Installing Z-Shell and helper utilities

Open a terminal and run the installation command:

BASH
sudo apt update && sudo apt install -y zsh fzf git curl
Click to expand and view more

What we installed:

After the command finishes, let’s check that everything installed correctly on our system:

BASH
whereis {zsh,fzf,git,curl}
Click to expand and view more

Using curly brace { } syntax in the Linux command line, you can specify a list of values. In this case, the whereis command processes them one by one and outputs the result to standard output.

Installing a powerline font for a GUI session

For icons to render correctly in your terminal during a graphical session, you need to use a special monospace icon powerline font🤯, for example from the Nerd Fonts project.

My readers know that I prefer the Hack font ☝️. Here’s a simple example of how to install it:

BASH
# create font directory
sudo mkdir /usr/share/fonts/Hack

# download font archive
curl -fsSLO \
    $(curl -s https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest \
    | grep browser_download_url \
    | grep 'Hack.zip' \
    | cut -d  '"' -f 4)

# unpack archive, copy fonts to system
sudo unzip ./Hack.zip -d /usr/share/fonts/Hack/ && rm -f ./Hack.zip
Click to expand and view more

After installing the font, activate it in your terminal settings🛠.

In Gnome-terminal, this is done as follows:

Everything’s in place, let’s move on.

Installing and configuring the Oh-My-Zsh framework

Let’s install Oh-My-Zsh, install two additional plugins, and perform the initial shell setup.

Installing Oh-My-Zsh on Linux Mint 21

So, Oh-My-Zsh is an open source project, representing a framework and set of extensions for the Zsh shell.

Why would we even need Oh-My-Zsh if we just installed Zsh? The thing is, low-level shell configuration is a rather complex process, comparable to writing software in a certain programming language. But since many people who use the Linux command line are not programmers, yet still want to leverage the shell’s potential, the developers of the Oh-My-Zsh project kindly provided the community with this opportunity.

All settings, with simple syntax, are set in a single configuration file ~/.zshrc, and the framework itself does the rest. We’ll see this below.

This framework is installed using a script, invoked with the command specified on the project’s official website: https://ohmyz.sh/

The command uses the curl utility, which we installed earlier. Run in the terminal:

BASH
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Click to expand and view more

To view the contents of the script file, just go to: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh

While running, you’ll be asked whether you want to change your current shell to Zsh:

Enter y then Enter, followed by your user password and Enter again.

You’ll notice that the command line prompt has changed:

After installation, a shell configuration file .zshrc and a folder with the framework’s files .oh-my-zsh will appear in the user’s home directory:

Both files are hidden

Installing plugins for dynamic highlighting, autosuggestions, and command timing

By default, Oh-My-Zsh already has many plugins. You can view their list in the ~/.oh-my-zsh/plugins directory. Let’s additionally install 3 more custom plugins for: fast dynamic syntax highlighting, smart command autosuggestions, and displaying the time spent executing a command.

Installation is done by cloning the repositories containing the plugin files using the git utility into the ~/.oh-my-zsh/custom/plugins folder. Run these 3 commands one by one in the terminal:

BASH
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zdharma-continuum/fast-syntax-highlighting ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting

git clone https://github.com/tom-auger/cmdtime ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/cmdtime
Click to expand and view more

Editing the .zshrc configuration file

Now let’s configure our Zsh a bit. Open the configuration file ~/.zshrc for editing with any editor convenient for you. For example, xed:

BASH
xed ~/.zshrc
Click to expand and view more

For convenience, enable syntax highlighting display for this file:

For variety, I’ll edit the file in vim:

BASH
vim ~/.zshrc
Click to expand and view more

By the way, I recently made an introductory article about vim (: VIM — Console Editor: An Introduction

In the file, edit the lines indicated below.

BASH
# Change from:
ZSH_THEME="robbyrussell"

# to:
ZSH_THEME="agnoster"
Click to expand and view more

You can view the list of available themes in the ~/.config/oh-my-zsh/themes directory.

You can view a preview of each theme in the official GitHub repository of the developers: https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

BASH
# Uncomment the line:
zstyle ':omz:update' mode disabled
Click to expand and view more

BASH
# Find the plugins parameter and set it to:
plugins=(git zsh-autosuggestions fast-syntax-highlighting fzf cmdtime)
Click to expand and view more

Save the contents and apply the changes made with a special command:

BASH
source ~/.zshrc
Click to expand and view more

Our shell’s prompt has changed:

To fully apply the changed shell settings, I recommend logging back into your user account on your desktop.

If you want to get a separator bar, like here for example:

Demonstration of Zsh + Oh-My-Zsh in action

For demonstration, let’s run the command:

BASH
ls -l Документы
Click to expand and view more

Here you can see:

A separate feature of Zsh is the completion mechanism when pressing the Tab key. For example, enter the command ls, then a space, and press Tab several times:

The shell lets you switch between the possible options, and when the one you need is highlighted, pressing Enter inserts it into the command line. Isn’t that lovely)

Also, by default, Zsh visually displays the result of the previous command’s execution:

A red cross indicates an error

Also enabled out of the box is the git plugin. If you use a version control system, this plugin will incredibly simplify your life)

About the fzf utility and its plugin for Oh-My-Zsh

FZF (Fuzzy Finder, “fuzzy” homie finder) is a powerful command-line tool that provides the ability to quickly and intuitively search for files, directories, and other items in the terminal.

If you type the command of the same name in the terminal, “fuzzy” search mode for the current directory is activated:

BASH
fzf
Click to expand and view more

The fzf plugin for Oh-My-Zsh is used for quick searching through command history.

By default, Oh-My-Zsh doesn’t pick up the terminal’s color scheme to display colors in fzf. So they need to be set manually as a variable in the ~/.zshrc configuration file. Here’s an example for the Nord color scheme:

BASH
export FZF_DEFAULT_OPTS=$FZF_DEFAULT_OPTS'
    --color=fg:#e5e9f0,bg:#3b4252,hl:#81a1c1
    --color=fg+:#e5e9f0,bg+:#3b4252,hl+:#81a1c1
    --color=info:#eacb8a,prompt:#bf6069,pointer:#b48dac
    --color=marker:#a3be8b,spinner:#b48dac,header:#a3be8b'
Click to expand and view more

Add the lines above at the beginning of the file:

Save and exit. Now, while working in the terminal, press Ctrl + r to activate the shell’s built-in interactive command history search function:

Enter the key you’re searching for, or move the cursor using the arrow keys on the keyboard:

When you press Enter, the command under the cursor will be inserted into the command line. To exit search mode, press the Esc key.

Manually updating Oh-My-Zsh

Earlier we disabled the framework’s automatic update check (because it annoys me). If needed, the update process can be started manually with this command:

BASH
omz update
Click to expand and view more

A minimalist shell prompt

When using a minimalist prompt, you may run into problems using scp and sftp on this host. Keep this in mind. I ran into this nuance myself. Details here.

If you crave minimalism and feel that constantly seeing your username and computer name is visual noise, add this line to the end of the ~/.zshrc file:

BASH
prompt_context() [ ]
Click to expand and view more

It’s crucial that it’s at the end of the file! Otherwise you’ll get an error) Save, close. Apply the changes:

BASH
source ~/.zshrc
Click to expand and view more

Nothing extra~, just a neurosis~.

Changing the shell from bash to zsh for the root user

To get a similarly configured shell for the root user as well, run these commands:

BASH
# copy our user's files into the root user's directory
sudo cp -r {~/.oh-my-zsh,~/.zshrc} /root/

# change root's default shell
sudo chsh -s /usr/bin/zsh

# switch to root to check
sudo -s
Click to expand and view more

The chsh utility in Linux allows a user to change the default shell. Since we ran this command as the root superuser via sudo, we thereby changed its shell.

As you can see, when working as root, the shell visually informs us of this with a lightning bolt symbol.

I should warn you that using a shell like this with extensions, and especially with automatic updates enabled, for the root user is considered unsafe.

For use on a home computer, I don’t see anything wrong with it. But, for example, on production servers, it’s probably worth refraining from this idea)

Afterword

To finish, a small comparison of the visual experience of working with Bash and Zsh:

Bash on the left, zsh on the right

The same actions were performed in both. Here you can clearly see that Zsh provides more interactivity than Bash. But I still love Bash, just so you know) It’s just that Zsh is convenient for interacting with the terminal on a daily basis.

I also recommend studying the article: Fine-tuning .zshrc when using oh-my-zsh.

Thanks for your time. Good luck!

Useful sources

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/software/zsh-interaktivnaya-komandnaya-obolochka-dlya-linux-oh-my-zsh/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut