Pimp my Terminal - An Introduction to "Oh My Zsh"

Introduction

Using a terminal can be a bit of an intimidating experience in the beginning - but after you get used to it, it can boost your productivity in ways you couldn't imagine and can become quite a pleasurable experience.

That's probably the most addicting part of using the terminal daily. You are always searching for the best ways to do certain tasks more efficiently, with as few clicks as possible.

That insatiable hunger for more efficiency, which is arguably one of the reasons Homo Sapiens rose to the top, will likely make you consider digging deeper into your terminal configuration, even if you are at the beginning of your terminal usage journey.

Maybe you've seen someone using a cool-looking terminal with a lot of useful bits of information clearly displayed and wondered how a terminal could look so cool and elegant.

Maybe it looked something along the lines of:

Either way, the answer to most of your questions about advanced terminal usage would probably be the Zsh shell with the Oh My Zsh framework installed on top of it.

In simple terms, Zsh is an alternative to the Bash shell which implements a lot of nice features that boost your productivity and improve your workflow. To that end, Oh My Zsh is a lightweight, open-source framework for managing those nice Zsh features - themes, plugins, aliases...

In this guide, we'll cover some of the reasons why you may consider using Oh My Zsh. Firstly, we'll get acquainted Zsh shell and then dig into Oh My Zsh, before diving deeper into the configuration of Oh My Zsh itself.

This guide is meant to serve as a starting point for your Oh My Zsh journey. By the end of it, you should understand the basic concepts of configuring Zsh with the help of Oh My Zsh, be aware of alternatives, and be ready to continue exploring new things about the Zsh shell configuration.

Let's start with the basics!

What Is Zsh?

Zsh, also known as Z shell, is another shell for UNIX-based systems, frequently used as an alternative to Bash, the default shell on a lot of UNIX-based systems. Although Zsh was created back in the 1990s, it has been gaining a lot of popularity in the last few years, especially since Apple announced that they will be using Zsh as the default shell on all of their new systems running macOS.

Both Bash and Zsh were created around the same time and are both based on the Bourne shell, the default UNIX shell at the time. That's why most casual Bash users often hesitate to switch to Zsh, even though Zsh has some advanced features that make it worth considering.

Why Use Zsh Over Bash?

Obviously, there is no clear way of deciding which shell is ultimately better and should be used all the time. There are definitely some upsides and downsides of choosing either one and your personal preference will likely play a large part in the decision.

In this section, we'll cover some advantages of using Zsh.

As we've stated before, both Zsh and Bash are developed from the same shell and have a lot of similar features. More specifically, Zsh can actually run a lot of Bash scripts without any problems. On top of that, Zsh comes with a lot of nice capabilities that could make it a better choice over Bash such as:

  • Better auto-complete - Zsh has arguably much better auto-complete than Bash. Navigating through auto-complete suggestions is much more user-friendly and intuitive.
  • Built-in auto-correction - if you tend to type fast and make a lot of typos along the way, Zsh will fix them for you, without the hassle of running external scripts or installing additional plugins.
  • Automatic cd - there is no need to type cd every time you wish to change a directory, just type the path to the desired directory.
  • Recursive path expansion - there is also no need to type the whole path, for example, cd us/b/l will expand to cd user/bin/lin.
  • Easier to customize - probably one of the most unique features of Zsh is its customizability. There are a ton of themes and plugins that are made to meet most of the needs that you may have. They are usually managed by the Oh My Zsh framework, but there are other frameworks as well.

How to Install and Configure Zsh

If you've decided to give Oh My Zsh a shot, the first thing you need to do is to install and configure Zsh on your system.

This is as simple as typing the following in your terminal:

$ sudo apt update
$ sudo apt install zsh

Now, let's make Zsh the default shell on our system by running the following command:

$ chsh -s $(which zsh)

That should do the trick! We've installed Zsh and set it as the default shell. Now we can focus on customizing it using the Oh My Zsh.

What Is "Oh My Zsh"?

Zsh has a lot of themes and plugins and if you decide to use them - they need to be installed and managed in the proper way. If you are a beginner, chances are that you won't be able to keep track of the Zsh configuration properly all by yourself.

That's where Oh My Zsh comes in! It is the most popular framework for managing Zsh configuration, plugins, and themes. It will help you transition from using your current shell to Zsh in the easiest way possible.

Although Oh My Zsh can do many other things, it is the most famous for its ability to easily manage a ton of themes and plugins. That is especially useful because Oh My Zsh themes are not just simple color schemes, but a mechanisms that provides us with a lot of clearly displayed useful bits of information such as:

  1. Current git branch
  2. Git status
  3. Battery status
  4. Size of the current folder
  5. Command performance information

On the other hand, plugins are made to extend the capabilities of Zsh, some of them just add new aliases for commonly used commands, and some add some more advanced features.

In this guide, we'll mainly focus on showing the functionalities of some interesting themes and plugins.

