Oh My Zsh – Improve your shell experience

Screenshot of a terminal with the Oh My Zsh plugin. The prompt is modified to show the current working directory and the current git branch.

Introduction

Have you already seen people making presentations which a fancy shell prompt like the above? This is particularly useful in presentations because viewers can clearly see in which directory the commands are being run.

The second great feature is for developers. When the current directory is in a Git repository, the prompt shows the current branch and its status, using a color that reveals whether there are uncommitted changes or not.

I’ve been attracted to this feature for a while now, in particular for the demonstrations I run during my training sessions, so I eventually made the jump. As after a few days, I’m really a fan and I haven’t found any noticeable side-effect yet, I wanted to share with you how I enabled it.

This feature is based on the Z shell (zsh) which has been around for a very long time, and Oh My Zsh, a collection of themes and plugins for zsh. We are going to use the agnoster theme, but other interesting themes are available, like this per-directory history one.

Installation instructions on Ubuntu 24.04

Installing Zsh

Of course, the first thing to install is zsh:

sudo apt install zsh

Then, the next thing to do is to make it the default shell for your user. That’s something that the installation script of Oh My Zsh could do, but I’m not comfortable running scripts from untrusted Git repositories with root privileges.

So, to do this, open the /etc/passwd file on your machine (you will have to run your editor through the sudo command to gain root privileges), find the line corresponding to your user name, and replace /bin/sh by /usr/bin/zsh.

Installing Oh My Zsh

It’s now time to install the extension. The Oh My Zsh website offers a one-line command to do it, but again, I prefer to control what is being run.

Start by cloning their Git repository:

git clone https://github.com/ohmyzsh/ohmyzsh.git
cd ohmyzsh/

You can then run the installation script:

./tools/install.sh

The script will ask you:

Do you want to change your default shell to zsh? [Y/n]

If you already made zsh the default shell, you can answer n here. Otherwise, you will have to type your password to let the script run with root privileges.

Further Ubuntu configuration work

To use the agnoster theme, a special font is need to render the specific characters in the prompt:

sudo apt install fonts-powerline

The default flavor of Ubuntu, based on GNOME, also needs another font tweak:

gsettings set org.gnome.desktop.interface monospace-font-name 'Ubuntu Mono 13'

I had to reboot my machine to make it take these settings into account. Restarting the graphical session was probably sufficient though.

Zsh theme tweaks

Enable the theme of your choice by adding the below lines to your ~/.zshrc file:

# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="agnoster"

Another thing you can edit in this file is removing the user@login information at the beginning of the prompt:

# Hide “user@hostname” info in the prompt
prompt_context(){}

Restore aliases and environment variables

Before you can use your system as you did before, you also need to restore your environment variables and aliases. I immediately noticed that I was missing my EDITOR setting, causing git commit to open an uninvited text editor, to say the least.

Fortunately, zsh seems to have great compatibility with bash, as its syntax for environment variables and aliases seems to be the same.

So, copy the environment variables and aliases that matter to you (see all the irrelevant settings that accumulated over the years!) from ~/.bashrc to the ~/.zshrc file.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Upcoming training courses

Yocto Project and OpenEmbedded
In-person – Feb. 25-28, Avignon, France
Online – Mar 18-20, 25-27

Embedded Linux
On-line, Apr. 14-17, 22-25
In-person, May 5-9, Avignon, France


Latest Posts


Social media