zsh

Aliases
  • Z shell
Image of Author
August 11, 2023 (last updated April 2, 2026)

https://en.wikipedia.org/wiki/Z_shell

The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.

Zsh was created by Paul Falstad in 1990

Check your shell

echo $SHELL
# /bin/zsh

Prompt

As of this writing I use https://starship.rs/ for prompt setup, but I have some notes on zsh prompt here: Zsh Shell Prompt (PS1, PROMPT).

aliases

You can set aliases as follows:

alias g='git'

You can save these in your ~/.zshrc, but also, you can set these per terminal session. For example, if you were going to explore a executable that normally has a long name, you can temporarily shorten it for a single terminal session, and it will go away once you close the session.

alias o='openssl'

autoload and fpath

This can get complicated, you can learn more via man zshbuiltins section on autoload and via the man zshmisc section on "Autoloading Functions".

autoload and fpath are used in conjunction to create lazily executed functions in the terminal. When you autoload a function, you essentially set it to undefined. When you execute that function, it checks fpath directories for that function and loads it and executes it, if found.

For example

cd ~
mkdir alpha
touch alpha/beta
echo "echo gamma" > alpha/beta
# in .zshrc
fpath+=~/alpha
autoload beta

Both fpath and autoload are necessary in order for this to work. (There are a few ways to construct functions, according to the man zshmisc section "Autoloading Functions", but from what I can tell, they all require autoloading and fpath.)

Then, in a new terminal,

beta
# => gamma

Importantly, calling autoload on a function does not execute the function. That's why you commonly see the following in .zshrc files:

autoload -Uz compinit
compinit

You first tell zsh to "prepare" the function (in the example, it's compinit, which initialises zsh completions). This makes the function available for both loading and executing. You then call the function itself (in the example, compinit). You will see this pattern for any custom functions you want to call immediately within the .zshrc file.

The -Uz flags are not crucial to understanding the flow, but for completeness, -U suppresses alias expansion and -z ensures zsh-style autoload behavior is used.

completions (via compinit)

Many CLI tools include completions for zsh.

Homebrew

https://docs.brew.sh/Shell-Completion#configuring-completions-in-zsh

Homebrew shell completions for zsh are nearly automatic. On MacOS the brew setup script automatically adds it to ~/.zprofile and in ~/.zshrc you only need the following (see other sections on what this does),

autoload -Uz compinit
compinit

It's important to note that while this is only ran when brew is present in the shell script, brew is always present and so this is always ran. The code within the if-block has almost nothing to do with brew and is pure zsh functionality except for the part where we prepend brew completions to the fpath.

Manual

For tools not installed via homebrew, the following works well.

mkdir -p ~/.zsh/completions
# something like the following exists on many cli tools
cli-tool completions zsh > ~/.zsh/completions/_cli-tool

and then in your ~/.zshrc,

fpath=(~/.zsh/completions $fpath)

It is worth noting that there are already places on fpath (echo $fpath) to put completions. But as of this writing, I prefer my own location set aside for manual additions.

You can read more about the zsh completion system via man zshcompsys. Be advised it is complicated.