• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
  • VPS
    • VPS Hosting
    • Flex VPS
  • Reseller
  • Email
  • Gaming
  • Domains
  • Website Builder
  • Account
  • Blog Home
  • Categories
  • Authors

How to Use zsh Syntax Highlighting

Bhagwad Park

Published on: April 30, 2025

Categories: Linux Command Line, VPS Hosting 0

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:

zsh Version
zsh Version

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:

Installing oh my zsh

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:

Cloning Syntax Highlighting into oh-my-zsh
Cloning Syntax Highlighting into oh-my-zsh

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:

~/.zshrc

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:

Syntax Highlighting works
Syntax Highlighting works

Here you can see that when I type ‘echo “Hello world”‘, zsh highlights two things:

  1. The command “echo”
  2. 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!

Bhagwad Park Profile Picture
Bhagwad Park

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!

Related Posts

Compress and Conquer: A Comprehensive Guide To The Linux Zip Command

Check out our guide to learn everything you need to know about the Linux zip command - from basics to advanced use cases, we've got it all.

Solve Temporary Failure in Name Resolution Error in Linux

The Temporary Failure in Name Resolution error signals you may have a DNS problem. Here are a few ways you can fix it.

Unlocking the Potential Of Printf In Shell Scripting

Learn more about the printf command with practical examples and best practices in our beginner friendly guide.

How to Fix the “Docker Permission Denied” Error

The Docker "permission denied" error happens when you're not using sudo, or don't belong to the "docker" group.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup (2024)

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Email Hosting
  • Game Hosting
  • Domains
  • Website Builder
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2025 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA