App Recommendations | Apps | Tutorials | Website

How to Install and Configure ZSH for Syntax Highlighting

So yesterday, I discovered ZSH. It’s pretty incredible. It’s a shell, like bash or sh, but it includes tons of features like syntax highlighting error correction, and tons more. In this tutorial, I will show you how to install zsh and configure it, so it’s as cool as I made it sound.

Installation

So the first thing you’re going to want to do is to install it. I will have separate tutorials for Ubuntu, FreeBSD, and macOS as the installation is different.

How to install ZSH on FreeBSD

To install ZSH on FreeBSD, we’re going to be using pkg. If you want to use ports, you can whereis to find where the proper port is. First, run the following command as root:

# pkg install zsh zsh-autosuggestions zsh-syntax-highlighting
Beastie, The Mascot of BSD Operating Systems
The is Beastie, the mascot of BSD Operating Systems. Isn’t he so adorable?

This should install zsh and all the recommended stuff for it. Now, you will want to open up vim or nano in your root directory with the file name “.zprofile” and enter in the following code (you can change it how you please.):

# BEGIN zsh data
# Syntax highlighting
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_STYLES[suffix-alias]=fg=cyan,underline
ZSH_HIGHLIGHT_STYLES[precommand]=fg=cyan,underline
ZSH_HIGHLIGHT_STYLES[arg0]=fg=cyan
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main,brackets,pattern,cursor)
ZSH_HIGHLIGHT_PATTERNS=('rm -rf *' 'fg=white,bold,bg=red')
# autosuggestions
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
# zsh options
setopt notify
setopt correct
setopt auto_cd
setopt auto_list
# some nice formatting for you
export PROMPT='%B%F{yellow}%~>%b%f '
alias ls='ls -G'
# END zsh data
# put your other code in here
A demonstration of how you can cd to directories without the actual cd command with these .zprofile options in zsh.

Now all you need to do is set yourself to use zsh as your default shell. To do that, type the following command as root:

# pw usermod [YOURUSER] -s /usr/local/bin/zsh

Then, log out and log back in and you should be all good. Enjoy.

How to install ZSH on Mac

If you’re on the latest macOS version, it actually ships with zsh as the default shell. However, if you’re using an older version that doesn’t have it by default, you can install it by running this (make sure you have homebrew installed first. https://brew.sh/):

brew install zsh
ZSH in Mac Terminal

After that, look into Oh My Zsh (https://ohmyz.sh/). It’s a useful utility with plugins for zsh. That will make customization easier.

How to install ZSH on Ubuntu

First of all, you need to install zsh with apt:

sudo apt update && sudo apt install zsh

Then, to make zsh your default shell, run the following command:

sudo usermod -s /usr/bin/zsh [YOURUSER]

Now, install zsh syntax highlighting by running the following command:

sudo apt install zsh-syntax-highlighting
A demonstration of zsh syntax highlighting.

Then, with nano or vim, open up the file ~/.zshrc and paste the following data:

# BEGIN zsh data
# Syntax highlighting
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_STYLES[suffix-alias]=fg=cyan,underline
ZSH_HIGHLIGHT_STYLES[precommand]=fg=cyan,underline
ZSH_HIGHLIGHT_STYLES[arg0]=fg=cyan
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main,brackets,pattern,cursor)
ZSH_HIGHLIGHT_PATTERNS=('rm -rf *' 'fg=white,bold,bg=red')
# zsh options
setopt notify
setopt correct
setopt auto_cd
setopt auto_list
# some nice formatting for you
export PROMPT='%B%F{yellow}%~>%b%f '
alias ls='ls -G'
# END zsh data
# put your code here

Once you’re done with that, you should be good to go. Just log out and log back in, and it should work!

Discord Server

Frank
Latest posts by Frank (see all)

Similar Posts

2 Comments

Leave a Reply

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