z cheatsheet
Using z on macOS is straightforward once installed. Follow these steps to get started and use z effectively:
1. Install z
Using Homebrew (Recommended):
Open your terminal. Run:
1
brew install z
2. Configure z
After installation, z needs to be sourced in your shell configuration file. For Zsh (default shell on macOS):
Open your .zshrc file for editing:
1
nano ~/.zshrc
Add the following line to load z:
1
. /opt/homebrew/etc/profile.d/z.sh
Replace /opt/homebrew
with the correct path if you’re using Intel-based macOS (default Homebrew path is /usr/local
).
Save and close the file (Ctrl + O, Enter, then Ctrl + X).
Reload your shell configuration:
1
source ~/.zshrc
3. Start Using z
z will automatically track the directories you visit using the cd command. Over time, it learns and prioritizes frequently and recently visited directories. Basic Commands:
Jump to a directory:
1
z <partial_name>
Example: If you have a directory ~/projects/work
, you can jump to it by:
1
z work
List matching directories: If multiple matches exist, use the -l option to list them:
1
z -l <partial_name>
Example:
1
z -l projects
Remove a directory from z’s database:
1
z -x <directory_path>
Example:
1
z -x ~/old_project
Show z’s database:
1
z -l
4. Tips for Effective Usage
Frequent Use: The more you navigate with cd, the smarter z becomes. It ranks directories based on frequency and recency of use. Combine with Other Commands: You can chain z commands with other commands. For example:
1
z projects && ls
This jumps to the directory and lists its contents.
5. Optional: Improve z with zoxide
For better performance and extended features, you can install zoxide, a modern version of z. Install zoxide:
1
brew install zoxide
Add zoxide to Zsh:
Edit your .zshrc:
1
nano ~/.zshrc
Add the following line:
1
eval "$(zoxide init zsh)"
Reload your shell:
1
source ~/.zshrc
Now you can use zoxide the same way as z!
Zoxide vs Z
zoxide replaces z; it is not designed to work alongside z. They are both tools for directory jumping, but zoxide is a modern reimplementation of z with better performance and additional features.
Here’s a breakdown of the differences and why you might choose one over the other:
Key Differences Between z and zoxide
Feature | z | zoxide |
---|---|---|
Installation | Simple, but relies on shell scripts. | Easy, modern binary. |
Performance | Slower as it uses a shell-based database. | Faster due to compiled Rust backend. |
Shell Support | Works for Bash, Zsh, etc. | Works for Bash, Zsh, Fish, etc. |
Command Usage | Requires cd usage for tracking. | Tracks directories automatically. |
Extra Features | Basic directory jumping. | Adds aliases, interactive search (fzf). |
Customizability | Limited. | Highly customizable. |