Learn Computing from the Experts | The Rheinwerk Computing Blog

What Is Oh My Zsh?

Written by Rheinwerk Computing | Oct 16, 2024 1:00:00 PM

There are countless extensions available for zsh. These can be divided into two groups: plugins that provide additional functionality and themes that change the look of command prompts.

 

The zsh script with the funny name Oh My Zsh helps you to add such extensions to the zsh configuration in an uncomplicated way. A wide range of plugins and themes is already included in the standard delivery, while others can be installed and integrated later. Besides Oh My Zsh, there are other plugin management systems for zsh, but we won't cover them here.

 

To install it, you need to download and run a small script from the project page at https://github.com/ohmyzsh/ohmyzsh. The script assumes that the git command from the package of the same name has already been installed. Instead of typing the following command, it’s better to copy the code from the GitHub page:

 

user$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/\

   ohmyzsh/ohmyzsh/master/tools/install.sh)"

 

During the installation, .zshrc will be overwritten. The previous content ends up in the backup file named .zshrc.pre-oh-my-zsh. From now on, Oh My Zsh will check for updates at startup. Accordingly, when you open a new terminal window, you are often prompted to update Oh My Zsh.

 

By default, Oh My Zsh is characterized by a relatively moderate basic configuration. The prompt is predefined by the robbyrussell theme. As the only active plugin, git contributes countless aliases that help with the operation of git. alias lists all the abbreviations. The plugin also influences the prompt: when you use cd to change to a directory under the control of this version control system, it displays the information about the Git status as well as the name of the currently active branch.

 

To regard Oh My Zsh purely as a tool for Git enthusiasts falls far short of the mark. The real power of Oh My Zsh is that by changing a few lines in .zshrc, you can enable more plugins as well as a theme of your choice. For this purpose, all you have to do is change the settings of the ZSH_THEME and plugins variables. The following listing shows the syntax that you must adhere to. In particular, you must not separate the plugins with commas. Spaces are sufficient:

 

# .zshrc file

...

ZSH_THEME="robbyrussell"

...

plugins=(git dirhistory)

 

For the changed configuration to take effect, you must start a new shell, and the easiest way to do this is to open a new terminal window. By the way, the dirhistory plugin mentioned in the previous listing provides the (Alt)+(í) and (Alt)+(î) keyboard shortcuts, which allow you to quickly jump through the last 20 directories used.

 

Themes control the color scheme of zsh and especially the design of the prompt. zsh provides an almost infinite number of design options in this regard. Instead of dealing with this yourself, it's much easier to try out one of the many predefined themes. On the following page, you will find screenshots of the many included themes: https://github.com/ohmyzsh/ohmyzsh/wiki/Themes.

 

There are also other themes that are not directly integrated into Oh My Zsh, such as Powerlevel10k (see figure below). You must first install these themes manually before you can activate them in .zshrc. Note that with some themes you also need custom fonts to display various special and status characters in the prompt. Copy the font files to the .fonts directory, then open the configuration dialog of your terminal program and set the appropriate font.

 

 

A detailed description of Powerlevel10k can be found on the project's GitHub page at https://github.com/romkatv/powerlevel10k.

 

An almost endless number of plugins for all conceivable tasks, tools and programming languages can be found at https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins. A better guide is the following top 20 list, though it was compiled in 2018: https://safjan.com/top-popular-zsh-plugins-on-github.

 

Each editor can highlight different parts of a program code. This must also work in the shell, right? We will briefly introduce three corresponding extensions here.

 

The colored-man-pages plugin provided by Oh My Zsh formats man pages using more colors.

 

The colorize plugin, also included in Oh My Zsh, provides the ccat and cless commands. When you use it to view text files, the commands try to recognize the syntax and highlight text in a meaningful way. Before adding colorize to the plugin line of .zshrc, you must install the pygments package or python-pygments, depending on your distribution.

 

The ccat and cless commands are aliases that refer to the zsh functions colorize_cat and colorize_less. They are noticeably slower than cat or less, which is why the obvious alias less=cless statement is not recommended.

 

A completely different approach is taken by the zsh-syntax-highlighting plugin, which is not included in Oh My Zsh. This plugin changes the colors of the zsh command you just entered. What may seem superfluous at first glance is actually a great tool for recognizing before you press (Enter) for the command whether it contains a typo (the command is then displayed in red instead of green) or whether a quotation mark is not closed.

 

For the installation, you need to run the following command, which you can also find on the GitHub project page (see https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/INSTALL.md):

 

user$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \

   ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

 

After that, you just need to add zsh-syntax-highlighting to the plugin line of .zshrc.

 

Editor’s note: This post has been adapted from a section of the book Linux: The Comprehensive Guide by Michael Kofler.