Running code that fails with style

We have all been there, working on code that continuously fails while staring at a dull and colorless command-line. However, we are in luck, as there is a way to make the constant error messages look less depressing. By changing our shell to one which enables a colorful themed command-line and fancy features like automatic text completion and web search your code won’t just fail with ease, but also with style!

A shell is your command-line interpreter, meaning you use it to process commands and output results of the command-line. The shell therefore also holds the power to add a little zest to the command-line. The most well-known shell is bash, which comes pre-installed on most UNIX systems. However, there exist many different shells, all with different pros and cons. The one we will focus on is called Z Shell or zsh for short.

Zsh was initially only for UNIX and UNIX-Like systems, but its popularity has made it accessible on most systems now. Like bash, zsh is extremely customizable and their syntax so similar that most bash commands will work in zsh. The benefit of zsh is that it comes with additional features, plugins and options, and open-source frameworks with large communities. The framework which we will look into is called Oh My Zsh.

Install zsh and Oh My Zsh

While bash often comes pre-installed, zsh does not. We therefore need to install zsh as well as the Oh My Zsh framework. Fear not, this can be done quite seamlessly. 

Zsh can be installed as seen below. For different platforms, steps can be found here.

sudo apt install zsh

After installation, zsh needs to be set as the default shell. This can be achieved with the below command followed by a log out and log in from your user session (a restart always works). 

chsh -s $(which zsh)

Oh My Zsh installation can be done as seen below.

sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Configurating your ~/.zshrc file

After installing both zsh and Oh My Zsh, you will now have a new ~/.zshrc file formatted by Oh My Zsh. As mentioned, zsh and bash have very similar syntax, so you will be able to add most of your code from your ~/.bashrc file to your ~/.zshrc without any problems. Things that don’t break include alias’ and conda initialisation.

Now for the main reason we wanted to change shell; having access to Oh My Zsh’s themes and plugins to spice up our command-line! Both the theme and plugins can easily be added, changed and removed using the ~/.zshrc file. 

Themes

For an aesthetic look of our command-line, we need to change the theme. A nice theme is eastwood, which can be set by changing the setting “ZSH_THEME=”eastwood”“ inside the ~/.zshrc file. NB: remember to run source ~/.zshrc for changes to take effect.

Luckily for us, people have gone crazy with themes, and there are currently over 100 to choose from here. The only thing you need to do is change “eastwood” with the theme of your liking.

Plugins

Another cool thing about Oh My Zsh is its plugins. While most of the plugins aren’t that complicated, they are extremely easy to add. To load a plugin, simply add its name inside the parentheses when setting the plugins in the ~/.zshrc file. The following loads the plugins history, web-search and zsh-autosuggestions. Note that each plugin is separated by a space. 

plugins=(history web-search zsh-autosuggestions)

There are plenty of cool plugins and a list of the standard plugins can be found here. To give some examples, we will take a quick look at two plugins.

The web-search plugin, allows you to initiate a search using your browser directly from the command-line. While this might seem a little unnecessary, it can be quite handy for quickly searching with a specific search engine. For searching you just need to write “web_search” followed by the search engine you want to use (i.e. google, scholar or github) and then what to search for, directly in your command line.

The following will search for OPIG using google.

web_search google opig

The following will search for “attention is all you need” on google scholar.

web_search scholar attention is all you need

The following will search for ablooper on github.

web-search github ablooper

More info about this plugin can be found here.

Another interesting plugin is zsh-autosuggestions. However, this plugin is not part of the standard plugin library and therefore needs to be installed first.

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

As the name suggests, this plugin gives suggestions based on your history when you type. 

More info about this plugin can be found here.

In summary, zsh and Oh My Zsh can be used to spice up the command-line and add extra functionality. While the Oh My Zsh framework is not needed, it significantly simplifies setting up and customizing your zsh shell. I hope this blog has increased your interest in shells and how they can be used!

Author