How to Install Oh My Zsh

Official documentation recommends that we install Oh My Zsh using curl or wget. All we need to do is to run the automatic installer by running one of two following commands:

# Run the following command to install Oh My Zsh using `curl`
$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Run the following command to install Oh My Zsh using `wget`
$ sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

Your terminal won't look any different until you restart it. Only then will it apply a different configuration, but it'll still look similar to the original state.

The appearance of the terminal is based on the theme we are currently using and the plugins that are installed. The default theme is pretty similar to what you're probably already used to.

Let's change the theme and take a look at how we can manage plugins with Oh My Zsh.

How to Use Oh My Zsh

During the installation, Oh My Zsh takes control over the Zsh configuration file called .zshrc. That's a file in your home directory in which Zsh stores all of its configuration parameters. Oh My Zsh backups the original .zshrc file and just creates a new one, so if you ever want to disable Oh My Zsh, you could revert to your original Zsh configuration easily.

There are a lot of tweaks and changes that you could make in the .zshrc file, but we'll focus on the most common ones.

Let's open the .zshrc file and take a look at its structure:

$ nano ~/.zshrc

This line will open the .zshrc file from the home directory using the nano editor. You could open it using any other editor such as Visual Studio Code, Vim, Emacs, etc.

The first thing you will see after opening the .zshrc configuration file is a lot of commented content. That content is there so that you can easily enable some of the most commonly used features just by uncommenting the corresponding command in the .zshrc file.

Additionally, the file follows a very simple sectioned structure:

# Section 1 - PATH variables & path to Oh My Zsh installation
export ZSH="/home/stamd/.oh-my-zsh"

# Section 2 - Theme settings
# robbyrussel is the default Oh My Zsh theme
ZSH_THEME="robbyrussel"

# Section 3 - General zsh settings
...

# Section 4 - Plugin settings
plugins=(git zsh-autosuggestions sudo)

# Section 5 - Other user settings
...

# Section 6 - User-defined aliases
# Aliases defined here will override any other alias
alias home="cd ~"
# Now, when you type `home` in the terminal, you will
# be navigated to the home directory
...

We'll focus on sections 2, 4, and 6. By doing that, we'll illustrate the most common use cases of Oh My Zsh, firstly by installing a simple theme and a couple of useful plugins. After that, we'll take a look at how to define some custom aliases.

How to Install Oh My Zsh Themes

The the official directory of themes contains over 150 easily-installable Zsh themes. Installing one of these themes is the simplest way to customize the looks of your Zsh terminal.

Themes are essentially configuration sets that other people have created. You can create your own custom themes by fiddling around with the configurations manually - but it's significantly easier to get started with existing themes.

In order to install a theme, you just need to find the name of the theme in the official directory and add that name to the themes section of the .zshrc file.

For instance, if you want to install the theme called "simple", all we need to do is replace the current value of the ZSH_THEME variable in the .zshrc file with the theme name:

ZSH_THEME="simple"

After saving the .zshrc file and restarting the terminal, it should look something like this:

As you can see, this is seemingly a very simple theme, however it has some interesting features:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

  • It changes the way the path to the working directory is displayed, it hides the current user name from the prompt.
  • It shows the name of the current git branch if the working directory is in a Git repository.
  • Furthermore, it displays the symbol if the current directory has some untracked changes, and the symbol if the current directory is up-to-date.

This theme is a good starting point since it's not too different to what you might already be used to, but still offers some benefits.

If you can't choose one single theme that you like the most, you can configure Oh My Zsh to randomly change the theme every time you fire up a new terminal. To achieve that behavior, you will need to uncomment the ZSH_THEME_RANDOM_CANDIDATES variable from the theme section in the .zshrc file, and set the ZSH_THEME variable accordingly:

ZSH_THEME=random

ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" "simple" )

Now, the Oh My Zsh will randomly choose one of three themes listed in the ZSH_THEME_RANDOM_CANDIDATES variable.

A popular theme is the Powerlevel10k theme:

ZSH_THEME="powerlevel10k/powerlevel10k"

How to Install Official Oh My Zsh Plugins

With the theme installed, the obvious next step is to customize your new cool-looking terminal even more by installing some interesting plugins!

Oh My Zsh comes shipped with more than 270 official plugins, as well as a lot of third-party plugins that can be installed. Their complexity ranges from simple plugins that will just add some new aliases to your configuration, all the way to the more complex plugins that perform syntax highlighting, command completion, and some other advanced features.

Let's take a look at how to install some of the most popular Oh My Zsh plugins and explain what they are used for. In order to install a plugin shipped with the Oh My Zsh framework, you just need to find its name on the official plugin list and add it to the list of plugins stored in the plugins variable of the .zshrc file.

Let's install the plugins called git, z, and sudo, by navigating to and editing the .zshrc file so that the plugins variable contains a list of mentioned plugin names:

plugins = (git z sudo)

