zsh

Syntax Highlighting and More Advanced Tips for Oh My Zsh Users

Oh My Zsh is one of the most incredible and overpowered framework for the Zsh shell. It allows you to enhance your Zsh experience with features such as themes, functions, aliases, plugins and more.

Trust us when we say Oh My Zsh makes your terminal usage much 10x more enjoyable and extremely fun even when working.

However, while it provides a great default setup out of the box, there are many ways to further customize and enhance your Zsh environment.

In this tutorial, we will explore the advanced tips for Oh My Zsh users, focusing on syntax highlighting and other productivity-boosting features.

Installation

NOTE: Ensure that you have Zsh installed on your system. You can use your default package manager to configure it on your machine.

Next, if you haven’t already, you can install Oh My Zsh by running the following command in the terminal:

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

Basic Configuration

Oh My Zsh creates a default configuration located in “~/.zshrc”. Whenever you need to customize it, you need to edit this file using a text editor.

Syntax Highlighting

Syntax highlighting in Oh My Zsh allows us to visually distinguish the commands, files, and more. It also allows you to know whether the command that you wish to run is correct or not.

To enable it, start by cloning the repository.

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

Next, add or update the following line in your “~/.zshrc”.

plugins=(... zsh-syntax-highlighting)

Finally, apply the changes by reloading the configuration file.

$ source ~/.zshrc

You can customize the highlighting colors to suit your preferences. You can do this by defining the color configurations in “~/.zshrc”.

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
ZSH_HIGHLIGHT_COLORS['pattern']='fg=blue,bold'

This example sets the color for pattern matching to blue and bold.

Oh My Zsh also allows us to enable the highlighting for aliases and functions. To enable this feature, we can add the following entries to the Zsh configuration file:

ZSH_HIGHLIGHT_ENABLE_ALIASES=true
ZSH_HIGHLIGHT_ENABLE_FUNCTIONS=true

In other cases, you may wish to disable the syntax highlighting for specific commands. We can achieve this by adding exceptions in the configuration as demonstrated in the following example:

ZSH_HIGHLIGHT_IGNORE_COMMENTS=true
ZSH_HIGHLIGHT_IGNORE_SELF=true

These lines prevent the highlighting of comments and self-executed commands.

Advanced Prompt Customization

Let us move on to a more advanced prompt customization using the Oh My Zsh framework.

We can use the Powerlevel10k theme to get a highly customizable and feature-rich prompt.

We can install it by running the following command:

$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k

We can then set the theme by editing the “~/.zshrc” file. Add the entry as follows:

ZSH_THEME="powerlevel10k/powerlevel10k"

Upon launch, the theme will run the setup wizard which walks you through the basics of setting up the theme.

For a complete guide on this, we have an existing tutorial on how to setup and configure the Powerlevel10k.

Custom Elements

Powerlevel10k allows us to add the custom prompt elements. For example, to display the current Git branch and status, we can add the entry as follows:

POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status git)

Prompt Themes and Styles

To further customize the appearance of the prompt, we can define custom styles in “~/.zshrc”. Reference the documentation for more details.

Plugins and Auto-Suggestions

Oh My Zsh has a vast collection of plugins that enhance the shell experience. To manage plugins, add them to the plugins array in your “~/.zshrc”.

For example, to enable the Git and Docker plugins, use the following command:

plugins=(... git docker)

Adding Custom Plugins

If you can’t find a specific plugin, you can create your own or clone; one from a Github repository. Place the plugin script in “~/.oh-my-zsh/custom/plugins/plugin_name” and add it to the plugins array.

Zsh Auto-Suggestions

Zsh also supports auto-suggestions which predict and complete your commands based on the previous command history. To enable it, add the following entry into “~/.zshrc”.

plugins=(... zsh-autosuggestions)

Aliases and Functions

Aliases are commands that act as subsequent or shortcuts for long commands. To define a custom alias, edit the Zsh configuration file and follow the given syntax:

alias ll='ls -alF'

alias update='sudo apt-get update && sudo apt-get upgrade'

In the given example, the “ll” alias represents the “ls –alF” command while “update” represents the full update commands.

Zsh Functions

We also have the ability to define functions in Zsh. Functions are more powerful than aliases as they include a custom logic and execution.

We can define them in the same configuration file as follows:

function show_datetime() {

local current_datetime

current_datetime=$(date "+%Y-%m-%d %H:%M:%S")

echo "Current Date and Time: $current_datetime"

}

We can then run the previous command to display the date and time as follows:

show_datetime

Organizing the Aliases and Functions

To keep the aliases and functions organized, we can create separate files in “~/.oh-my-zsh/custom/aliases” and “~/.oh-my-zsh/custom/functions”, and then source them in the “~/.zshrc” config file.

source ~/.oh-my-zsh/custom/aliases/*

source ~/.oh-my-zsh/custom/functions/*

This allows you to keep the aliases and functions in different subdirectories while still accessing them at any part in the filesystem.

Conclusion

In this tutorial, we walked you the basics of installing and setting up Oh My Zsh on your machine. We then proceeded to cover more complex and detailed concepts such as prompt customizations, theme installations, plugin usage, auto suggestions, aliases and functions, and many more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list