cd into saved directories with fzf
This hack is primarily the fzf-cd-widget
, but adapted to read from a file. Works only
with zsh
shells.
In a file add one path per line ~/data/locations.txt
:
~/path/1
~/path/2
...etc
loc_picker() {
setopt localoptions pipefail no_aliases 2>/dev/null
dir=$(cat "${HOME}/data/locations.txt" | fzf --no-multi)
# exit gracefully on ESC
if [[ -z $dir ]]; then
zle reset-prompt
return
fi
zle push-line # Clear buffer. Auto-restored on next prompt.
BUFFER="builtin cd -- ${dir}"
zle accept-line
local ret=$?
unset dir # ensure this doesn't end up appearing in prompt expansion
zle reset-prompt
return $ret
}
zle -N loc_picker
# bind to ctrl-p
bindkey '^p' loc_picker
You can find more cd
options in the fzf wiki.