That's all, we've enabled our desired plugins, and we can use their features after restarting our terminal. If you want to enable any other official plugin, you just need to add its name to the plugins list.

Note: The elements in the Zsh array are separated with blank space characters, separating them with a comma will result in an error.

Let's explain the use cases of our newly installed plugins:

  • Git - in essence, this plugin is a bundle of predefined aliases that helps you speed up the usage of Git in a terminal. Instead of git status, you can write gst, instead of git add, you can write ga, etc. Take a look at the list of all aliases to get a feel for the shortcuts.
  • sudo - a very useful plugin that enables you to add sudo as the prefix to the current or previous command, just by pressing ESC two times.
  • z - this plugin aims to boost your productivity by enabling you to navigate through directories with as few clicks as possible. It keeps track of your most visited directories and enables you to navigate to them by typing just a few characters from the desired directory path.

For instance, let's assume that we've visited the /usr/share/media-player-info directory. To access it the next time, it's enough to run the z med command.

How to Install Third-Party Oh My Zsh Plugins

Now that we've explained how to enable plugins shipped with Oh My Zsh, let's consider some interesting third-party plugins.

Enabling them is not as straightforward as enabling official plugins though since every plugin has its own installation procedure. Especially given the fact that these procedures may change over time - the best we can do is point you to the official documentation, where you can follow the outlined steps to enable the plugins.

A few of some interesting third-party plugins are:

  • zsh-syntax-highlighting - this plugin highlights the commands that you are currently typing based on the correctness of the typed commands. The command is red if typed wrong, green if typed right, underlined if it is the name of the existing folder.
  • zsh-autosuggestions - this plugin suggests the ending of the command that you are currently typing based on the commands that you've previously executed.

Creating Aliases

If you use some command more than a few times a day - it's probably a good idea to define an alias for that command.

An "alias" is just an alternative name for a command. You'll typically define aliases shorter than the commands themselves, and use them as shortcuts.

For example, gaa could be the alias for the git add all command. If you type the alias in the terminal the system will execute the original command, which can oftentimes be a huge time saver.

Oh My Zsh comes with some predefined aliases, most of which are related to Git. We won't cover them in this guide, but you can read the complete list of predefined Oh My Zsh aliases on the official Oh My Zsh page.

The first type of Zsh aliases is the simple alias. These aliases are defined in the .zshrc file like so:

# alias my-alias="command"
# Examples:alias gaa="git add all"alias lsla="ls -la"...

Simple aliases are used to substitute the long command with the shorter alias. In essence, they are reducing the number of keystrokes needed to run certain frequently used commands.

The other type of Zsh aliases that we are going to cover is the suffix alias. We define this type of alias using the -s flag:

# alias -s extension=name-of-the-tool
# Examples:alias -s py=codealias -s js=codealias -s {css,html}=code...

Suffix aliases are used to define which tool will be used to open all files with the listed extension. For example, alias -s py=code will result in all commands including Python filenames being opened with the code editor.

For example the example.py command will be transformed into code example.py, resulting in the example.py file being opened using Visual Studio Code, assuming that Visual Studio Code is installed.

There are three more types of Zsh aliases, global aliases, operating system specific aliases, and functions for aliases with parameters. These are the topic of an upcoming Guide to Zsh Aliases (coming soon!).

To create an alias, you simply modify the .zshrc file following the standard syntax:

alias ct="{print -z Current time is $(date)}"

Once the alias is saved in the file, you can reference it from the terminal as:

$ ct
Current time is Fri Jul 23 15:56:02 CEST 2021

When ran in the stylized terminal, it'd look like:

Or you can even create an alias in the terminal, on the fly, without modifying the file at all:

~ alias ct="print -z Current time is $(date)"
~ ct                                         
~ Current time is Fri Jul 23 15:59:08 CEST 2021

Conclusion

As you've probably guessed, the most important goal of Oh My Zsh is ease of use. It's absolutely not necessary to use it for managing your Zsh configuration, by all means, all of it could be achieved without the help of Oh My Zsh.

The caveat is that managing the configuration without it could be a bit hard for a new and inexperienced user.

To that end, we can define Oh My Zsh as the tool to flatten the learning curve when switching to the Zsh shell.

All in all, Oh My Zsh will help make using the terminal a comfortable experience. There are a lot of beautiful themes to choose from, as well as a ton of useful plugins that are made to boost your productivity. On top of that, the Zsh shell itself is a lot quicker to run than most of the alternative shells, which makes it great for use.

Of course, the Bash shell has the framework analog to the Oh My Zsh called Bash-it. If you are hesitant to switch from Bash to the Zsh, Bash-it could be a feasible alternative to the Oh My Zsh.

This guide was meant to be an overview of the basic capabilities of the Oh My Zsh framework and aims to give you some starting knowledge and encourage you to dig deeper into details of the Zsh configuration.

Last Updated: March 7th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms