I’ve found that I spend more and more time in the terminal over the years as I become more familiar with the command line. It’s often the quickest and most efficient way to accomplish something. And with custom scripts and aliases, annoying/lengthy tasks can be reduced to just seconds.
This weekend, I moved my site to Digital Ocean and had quite a bit of fun setting up and managing the server solely from the command line. Working with the remote server (via ssh) did feel a bit tedious though as I didn’t have all the custom scripts and aliases I’m used to having locally. But there was one thing in particular that it was hard to go without. In fact, it’s one of the first things I installed (WP CLI took the first slot). It’s called Z.
What is Z?
Z is a shell script that makes jumping around your file directory pleasantly simple. Instead of trying to remember the exact path of where you need to go, or worse,
cding into the next directory followed by
lsing and then
cding again over and over (we’ve all been there), Z allows you to “lazy type” where you want to go and it’ll handle the rest.
It works by remembering which directories/paths you visit. It then weighs them by what it calls ‘Frecency’, a combination of ‘recent’ and ‘frequency’. So when I visit
/var/www/mysite/wp-content/themes for example, it will +1 the frequency and also mark down that I recently visited it. As you navigate around your filesystem, it keeps a hidden file with this data. By default, you’ll find it at
~/.z. It looks like this:
/usr/local/bin|9|1554673656 /var/www/mysite/wp-content/themes|7|1554674907 /var/www/staging/wp-content|3|1554673685 /var/www/mysite/wp-content/themes/storefront|1|1554673438 /var/www|3|1554674894 /var/www/mysite/wp-content|2|1554673434 /var/www/mysite|5|1554673659 /var/www/staging/wp-content/themes|2|1554673691 /var/www/mysite/wp-content/plugins|4|1554673669 /var/www/staging|7|1554674892
Looking at the above, you’ll see I’ve visited
/usr/local/bin the most. The last two numbers are tracking the frequency and how recent it was visited (time). As a result, typing
z bin is going to take me straight to
/usr/local/bin no matter where I currently am in the filesystem.
Enough about how it works behind the scenes though, you don’t really need to know any of that 🙂. Here is how you can use it.
Before we can use it, we need to install it. You can install the directory wherever you want really, I chose my local bin. Run this in your terminal (may require sudo):
cd /usr/local/bin && curl -O https://raw.githubusercontent.com/rupa/z/master/z.sh
Now that we’ve downloaded the script, we just need to make sure it’s included in our shell. To do this, you’ll need to edit your
.bashrc file (or similar). This would be
.zshrc if using ZSH.
// Edit using nano nano ~/.bashrc // Or open in text editor open ~/.bashrc // Or open in VS Code code ~/.bashrc
Once you have the file open, using whichever method you prefer, add the following at the bottom on its own lines (be sure to use whichever path you installed on):
# Include Z . /usr/local/bin/z.sh
Lastly, back in the terminal we need to refresh the bashrc file so our current shell is aware of the changes:
Now that it’s installed, we just need to navigate around to give it some history to work with.
cd around to some locations. Perhaps to your local WordPress install, go to the themes folder, then to plugins, etc. You don’t need to spend too much time here. Two or three locations is plenty.
It’s time for what you’ve been waiting for. Time to jump around! If you type just
z in your terminal, it should now show you the locations you’ve visited, along with the Frecency score. Going off of what I posted above, I can do the following:
// Takes me to /var/www/mysite/wp-content/themes z themes // Takes me to /var/www/staging/wp-content/themes z staging themes // Takes me to /var/www/mysite/wp-content/plugins z plu // Lists all the paths including "themes" in them. z themes -l
There are some extra flags you can pass to the command, such as ones to remove a location from this history (reset its Frecency). You can read more about the command from the GitHub page.
And, that’s pretty much it! Enjoy jumping around and quickly visiting the folders you want without painful navigating. While you play around with this command and set up your history, here is some background music 😜: