The letter A styled as Alchemists logo. lchemists
Published November 2, 2019 Updated May 15, 2020

Shell Z

Demonstrates using the Z program for quickly jumping between directories.


# Hello and welcome to the Alchemists Screencasts!
# Today, we'll look at adding Z ( to your toolbox.
# Let's say we have the following directory structure:

one="$(mktemp -d -t one)" && printf "%s\n" "$one"
two="$(mktemp -d -t two)" && printf "%s\n" "$two"
three="$(mktemp -d -t three)" && printf "%s\n" "$three"

# Granted, these are temporary directories you'd not use in a typical workflow.
# However, they demonstrate long paths which are a pain to type out.
# For example, let's visit the first directory we created:

cd /var/folders/lp/ptcmr94j0fn366vw8cczw5s00000gn/T/one.tw3PwX1f

# Ugh, that was time consuming. 😅
# ← By the way, my shell is configured to *only* show the parent directory.
# To confirm, here's the full path of our current directory:


# What if we wanted to visit the second directory we created?
# We could cheat, since we have that path stored in the `$two` variable:

cd "$two"

# What if we wanted to work in the third directory?
# By this time, we've probably forgotten what that path was.
# Since they are local variables, we can at least print them out again:

set | ag --nocolor "(one|two|three)="

# OK, good, now we can change directory to `$three`:

cd "$three"

# That's a lot of cognitive load to keep in our mental stack.
# This is where Z comes into play.
# Instead of using `cd`, we can use `z` instead:

z var one
z var two
z var three

# Because these are temporary directories, I need to supply the "var" path prefix.
# This gives Z more *specificity* to intelligently jump to the desired path. 😉
# This doesn't happen for *normal* paths. Example:

z Downloads
z demo

# To install Z, run the following:

brew install z

# Additional documentation for setting up your environment can be found here:
# I'd recommend adding this to your `.bashrc` file, though:

cat $HOME/.bashrc | ag --nocolor --after=4 "# Z"

# Note the use of the `$_Z_DATA` environment variable.
# I use this to adhere to the XDG specification:
# ...which prevents my $HOME directory from being cluttered. 🎉

# I even define a `ze` alias for editing the Z cache so-to-speak:

cype ze

# Comes in handy for times when you want to make modifications to what Z is tracking.
# Hopefully this gives you another tool in which to speed up your workflow. 🎉

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