Using zoxide - smarter cd command for Linux/macOS | ComputingForGeeks
source link: https://computingforgeeks.com/using-zoxide-smarter-cd-command-for-linux-macos/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
One of the major operations on the terminal is navigating into various directories on your system. There are several directories with different naming on Linux/macOS. Working with these directories at times can be really confusing.
Using zoxide really helps when navigating between Linux/macOS. This is a shell plugin that can help improve productivity when working at the shell. The zoxide plugin is a replacement for the common
cdcommand inspired by z and z.lua. It simplifies the task by keeping track of frequently visited directories using a ranking algorithm to navigate to the best match. With just a few keystrokes, one is able to navigate into the desired directory.
This guide aims to illustrate how you can use the zoxide – smarter cd command for Linux / macOS with an added shell completion for a more intuitive experience.
Step 1 – Install zoxide
Zoxide works on innumerable platforms and can be installed as below.
Install zoxide on Linux
Zoxide is easy to install on Linux systems since it is available in default package repositories for many distributions.
Install zoxide on your Linux distribution using the command:
##On Debian/Ubuntu sudo apt install zoxide ##On CentOS 8/Rocky Linux 8/Alma Linux 8 sudo dnf copr enable atim/zoxide sudo dnf install zoxide ##On Arch Linux based system sudo yay -Sy zoxide-bin ##Fedora sudo dnf install zoxide ##Alpine Linux sudo apk add zoxide ##Manjaro sudo pacman -S zoxide
You can also use the script below to install zoxide on your system.
curl -sS https://webinstall.dev/zoxide | bash
Export your path.
Install zoxide on macOS
On macOS, zoxide can be installed using the below commands:
##HomeBrew brew install zoxide ##MacPorts port install zoxide ##conda-forge conda install -c conda-forge zoxide
For both Linux and macOS systems, zoxide can be installed from the crates repository using the command:
cargo install zoxide --locked
Verify the installation as below:
$ zoxide -V zoxide v0.7.9
Step 2 – (optional) Install fzf
fzf (command-line fuzzy finder) is used by zoxide to provide an interactive shell and can be installed as below.
- On Linux
fzf is available in the default package repository on many Linux distributions:
##On Debian/Ubuntu sudo apt-get install fzf ##On Fedora sudo dnf install fzf ##On Arch Linux sudo pacman -S fzf ##On openSUSE sudo zypper install fzf
Alternatively, install fzf from the git repo as below.
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install source ~/.bashrc
- On macOS
Use HomeBrew to install fzf as below.
$ brew install fzf # To install useful key bindings and fuzzy completion: $(brew --prefix)/opt/fzf/install
Step 3 – Add zoxide to your shell
To be able to use zoxide, you need to add it to your shell as shown.
- For Bash/Zsh
Open the bash file using your favorite editor.
$ vim ~/.bashrc ##OR $ vim ~/.zshrc
In the opened file, add the below lines.
eval "$(zoxide init bash)"
Save the file and source the profile
source ~/.bashrc ##OR source ~/.zshrc
- For Fish.
Open the file.
Now add the lines below to it.
zoxide init fish | source
- For Xonsh
Open the below file for editing.
Add the lines below to it.
execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide')
- For Nushell
Find the configuration file by executing the command in Nushell:
Add the below line to it.
startup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"]
There are several configurations one can make in zoxide. Set environment variables for zoxide normally before the
zoxide initis executed
These environment variables are set as below:
- _ZO_DATA_DIR: this is used to specify the directory in which the database is stored. The default directories are:
- Linux / BSD: $XDG_DATA_HOME or $HOME/.local/share
- macOS: $HOME/Library/Application Support
- _ZO_ECHO: This is used to print the matched directory before navigating into it when set to 1
- _ZO_EXCLUDE_DIRS: this is used to include the specified directories from the database. By default this is set to “$HOME“. Separators (:)can be used for example $HOME:$HOME/private/*
- _ZO_MAXAGE: Configures the aging algorithm, which limits the maximum number of entries in the database.
- _ZO_RESOLVE_SYMLINKS: When set to 1, z will resolve symlinks before adding directories to the database.
- _ZO_FZF_OPTS: used to set custom fzf options to be executed during interactive selection.
There are other configurations one can make to zoxide when calling
zoxide initsuch as:
- –cmd: this is used to chnage prefixed of defined aliases(z and Zi) for example
-cmd jwould change the aliases to (j, ji)
- –hook <HOOK>: this defines how often zoxide increments a directory’s score. The following arguments can be used along with the hook command:
- none – for never
- prompt – to prompt for every shell
- pwd – Whenever the directory is changed
- –no-aliases: This is used to define no aliases (z, zi) these function will be available as __zoxide_z and __zoxide_zi
Zoxide works in a similar way to the cd command but now has more added features. Using the default z alias, navigate into a directory using the syntax.
To exit to the previous directory, use the command:
You can also cd one level up using the command:
With two directories sharing some similar name(foo), you can start an interactive shell for selection using the syntax.
For example here, I have two directories /home/thor/Downloads and /home/thor/new/Downloads and I have navigated into both folders now I will start the interactive shell for selection as below
You can also launch the interactive shell using the below syntax.
Here, you will run the command as below.
Then press Space + TAB keys to see the output:
If you have multiple paths with the same name(foo), Zoxide will remember them all and you can navigate into them using the rank in the provided name list.
You can navigate to the highest-ranked directory matching the name(foo) using the command:
For example, navigating into the /home/thor/Downloads/ directory, which is highest-ranked here with the command:
You can as well navigate into the highest-ranked directory using two arguments for better matching say new/Downloads as below:
z new Downloads
Now you can navigate into the desired directory with zoxide and it remembers them all, making navigation so easy and with shorter strings.
Third Party Intergrations.
There are several integrations for zoxide with some such as zsh-autocomplete supported by default. You can install them and use them as below:
This is a File manager integration that enables one to jump between commonly visited directories by running the command.
his can be installed as below.
sudo git clone https://github.com/jchook/ranger-zoxide.git ~/.config/ranger/plugins/zoxide
For example, navigate into the Downloads directory using the partial command below and press Enter.
Now use ranger for faster navigation between directories. There are many third-party integrations for zoxide such as vim, xplr, xxh, emacs e.tc well explained on the zoxide GitHub page.
That marks the end of this guide on how to install zoxide – smarter cd command for Linux / macOS and use it to simply the cd command. I hope this was significant to you.
Aggregate valuable and interesting links.
Joyk means Joy of geeK