In an earlier post, we discussed the basics of zsh, what it is, and how to get started. Today we’ll look at syntax highlighting in zsh, its meaning, and how to implement it. I’ll also explain why it doesn’t work well with bash, even though we can get it to work in a “hacky” way.
What is Syntax Highlighting?
Syntax highlighting is a feature whereby the color of the text changes depending on what you type, providing you with feedback about the accuracy of your commands. Those of you who have coded using an Integrated Development Environment (IDE) know how useful syntax highlighting can be when it provides you with a visual indication of an incorrect command because of a typo, for example. Syntax highlighting is also useful when you have to complete nested brackets, letting you see which bracket level is being closed.
This is particularly useful with complicated commands, where it’s hard to see how one command flows into another, and typos are easily made. How many times, for example, have you typed “claer” instead of “clear”? If you work in a shell environment for a long time, syntax highlighting is a massive quality of life improvement. And because of the ease of extensibility in zsh, we can easily add syntax highlighting to it, compared to bash.
Adding Syntax Highlighting in zsh
The first step, of course, is to make sure that you have zsh installed. Use this command:
zsh --version
You should see something like this:
If you haven’t already installed zsh, then use the following commands:
sudo apt update
sudo apt install zsh -y
Installing a Plugin Manager
Once zsh is installed, it’s time to install a plugin manager. You don’t need to install this, but it’s highly recommended since it makes the entire process so much easier. And since one of the great strengths of zsh is the plugin system, why not exploit that to our advantage? A plugin manager allows us to update all plugins with a simple command and keep track of them from a centralized location.
Plugin managers can also optimize the loading of plugins – a process called “lazy-loading”, as they bring the plugins forward only when necessary, reducing bloat. Plugin managers also have more helpful error messages when something goes wrong. Because of all these benefits, we’re going to use a plugin manager called “Oh My ZSH”.
We install the “Oh My ZSH” plugin manager using the following command:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
This will download and install “Oh My ZSH” and you’ll see something like this:
During the installation process, you’ll see an option to change the default shell to zsh. If you plan on using it a lot, you might as well make use of that option right now.
You might wonder why we’re not using the official Ubuntu repos to install software, and question whether or not this is safe. It’s a fair concern. But Oh My ZSH isn’t a compiled binary, but rather a collection of shell scripts, so it’s not like a regular package. It’s designed to live in your home directory. Installing from sources like this is always a risk, so we must always check the source. Oh My ZSH is a well-known package, and is widely acknowledged to be safe, so you’ll have to go with that. You can view the oh-my-zsh github page and see that the cloning URL exists.
Installing the Syntax Highlighter
Now that we’ve completed the installation of the Oh My ZSH plugin manager, we can install the actual syntax highlighter plugin. To do this, type the following:
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
As you can see, we clone the git repo for the zsh-syntax-highlighting plugin and point it towards the folder where the zsh plugin manager keeps its plugins. Here’s what it looks like:
Now there’s only one step left.
Editing ~/.zshrc
Once you’ve installed the plugin, you need to tell zsh to activate the plugin when the shell starts. Otherwise, it won’t load anything. Just like the bash environment has a “~/.bashrc
” file that maintains all its configurations, zsh has something similar. The corresponding file for that is ~/.zshrc
. Open it and scroll down to the “plugins” section. If you’re using vi, then the command is:
vi ~/.zshrc
And the plugin section looks like this:
You can see that I already have one plugin installed and ready, namely, git. To activate our plugin, we need to add it to the list, separated by a space. Make sure you don’t use something like a comma (,) to separate the plugins – it won’t work! The name of the plugin you need to add is:
zsh-syntax-highlighting
Save your changes and exit. Now, just like with using the bash source command, we need to use the source command with zsh for reloading the file, like this:
source ~/.zshrc
And you’re done!
Testing the Syntax Highlighting
If everything works as it should, zsh should now start highlighting the syntax of valid commands whenever you start typing. For example:
Here you can see that when I type ‘echo “Hello world”‘, zsh highlights two things:
- The command “echo”
- The string “Hello world”
While writing complex commands, this can be a big help as zsh gives you visual feedback on what’s going on, which pieces of text are the command, which are the strings, and which are the arguments. Segmenting long inputs like this reduces errors and makes them more readable.
Why not Use Syntax Highlighting with Bash?
You might be wondering why it’s important to use zsh with syntax highlighting. Surely bash has similar functionality, right? Unfortunately, that’s not the case.
Bash was never designed for real-time syntax highlighting in the way zsh was. Bash uses a module called “GNU Readline”, which doesn’t support hooks into the input process by plugins, making dynamic styling problematic. There are ways to get around this, but they’re “hacky”, unlike with zsh, which allows plugins direct access to the input stream in real time.
Some bash plugins like bash-preexec attempt to simulate input hooks, but these are not officially supported by bash, and because they use methods not designed for this, they don’t work well, are clunky, and can break easily. In complex shell environments, they can behave unpredictably, making debugging much harder.
The “Readline” tool can color prompt strings (what you see before the command itself), but changing the color of actual commands and strings is problematic, and requires us to hijack regular terminal behaviour, which might not work across terminals and is easily breakable.
Finally, whatever experiments exist for syntax highlighting on bash are not well supported. A lot of projects are abandoned or experimental, meaning that when something goes wrong, you’re not going to find support either from other people or through documentation.
Bottom line – if you want syntax highlighting, it’s best to use another shell environment than bash. For all the power of bash, this is one thing it’s simply not cut out to do.
Conclusion
Syntax highlighting is a pretty significant quality-of-life improvement for an active Linux administrator. If you switch to zsh from bash, you can get started with highlighting in just a few minutes. Don’t try it on bash – it’s not worth it!

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply