0

Using zoxide - smarter cd command for Linux/macOS | ComputingForGeeks

 6 months ago
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.
smarter cd command for Linux/macOS

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.

export PATH="/home/$USER/.local/bin:$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.

vim ~/.config/fish/config.fish

Now add the lines below to it.

zoxide init fish | source
  • For Xonsh

Open the below file for editing.

vim ~/.xonshrc

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:

config path

Add the below line to it.

startup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"]

Configure zoxide.

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 j would 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

Use Zoxide

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.

z foo

For example.

z /home/thor/Downloads/

Sample output:

To exit to the previous directory, use the command:

z -

You can also cd one level up using the command:

z ..

With two directories sharing some similar name(foo), you can start an interactive shell for selection using the syntax.

zi foo

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

zi Downloads

Sample output:

You can also launch the interactive shell using the below syntax.

z foo<SPACE>+<TAB>

Here, you will run the command as below.

z Downloads

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:

z foo

For example, navigating into the /home/thor/Downloads/ directory, which is highest-ranked here with the command:

z Downloads

Sample Output:

You can as well navigate into the highest-ranked directory using two arguments for better matching say new/Downloads as below:

z new Downloads

Sample output:

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:

  • Ranger

This is a File manager integration that enables one to jump between commonly visited directories by running the command.

z <partial-name>

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.

Conclusion

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.

See more:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK