zsh

How to Highlight Syntax in Zsh on Mac?

In the world of coding and programming, the methods to write commands or instructions are crucial for computers to understand us. As a Mac user, you are familiar with Zsh, a command-line shell that makes it easy for you to enhance your terminal experience by offering several customization options compared to the default Bash shell. Syntax highlighting is one of the features of Zsh that makes your visual appearance go beyond your imagination. Using Syntax highlighting, your commands will appear in different colors, making them stand out from the rest.

This guide will help you in learning the art of highlighting the syntax in Zsh on your MacOS.

How to Highlight Syntax in Zsh on Mac?

You can highlight syntax in Zsh from the following two methods:

Method 1: How to Highlight Syntax in Zsh Using Extension?

There is an extension, which you can easily install and use in Zsh to highlight syntax. The complete step-by-step process to install and use the extension method for syntax highlighting in Zsh is given below:

Step 1: Open Zsh terminal and execute the below-given command to download Zsh syntax highlighting extension source file from GitHub website.

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

Step 2: Now open the Zsh configuration file called zshrc on your terminal using the following command:

sudo nano /etc/zshrc

Step 3: Then add the following line at the end of the zshrc file, as shown below:

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

Step 4: Save the file using Ctrl+X keys, add Y, and enter to exit. Then reload the changes to the source file to enable the Zsh syntax highlighting feature on your Mac system.

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

Here are the snapshots of the terminal before and after enabling the syntax highlighting feature in Zsh.

Before Syntax Highlighting

After Syntax Highlighting

2: How to Highlight Syntax in Zsh Using Oh My Zsh Plugins?

You can also install Oh My Zsh on the terminal and use its plugins option to highlight syntax in Zsh; this can be done through the following steps:

Step 1: First, install Oh My Zsh on Mac from here.

Step 2: Then use the following git command to clone the Oh My Zsh syntax highlighting feature for Zsh.

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

Step 3: Open the zshrc configuration from the following command:

nano .zshrc

Step 4: Inside the source file, search plugins option using Ctrl+W and then add the following line right in front of the plugins, inside the bracket after the git keyword as shown below:

zsh-syntax-highlighting

Step 5: Save the file using Ctrl+X keys, add Y, and enter to exit. Then reload the changes and enable the syntax highlighting feature from the following command:

source .zshrc

The following are snapshots that are taken before and after performing syntax highlighting in Zsh from Oh My Zsh plugins.

Before Syntax Highlighting

After Syntax Highlighting

Conclusion

The syntax highlighting feature in Zsh is crucial since it allows you to recognize and colorize various parts of your commands or syntax. There are two easy methods to highlight syntax in Zsh, one is using the extension and the other is using Oh My Zsh plugins. Both methods require cloning the source file from the GitHub website and adding a line inside the zshrc file to enable the highlight feature in Zsh. The above guide has presented a detailed step-by-step tutorial to easily use both these methods on your Mac terminal to quickly highlight syntax in Zsh.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.