Zsh快速路径跳转之zoxide

Zsh中实现快速路径跳转, 之前已经介绍了z.luaZsh配置快速路径跳转)。今天看到了另一个zsh的插件zoxide, 它是用rust语言写的,所以效率上应当是更高的。至于为什么会由z.lua切换到zoxide, 这里我也想不出更好的理由,但是大家可以参考文章zoxide VS z.luazsh-z VS zoxide, 今天我是做为尝试而切换到zoxide, 当然z.lua依然优秀。

zoxide 简介

zoxide is a smarter cd command, inspired by z and autojump.

It remembers which directories you use most frequently, so you can "jump" to them in just a few keystrokes.zoxide works on all major shells.

Getting started

1
2
3
4
5
6
7
8
9
10
11
12
z foo              # cd into highest ranked directory matching foo
z foo bar # cd into highest ranked directory matching foo and bar
z foo / # cd into a subdirectory starting with foo

z ~/foo # z also works like a regular cd command
z foo/ # cd into relative path
z .. # cd one level up
z - # cd into previous directory

zi foo # cd with interactive selection (using fzf)

z foo<SPACE><TAB> # show interactive completions (zoxide v0.8.0+, bash 4.4+/fish/zsh only)

Installation

install zoxide
1
sudo pacman -S zoxide

Configure

~/.zshrc
1
2
3
4
5
6
# set for zsh-z-git
# source /usr/share/zsh/plugins/zsh-z/zsh-z.plugin.zsh
# set for z.lua 功能上不如zsh-z-git完善
# eval "$(lua /usr/share/z.lua/z.lua --init zsh enhanced once)"
# set for zoxide instead of z.lua
eval "$(zoxide init zsh)"

对比配置信息来看,zoxide 好像更加简洁一些,但是这不是主要的切换依据,几个工具都可以尝试。我认为z.lua是从aur中安装的,但是zoxide是从Archlinux的官方仓库中安装,使用官方的工具我认为更可靠一些。

Import your data

If you currently use any of these plugins, you may want to import your data

autojump

Run this command in your terminal:

1
zoxide import --from=autojump "/path/to/autojump/db"

The path usually varies according to your system:

OS Path Example
Linux $XDG_DATA_HOME/autojump/autojump.txt or $HOME/.local/share/autojump/autojump.txt /home/alice/.local/share/autojump/autojump.txt
macOS $HOME/Library/autojump/autojump.txt /Users/Alice/Library/autojump/autojump.txt
Windows %APPDATA%\autojump\autojump.txt C:\Users\Alice\AppData\Roaming\autojump\autojump.txt
fasd, z, z.lua, zsh-z

Run this command in your terminal:

1
zoxide import --from=z "path/to/z/db"

The path usually varies according to your system:

Plugin Path
fasd $_FASD_DATA or $HOME/.fasd
z (bash/zsh) $_Z_DATA or $HOME/.z
z (fish) $Z_DATA or $XDG_DATA_HOME/z/data or $HOME/.local/share/z/data
z.lua (bash/zsh) $_ZL_DATA or $HOME/.zlua
z.lua (fish) $XDG_DATA_HOME/zlua/zlua.txt or $HOME/.local/share/zlua/zlua.txt or $_ZL_DATA
zsh-z $ZSHZ_DATA or $_Z_DATA or $HOME/.z
ZLocation

Run this command in PowerShell:

1
2
3
$db = New-TemporaryFile
(Get-ZLocation).GetEnumerator() | ForEach-Object { Write-Output ($_.Name+'|'+$_.Value+'|0') } | Out-File $db
zoxide import --from=z $db