Using and Customizing zsh-syntax-highlighting With oh-my-zsh

| Comments

A screenshot of zsh-syntax-highlighting

I recently read about this nifty new real-time syntax highlighter: zsh-syntax-highlighting. It looked like it would be pretty handy, so I decided to try it out for a couple of weeks.

It is a bit too colorful out of the box

Too many things are highlighted by default for my tastes. Every correctly typed command, every file name, and every globbing character is highlighted in one way or another. I was especially unhappy with the underlined path names.

Some highlighting is really awesome

I may not like all the extra noise, but there are a few things that I’m finding to be very useful. Highlighting a misspelled command in red is very nice. It is nice to catch typos before trying to execute a command.

Highlighting reserved words should be pretty helpful. I use one-liner for loops all the time, but I’m very good at leaving out the do. Highlighting makes that slightly more, obvious but I sure wish the done would highlight red if it didn’t match a do. It does match pairs of brackets, though, so maybe I can get in the habit of using those instead of old bash-isms…

It also does a good job of quote highlighting. That will probably help me catch mismatched and unescaped quotes pretty easy.

Customizing colors when using oh-my-zsh

I had a bit of trouble here. I tried setting the color variables in a file in my ~/.oh-my-zsh/custom directory while loading zsh-syntax-highlighting as a plugin from my ~/.oh-my-zsh/plugins directory. When I did, this it was acting like the colors were not already defined. I thought this was a bit strange because oh-my-zsh loads plugins before it runs anything in ~/.oh-my-zsh/custom.

I didn’t investigate this very heavily. I just moved zsh-syntax-highlighting into my custom directory and loaded it manually.

Here’s what my zsh-syntax-highlighting configuration looks like:

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)

source $ZSH/custom/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

ZSH_HIGHLIGHT_STYLES[default]=none
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=red,bold
ZSH_HIGHLIGHT_STYLES[reserved-word]=fg=green
ZSH_HIGHLIGHT_STYLES[alias]=none
ZSH_HIGHLIGHT_STYLES[builtin]=none
ZSH_HIGHLIGHT_STYLES[function]=none
ZSH_HIGHLIGHT_STYLES[command]=none
ZSH_HIGHLIGHT_STYLES[precommand]=none
ZSH_HIGHLIGHT_STYLES[commandseparator]=none
ZSH_HIGHLIGHT_STYLES[hashed-command]=none
ZSH_HIGHLIGHT_STYLES[path]=none
ZSH_HIGHLIGHT_STYLES[globbing]=none
ZSH_HIGHLIGHT_STYLES[history-expansion]=fg=blue
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]=none
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]=fg=yellow
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]=fg=yellow
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]=fg=cyan
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]=fg=cyan
ZSH_HIGHLIGHT_STYLES[assign]=none

Most of what I did was remove colors.

Comments