home-config/fish/functions/_fzf_search_directory.fish
ce1c24e3100d48c9c210eb1ebc5ebadcc6b77659
· 1.9 KB · 33 lines
raw
| 1 | function _fzf_search_directory --description "Search the current directory. Replace the current token with the selected file paths." |
| 2 | # Directly use fd binary to avoid output buffering delay caused by a fd alias, if any. |
| 3 | # Debian-based distros install fd as fdfind and the fd package is something else, so |
| 4 | # check for fdfind first. Fall back to "fd" for a clear error message. |
| 5 | set -f fd_cmd (command -v fdfind || command -v fd || echo "fd") |
| 6 | set -f --append fd_cmd --color=always $fzf_fd_opts |
| 7 | |
| 8 | set -f fzf_arguments --multi --ansi $fzf_directory_opts |
| 9 | set -f token (commandline --current-token) |
| 10 | # expand any variables or leading tilde (~) in the token |
| 11 | set -f expanded_token (eval echo -- $token) |
| 12 | # unescape token because it's already quoted so backslashes will mess up the path |
| 13 | set -f unescaped_exp_token (string unescape -- $expanded_token) |
| 14 | |
| 15 | # If the current token is a directory and has a trailing slash, |
| 16 | # then use it as fd's base directory. |
| 17 | if string match --quiet -- "*/" $unescaped_exp_token && test -d "$unescaped_exp_token" |
| 18 | set --append fd_cmd --base-directory=$unescaped_exp_token |
| 19 | # use the directory name as fzf's prompt to indicate the search is limited to that directory |
| 20 | set --prepend fzf_arguments --prompt="Directory $unescaped_exp_token> " --preview="_fzf_preview_file $expanded_token{}" |
| 21 | set -f file_paths_selected $unescaped_exp_token($fd_cmd 2>/dev/null | _fzf_wrapper $fzf_arguments) |
| 22 | else |
| 23 | set --prepend fzf_arguments --prompt="Directory> " --query="$unescaped_exp_token" --preview='_fzf_preview_file {}' |
| 24 | set -f file_paths_selected ($fd_cmd 2>/dev/null | _fzf_wrapper $fzf_arguments) |
| 25 | end |
| 26 | |
| 27 | |
| 28 | if test $status -eq 0 |
| 29 | commandline --current-token --replace -- (string escape -- $file_paths_selected | string join ' ') |
| 30 | end |
| 31 | |
| 32 | commandline --function repaint |
| 33 | end |