Skip to main content

Using Zsh

Enable Syntax Highlighting in Zsh

Improve your Zsh experience by enabling syntax highlighting. Here's how to do that.

Enabling the syntax highlighting on ZSH can be a lifesaver, especially if you are into development.

And in this tutorial, I will walk you through ways to enable syntax highlighting in ZSH:

  • Using syntax highlighting extension (easy)
  • Using the Oh My ZSH plugin (recommended)

So let's start with the first method.

Enable syntax highlighting on ZSH using the extension (easy)

This is the easiest way to enable syntax highlighting on ZSH.

To install syntax highlighting extension, you will have to have git installed on your system.

And if you are using Debian/Ubuntu-based system, you can use the following command to install git :

sudo apt install git

Once you have git installed, use the following command to clone the zsh-syntax-highlighting repository:

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

Next, you will have to add this syntax highlighting extension to the .zshrc file using the following command:

echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc

And finally, enable the syntax highlighting to your current shell using by sourcing the file using the following command:

source ./zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

That's it!

Here are the before and after results:

before and after results of enabling syntax highlighting on ZSH

Oh My ZSH is the framework that allows you to do various tweaks to your ZSH.

Also, you get some basic configuration by default making it look way better than the default ZSH.

And that's the reason why I recommend this instead of the first method.

First, install the Oh My ZSH on your system using the following command:

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

And if the ZSH is not your default shell, it will ask whether you want to make it the default:

change your default shell to zsh

I would recommend you make it the default shell!

Now, clone the zsh-syntax-highlighting repository using the following command:

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

Next, open the /.zshrc file:

nano ~/.zshrc

And add zsh-syntax-highlighting in the plugins as shown:

enable syntax highlighting in ZSH using the oh my zsh

Now, save changes and exit from the nano text editor.

To enable the change, log out and log back in.

And here are the before and after results:

how to enable syntax highlighting in ZSH using the oh my zsh.png

A night and day difference! Isn't it?

You can do a lot more with ZSH! Learn How to

If you want to learn how you can add multiple plugins like syntax highlighting and auto-suggestions with a pretty theme, you can refer to the given guide:

How to Install Zsh on Ubuntu and Customize it
Envy the beautiful Z shell? Learn how to install zsh on Ubuntu and customize it with Oh My Zash.

I hope you will find this guide helpful.

And if you have any queries, let me know in the comments.

Sagar Sharma