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:
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:
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:
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:
highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
Finally, enable the syntax-highlighting in your current shell by sourcing the file with the command:
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:
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Finally, add the zsh-syntax-highlighting as the plugin in your .zshrc file as:
To apply the changes to your current terminal session, enter the command:
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.