もた日記

くだらないことを真面目にやる

Linuxメモ : Rust製のzoxideで利用頻度の高いディレクトリに高速移動

zoxide

github.com

Rust製のzoxideは利用頻度の高いディレクトリに高速移動できるコマンドラインツール。
zautojumpなどと同様なツール。


インストール

README.mdのインストール方法によると、バイナリのダウンロードやcargoでインストールできる。
また、fzfと連携できるのでfzfもインストールしておくとよい。

$ cargo install zoxide

次にshellに応じて設定を追加する。zshの場合は~/.zshrcに下記行を追加する。
これでzziといったコマンドが使えるようになる。

eval "$(zoxide init zsh)"

ヘルプメッセージ。

$ zoxide --help
zoxide 0.3.0
A cd command that learns your habits

USAGE:
    zoxide <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    add       Add a new directory or increment its rank
    help      Prints this message or the help of the given subcommand(s)
    import    Import from z database
    init      Generates shell configuration
    query     Search for a directory
    remove    Remove a directory


使い方

基本的な使い方は以下のとおり。

z foo       # cd to highest ranked directory matching foo
z foo bar   # cd to highest ranked directory matching foo and bar

z foo/      # can also cd into actual directories

zi foo      # cd with interactive selection using fzf

zq foo      # echo the best match, don't cd

za /foo     # add /foo to the database
zr /foo     # remove /foo from the database


まずは、インストールした後で普段通りにcd(またはz foo/のように移動)でディレクトリを移動して履歴を蓄積していく。
履歴が蓄積された後でziを実行すると移動履歴をもとに利用頻度の高い順にディレクトリが表示される。

f:id:wonder-wall:20200413234455p:plain

fzfと連携しているので絞り込んでEnterを押すとそのディレクトリに移動できる。

f:id:wonder-wall:20200413234507p:plain

インタラクティブな選択が不要であれば、z foo barのように実行することで利用頻度の高いマッチするディレクトリに直接移動できる。
もし、履歴にはあるが移動したくないディレクトリがあればzr fooのようにすることで履歴から削除できる。


類似ツール

類似ツールとしては下記がある。