by latexr · 2y ago · 0 comments
Eventually I came up with an alternative using fd¹ and fzf². I have a variant of this in my .zshrc:
function n {
cd "$(fd . "${HOME}" --type d --color never | fzf --select-1 --query "${*}")"
ls
}
Call it with `n` and get an interactive fuzzy search for your directories. If you do `n <argument>`, it’ll start the find with `<argument>` already filled in (and if there’s only one match, jump to it directly). The `ls` is optional but I find that I like having the contents visible as soon as I change directories.In my personal setup I’m also including iCloud Drive while excluding the rest of the Library directory³ as that is too noisy. I have a separate `nl` function which searches just inside `~/Library` for when I need it, as well as other specialised `n<char>` functions that search inside specific places that I use frequently.
¹ https://github.com/sharkdp/fd
² https://github.com/junegunn/fzf
³ Change the fd command to: fd . "${HOME}" "${HOME}/Library/Mobile Documents/com~apple~CloudDocs" --exclude '/Library/' --type d --color never
That's a good intermediary solution. But the one that totally changed my flow was to combine autojump[1] and fzf. autojump is similar to Z (this submission). It stores all the directories you've visited in an SQLite DB and can show them to you (ordered by visit frequency) with a command line argument. So I pipe that to fzf.
Now I can extremely quickly jump to any directory I've been to before - it really helps that they're sorted by visit frequency. I honestly use this more than any other approach - and I probably go for days on end without using the usual TAB autocompletion.
In fish for example, it's as easy as the following (using zoxide query)
complete -c z -f -k -a "(zoxide query -l)"I've tweaked it to search a lot less folders (depth 5) and to restrict to a pattern beginning by X...
cd "$(fd "^${*}" "${HOME}" --max-depth 4 --type d --color never | fzf --select-1 --query "${*}")"
This makes it super snappy. I probably have waaay too many folders with similar names.ps: out of fun, I wanted to toy with fzf --multi mode
function zzg {
local selection="$(fd . "${HOME}" --type d --color never --max-depth 4 | fzf -m --query "${*}")";
local term="xterm"
for dir in $selection
do
"${term}" -title "zzg({$dir})" -e "cd ${dir} && bash" &
done
}I'm going to try out a version of this now.