跳到主要内容

Use ZSH

Bash, or the Bourne Again Shell, is a hallmark of Linux systems. However, there are other shells out there that may offer extra functionality. Here's how to install one popular alternative, ZSH.

What is ZSH?

ZSH, also called the Z shell, is an extended version of the Bourne Shell (sh), with new features and support for plugins and themes. Since it's based on the same shell as Bash, ZSH has many of the same features, and switching over is a breeze.

ZSH vs. Bash: The Important Differences ZSH has too many features to list here, some just minor improvements to Bash, but here are some of the major ones:

  • Automatic cd: Just type the name of the directory
  • Recursive path expansion: For example /u/lo/b expands to /usr/local/bin
  • Automatic Syntax Highlighting: Words, file extensions, and other things visible in the Terminal are color-coded
  • Spelling correction and approximate completion: If you make a minor mistake typing a directory name, ZSH will fix it for you
  • Plugin and theme support: ZSH includes many different plugin frameworks

Plugin and theme support is probably the coolest feature of ZSH and is what we'll focus on here. If you're on Linux, the command to install ZSH varies by the distro, but it should be a default package in your package manager.

How to install ZSH?

To install ZSH on Ubuntu, or any other Debian-based distro, open up the Terminal, then run:

sudo apt install zsh

To install ZSH on ArchLinux, run:

sudo pacman -Syu zsh

Confirm that zsh has been installed correctly by running zsh --version in the console.

Now that ZSH has been downloaded and installed correctly, we need to change the default shell from Bash to ZSH. Run:

chsh -s $(which zsh)

If you are using a command line SSH (including WSL), logout and re-login. If you are using a graphics distribution, you should restart your PC to make it work.

When you reopen the Terminal, you'll be given some configuration options for ZSH. Once you've installed ZSH you can start adding themes and using plugins. We've covered how to do that in the last two sections of the article.

Install ZSH on Other Linux Systems

Generally speaking, you'll want to use your distros default package manager to install ZSH, as it is included by default in most repositories (repos). If it is not present for some reason, you can always download the source code and install that. Additionally, the ZSH GitHub has instructions for installing it on the most popular Linux distros.

Oh-My-Zsh is the most popular plugin manager for ZSH, and it comes with many built-in plugins and themes as well. There are also a couple of other plugin frameworks, including Antigen, which is a full package manager for ZSH, but Oh-My-Zsh has loads of plugins built right in and does its job well.

Oh-My-Zsh has a simple install script you can run:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

How to Use ZSH Themes

There are plenty of themes to go around, but powerlevel10k is by far the coolest. It adds a right-aligned info box, integration with git and command history, incredible customization, and wraps it all up in a slick interface based on the powerline plugin for vim. You'll want to use iTerm on macOS, or any terminal with 24-bit color, to get the most out of powerlevel10k (or any ZSH theme, really).

See Powerlevel10k for details.

You can also use Starship.

Install Nerd Font

You need to install the Nerd Fonts to use modern icons and themes in zsh. You can either download and manual install in Nerd Fonts Releases, or use a script getnf.

Recommend Plugins

The plugins required are not included in Oh My Zsh plugins. We will add them manually:

git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH/plugins/zsh-syntax-highlighting

Open the .zshrc file, replace the line plugins=(git z) by plugins=(git z zsh-autosuggestions zsh-syntax-highlighting).

Reload ZSH to make it work:

exec zsh