DEV Community

Cover image for zoxide - A faster alternative to boring cd command
Chamal Randika
Chamal Randika

Posted on

zoxide - A faster alternative to boring cd command

Why zoxide is a better replacement??

Best way to start explaining is to show how we normally navigate using the good old cd command.

Usual cd command usage

Now you might think that "hey, can't we just use up arrow key or an autocomplete plugin?".

Well, those might perform very well when working with simple folder structures, but imagine having to press up arrow key thousands of times to get to your previously used directory that wanted to navigate to.

And also in the case of auto-complete plugin, it will also leave a very unreadable number of lines in the terminal that might confuse you when you referring back the previous lines of your terminal for reviewing something.

That's where zoxide excels!

Both of those problems can be easily be eliminated just by installing zoxide to your terminal.

Now let's see how we do the same operation using Zoxide.

zoxide navigate command usage

Pretty easy right?

Well if you have multiple folders with the same name in different directories there are two ways you can use zoxide to get to the exact one you want.

Method 1 - If you are on a compatible shell version of bash,zsh or fish you will be automatically prompted with the available directories for that folder name as follows.

Multiple directories on zoxide method 1

Method 2 - If for some reason you don't get the above feature, you can use another famous tool called fzf (aka fuzzy finder). With the power of fzf you can use the below command to show all the directories saved with that particular folder name. You can even take a peak at the content inside that folder.

Multiple directories on zoxide method 2

Not only that there are many other ways you can customize the commands. You'll find a very detailed instruction set in their official repo which I have linked at the end of this article.

Actually zoxide isn't the first program that provided these facilities. There were earlier programs which are still up-to-date named z, autojump and fasd. While autojump and fasd are way more advanced than zoxide, they also have a more steep learning curve.

In terms of simplicity and lightweightness, zoxide is the best option out of all. On top of that, zoxide is built on Rust which makes it blazing fast.

Believe me you won't even notice that you're using a third party tool.

Okay now we'll see how to install this into your favorite terminal.

zoxide will work with any Linux terminal including Windows and MacOS. Infact it is available through almost any package manager you have heard, used or seen.

For Linux/MacOS

Although the official documentation of zoxide recommends using package installers or managers, I would highly suggest you to use curl to get the install script and install.



curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | bash


Enter fullscreen mode Exit fullscreen mode

For Windows

zoxide works really well with Powershell in Windows.

Use winget package manager to install zoxide.



winget install ajeetdsouza.zoxide


Enter fullscreen mode Exit fullscreen mode

Post install steps (only for Linux/MacOS)

  1. Open your configuration file related your terminal using a code editor.


# Assuming you are on zsh and using nano as the code editor
nano .zshrc


Enter fullscreen mode Exit fullscreen mode
  1. Add the following line to the end of the file to initialize zoxide with your terminal.

eval "$(zoxide init zsh)"

(skip this step if it is already in the file)

  1. Now add following line to add the zoxide install directory to the PATH which helps you to use the z command in every directory of your terminal.

export PATH="/Users/chamalrandika/.local/bin:$PATH"

  1. Source the configuration file to invoke the changes.


source .zshrc


Enter fullscreen mode Exit fullscreen mode

Install fzf (if multiple directory handling didn't work with the default z syntax)

  1. clone the official repo to get the install script.


git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf


Enter fullscreen mode Exit fullscreen mode
  1. Install


~/.fzf/install


Enter fullscreen mode Exit fullscreen mode
  1. Add this line to configuration file as explained in previous steps to reduce the width of the suggestions window (default is 100%)

export FZF_DEFAULT_OPTS='--height 40%'

  1. Source the configuration file.

More information

You can refer the official documentation if you are an advanced user or just curious to know more about the projects.

That's it!

Happy CLI life devs. ✌️

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair
  • You can switch between the last directories with cd -, which should be a familiar convention if you've used other commands like git, etc.
  • CDPATH is a simple way of managing preferred directories, and is built-in as part of POSIX.
  • Most shells that use readline will allow you to interactively search through your history with ctrl-R or the equivalent.

I mention these because they show how you can do most of what Zoxide offers without installing anything. I mean, of course you can install whatever you like, but Zoxide won't be available on the machine you ssh into next week, or your co-worker's laptop, etc., whereas the basics will. It's that as a top tip to newbies, it's usually a good idea to familiarise yourself with what's already there, then add on utilities to fit your productivity style afterwards.

Collapse
 
chamal1120 profile image
Chamal Randika

Thanks a lot man I didn't know about these. You're a total neckbeard I see. I mean zoxide offering what native commands can do with less keystrokes. Just like how Vim offers vimgrep and regex commands to code refactor or you can use some LSP plugins to do the same. There will be always a built in method to do 90% of the things in most scenarios in POXIS systems. These are just a tool to customize your shell experience and impress the friends a lil bit.

Collapse
 
vapg1974 profile image
Nosotros Los Lobos Estamos Regresando

Thank you. As a newcomer, your words of wisdom are invaluable.