Karpton Logo

How to Install Oh My Zsh

Written by Toni Karppi on

In this tutorial I will be showing you how to install and configure Oh My Zsh on your system. This tutorial assumes that you’re running Ubuntu 18.04, but it should work similarly for other systems.

Installation

First we’ll need to install some packages. We’ll need curl for the download script, and zsh for the actual shell.

sudo apt install curl zsh

Next we need to download and run the installation script.

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Now you should log out and in again. When you log back in, your shell should be running ZSH instead of bash. To test this, you can run:

echo $0

If it says ‘zsh’, you’re set.

Configuring the shell

Now you probably want to customize the shell. The configuration file that you will be editing is ~/.zshrc. Open up this file with your favorite text editor:

gedit ~/.zshrc

You will see a lot of commented out code. You should read through the commented out lines to see what options are available for you. I personally think that the default configuration is good enough. I would recommend you to just remove all the commented out code, and leave the lines that were not commented. Your configuration file should look like this:

export ZSH="/home/$USER/.oh-my-zsh"

ZSH_THEME="robbyrussell"

plugins=(
  git
)

source $ZSH/oh-my-zsh.sh

Changing the theme

There are some themes that come included with your oh-my-zsh installation. You can find the full list of themes here. As you can see from the configuration file, the default theme is ‘robbyrussell’.

All you need to do to change the theme is to modify the variable ZSH_THEME, and then reopen the terminal. My favorite theme is ‘nicoulaj’.

Using plugins

Next we’ll look at how to use plugins. Plugins are modules that add additional functionality to the shell. There are a lot of plugins included with oh-my-zsh; however, only the ‘git’ plugin is activated by default. You can see this in the plugins array in the configuration file.

To see how the ‘git’ plugin works, you can read the documentation here. One example of what the plugin does is that it provides you with an alias for adding files: instead of writing git add ., all you have to write is ga ..

You can find a full list of the plugins provided with oh-my-zsh here. To add an additional plugin, just add it to the plugins array, either space-separated, or new-line separated. Warning: do not use commas to separate the plugins, or they won’t work. I would recommend that you add those plugins that relate to your daily workflow. There are however two plugins that do not come with oh-my-zsh, but I feel are indispensible. Luckily, the process of installing of custom plugins is simple.

zsh-syntax-highlighting

This plugin — as the name suggests — adds syntax highlighting to your terminal commands. To install this plugin, run:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

Now you can add zsh-syntax-highlighting to the plugins array in the configuration file. The plugins array should now look like:

plugins=(
  git
  zsh-syntax-highlighting
)

Reopen the terminal to active the plugin.

zsh-autosuggestion

This plugin provides suggestions for commands to enter, based on your command history. It also allows you to use autocompletion — similar to what you use on your phone. To install this plugin, run:

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

Now you can add zsh-autosuggestions to the plugins array in the configuration file. The plugins array should now look like:

plugins=(
  git
  zsh-syntax-highlighting
  zsh-autosuggestions
)

Reopen the terminal to active the plugin.

Now you should be able to see suggestions for commands. However, you probably also want autocompletion of these suggestions. For this you’ll need to create a file:

gedit $ZSH_CUSTOM/zsh-autosuggestions.zsh

In this file, add:

export ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE="20"
export ZSH_AUTOSUGGEST_USE_ASYNC="1"
bindkey '^ ' autosuggest-accept

This binds the autocompletion key to CTRL+Space, and also enables some performance optimizations. Reopen the terminal to activate the changes. This plugin uses history from ~/.zsh_history, so if you want to remove some command from autosuggestions, just remove it from the history (or delete the entire history file).

What’s next

I would recommend that you check out the plugins directory in the Github repository for Oh My Zsh, as it may contain plugins that are not listed in the Wiki. Some plugins may perform automatic sourcing of other applications (pyenv for example), so make sure that you do not also source it manually (as you would do without the plugin), as it may slow down the startup time of your terminal. If you need to perform custom export or alias calls, you should put these inside ~/.zshenv. Thanks for reading!