Jazz up your terminal prompt with Z shell and Prezto

Jazz up your terminal prompt with Z shell and Prezto

Working with containers and virtual machines means that you spend most of your time on the command line. The default Bash shell in Linux does the job, and it's always available on any Linux box, but the Z shell (or zsh) includes a metric tonne more functionality like plugin and theme support that will make your command line experience much more productive, and as a bonus, look very cool.

This post is in several sections, each of which will contribute a step change to your shell experience:

  1. Install zsh
  2. Install Prezto
  3. Change the prompt theme
  4. Configure Prezto modules
  5. VS Code (optional)

1. Install zsh

First, install the Z shell:

1sudo dnf install zsh -y

I am using Fedora, but zsh is available in all Linux distros, so use the relevant package installer.

The Z shell is an extended version of the Bourne shell, so all the useful features of bash, ksh, and tcsh are available in zsh, but out of the box, zsh will also give you:

  • Automatic cd by just typing the pathname
  • Extended globbing and grouping: *, (, |, <, [, and ?.
  • Advanced expansion of filenames, directories, and parameters: see here for dozens of examples
  • Spelling correction and approximate completion: zsh will fix mistakes when typing a directory name

Run zsh by executing it in a Bash terminal.

You will be prompted to initialise the configuration of the Z shell. Follow the prompts to configure settings for history, command line completion, how keys behave, and various other common shell options to provide the functionality listed above.

Once you are satisfied, make it your default shell by running:

1chsh -s /usr/bin/zsh

Logout then login again for it to take effect.

You can tweak the shell configuration by rerunning the wizard:

1autoload -U zsh-newuser-install
2zsh-newuser-install -f

Run man zshoptions to see the insane number of shell options that can be set and read the Z Shell Manual for all the gory details.

2. Prezto framework

The full power of zsh comes when you install a framework like Oh-My-Zsh or Prezto to enrich the command line environment with sane defaults, dozens of aliases, functions, auto completion, and theming.

Prezto started as a fork of Oh-My-Zsh, but it was subsequently rewritten with optimization in mind, so it is much faster. Install it by cloning the prezto project:

1git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

Note: the variable ZDOTDIR is probably empty, but don't stress, the default location will failover to $HOME.

Now delete the ~/.zshrc that was created in step 1 above (or rename it if you need to reuse any of the content). It will be replaced by Prezto.

Create symlinks so that zsh finds the relevant config files in ~/.zprezto (note the use of extended globbing, so make sure you enabled it when you configured zsh above, or explicitly set the option):

1setopt EXTENDED_GLOB
2for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
3  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
4done

Logout and login again. The Z shell will now have the additional functionality of Prezto.

3. Theming the shell

The default sorin theme is pretty neat, but there are 21 others to choose from. List the available themes with prompt -l then preview each of them with prompt -p <THEME_NAME>.

To change the theme, edit ~/.zpreztorc and replace sorin with the <THEME_NAME> of your choice:

1# Set the prompt theme to load.
2# Setting it to 'random' loads a random theme.
3# Auto set to 'off' on dumb terminals.
4zstyle ':prezto:module:prompt' theme 'sorin'

Logout and login again so that zsh picks up the new theme.

I prefer powerlevel10k, but this involves installing one of the Nerd Fonts first. I installed the RobotoMono font as follows:

  • download RobotoMono.zip from here
  • unzip the download
  • copy the ttf files to the local font directory and refresh the font cache:
1mkdir -p ~/.local/share/fonts/RobotoMono
2cp ~/Downloads/RobotoMono/*.ttf ~/.local/share/fonts/RobotoMono
3fc-cache -v

Logout and login again so that zsh picks up the new theme.

When you open a new terminal, it will automatically execute p10k configure to prompt you to configure the theme. Play around with the options to get the prompt theme of your techno-dreams.

4. Modules

Prezto includes dozens of modules to enhance your life on the command line, ranging from autosuggestions, look-and-feel, aliases, support for Docker and git, helper functions, syntax-highlighting, and more.

Manage the modules by editing the list in ~/.zpreztorc:

 1# Set the Prezto modules to load (browse modules).
 2# The order matters.
 3zstyle ':prezto:load' pmodule \
 4  'environment' \
 5  'terminal' \
 6  'editor' \
 7  'history' \
 8  'directory' \
 9  'spectrum' \
10  'utility' \
11  'git' \
12  'completion' \
13  'syntax-highlighting' \
14  'history-substring-search' \
15  'autosuggestions' \
16  'prompt' 

I have added git and syntax-highlighting to the default list.

It is important to get the order of the modules correct, but the documentation for each module has the details of the sequencing.

5. VS Code terminal (optional)

Note that if you use VS Code, and you install powerlevel10k, you must explicitly add the Nerd Font to the VS Code configuration settings.

Edit ~/.config/Code/User/settings.json to add the fontFamily as follows:

 1{
 2    "workbench.colorTheme": "Solarized Light",
 3    "terminal.integrated.fontFamily": "RobotoMono Nerd Font Mono",
 4    "[javascript]": {
 5        "editor.defaultFormatter": "vscode.typescript-language-features"
 6    },
 7    "git.confirmSync": false,
 8    "liveServer.settings.donotShowInfoMsg": true,
 9    "explorer.confirmDelete": false,
10}

End note

Note that there is no easy way to reload the shell settings without logging out and logging back in. There are too many environment variables and other inter-dependencies, so the usual source ~/.zshrc will not work.

And finally, a big shout out to Sorin Ionescu for this incredible project.