ふと、ディレクトリの移動をもっと快適にしたいと思ったので、自分好みにcdコマンドをラップしてみました。
z、z.lua、zoxideのように一度行ったことのあるディレクトリを記憶しておき、2回目以降は$ z foo
のように実行すると「ディレクトリ名の一部が一致すれば最近アクセスしたディレクトリへ遷移できるというラッパー」も良さそうだったんですが、
foo以下のディレクトリが補完されて欲しい(正確にはfoo以下にどんなディレクトリがあるか分かり、そこに直接cdできて欲しい)という気持ちがあったので使うのはやめました。
最終的には以下のようにcdコマンドをラップすることにしました。
fd
で特定ディレクトリ配下のディレクトリ一覧を取得して、それをfzf
で曖昧検索し、そこにcd
できるようになっています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirements | |
# fd: https://github.com/sharkdp/fd | |
# fzf: https://github.com/junegunn/fzf | |
function cd() { | |
flag='-f' | |
if [ $# -eq 0 ]; then | |
builtin cd | |
elif [ $# -eq 1 ]; then | |
if [ $1 = "$flag" ]; then | |
dirs=$(fd --type d . $HOME | fzf) | |
if [ "$dirs" != '' ]; then | |
builtin cd "$dirs" | |
fi | |
else | |
builtin cd $1 | |
fi | |
elif [ $2 = "$flag" ]; then | |
target=${1:-$HOME} | |
dirs=$(fd --type d . $target | fzf) | |
if [ "$dirs" != '' ]; then | |
builtin cd "$dirs" | |
fi | |
fi | |
} |