How to automatically change to the first matching directory using fuzzy search and cd?
+2
−1
I want the cd
command to automatically change to the first directory that matches a fuzzy search, without prompting me to select between multiple options.
This is what I've tried:
cd() {
local dir="$1"
local selected_dir
selected_dir=$(find . -depth 1 -type d 2>/dev/null | fzf +m -q "$dir" --height 40% --reverse --select-1)
builtin cd "$selected_dir"
}
This prompts me to select between different directories but I want to change automatically to the first match.
1 answer
+0
−0
Possibly -f "$dir"
not -q "$dir"
. According to man:
-q, --query=STR
Start the finder with the given query
...
-f, --filter=STR
Filter mode. Do not start interactive finder. When used with --no-sort, fzf becomes a fuzzy-version of grep.
If --select-1
doesn't then do what you expect (i.e. emit the first result), replace with | head -n1
(i.e. pipe the output of fzf
to head
to select the first result).
2 comment threads