zsh

How Do I Enable Syntax Highlighting in ZSH

Whether you are a power user or just starting to dip your feet into the Linux command line, you must accept the inescapable truth that you can never memorize all the commands in the system. You will encounter instances where you execute an incorrect command or reference the help menu to perform some operations.

In this tutorial, we will learn how to use the ZSH shell and the zsh-syntax-highlighting feature.

What is ZSH?

Z Shell, commonly ZSH, is an extension of the Bourne Again Shell (bash). It comes with new features and supports for tools such as plugins and themes. Since it offers features similar to BASH, switching to ZSH is incredibly easy.

What is ZSH-Syntax-Highlighting?

The ZSH syntax highlighting feature—similar to one found in the fish shell—automatically highlights your commands as you type them, which can help you catch syntax errors and fix them before running the command.

Installing ZSH

The first step before we enable syntax highlighting for ZSH is to ensure we have the shell installed. ZSH does not come pre-installed in most Linux distributions but is readily available in the main repositories.

To install it, use the commands:

#Installing on Debian/Ubuntu

sudo apt-get update

sudo apt-get install zsh

# Installing on Arch/Manjaro

sudo pacman –Sy

sudo pacman –S zsh

# Installing on Fedora/CentOS

sudo yum update

sudo yum install zsh

Once you have ZSH installed successfully, we can proceed to set up command syntax highlighting.

How to Install ZSH-Syntax-Highlighting

Installing the zsh-syntax-highlighting is relatively straightforward. However, you must ensure you have git installed on your system before proceeding. You can use the commands:

# On Debian

sudo apt-get install git

# Arch/Manjaro

sudo pacman –S git

#Fedora/CentOS

sudo yum install git

Once installed, start by cloning the repository using the command:

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

The next step is to add the syntax-highlighting extension to the .zshrc file. The value of the ZDOTDIR variable will determine this. Use the command shown below:

echo "source ${(q-)PWD}/zsh-syntax-

highlighting/zsh-syntax-highlighting.zsh"
>> ${ZDOTDIR:-$HOME}/.zshrc

Finally, enable the syntax-highlighting in your current shell by sourcing the file with the command:

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

Using oh-my-zsh

If you have the oh-my-zsh framework installed for your ZSH shell, you can add the zsh-syntax-highlighting as a plugin.

Start by cloning the repository as:

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

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

Finally, add the zsh-syntax-highlighting as the plugin in your .zshrc file as:

plugins=( zsh-syntax-highlighting)

To apply the changes to your current terminal session, enter the command:

source ~/.oh-my-zsh/zshrc

Conclusion

This tutorial has shown you how to install and set up the zsh-syntax-highlighting feature. Using this feature, you can view the syntax of your commands in real-time and review them before execution.

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