Zoxide smart CD command in PowerShell

November 8th 2024 PowerShell

While watching a video on coding environment and workflow I learned about zoxide, a smarter cd command. I checked it out in more detail and was pleasantly surprised that it's also available for PowerShell on Windows. Of course, I had to try it out, and after a short period of testing I decided to make it a permanent part of my PowerShell configuration, along with several other PowerShell modules I'm already using.

The installation process is simple and well documented. The tool is available in multiple package managers. I'm still using Chocolatey, so I used the following command from a terminal with administrative privileges:

choco install zoxide

For full functionality, you also need to install fzf which I have done already as part of the PSFzf installation process. With Chocolatey, you can do it with the following command:

choco install fzf

To make the command available in PowerShell, you need to modify your PowerShell configuration file. If you're a Visual Studio Code user, you can edit it by running the following command from your PowerShell terminal:

code $PROFILE

You need to add the following line to the end of your file:

Invoke-Expression (& { (zoxide init powershell | Out-String) })

Once you save the changes, you can apply the new configuration file to your terminal without reopening it with the following command:

. $PROFILE

With that, the zoxide command becomes available.

And what does it do? It remembers previously visited directories, allowing you to navigate to them without typing out the full path. Until now, I've used PSFzf for that: I typed a part of the destination path, pressed Ctrl + R to access command history and selected the desired command from the list:

Searching command history with PSFzf

The process with zoxide is even simpler. You just need to consistently use z instead of cd to change directories. The first time you navigate to a directory, you need to enter the whole path. In subsequent attempts you only need to enter a part of the path and the command will automatically navigate to the best match:

Navigating to directories with zoxide

I like customizing my PowerShell configuration to make my work more efficient and generally more pleasant. I was already pretty satisfied with my configuration, but adding zoxide to it made the process of navigating to frequently used directories even better. It works particularly well for one of my work projects, where I often navigate to one of many microservice directories. I can only use a part of their name instead of the full path to reliably navigate to the right one.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License