Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit file search to directory under cursor if it has a trailing slash #75

Merged
merged 14 commits into from
Jan 9, 2021
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Use `fzf.fish` to interactively find and insert into the command line:
- **Preview window:** file with syntax highlighting, directory contents, or file type
- **Remarks**
- appends `./` to the selection if the selection is a single directory, allowing for quick cd into that directory (see [cd docs][])
- if the current is a directory name with a trailing slash (e.g. `functions/<cursor position>`), then search will be scoped to that directory
- ignores files that are also ignored by git
- <kbd>Tab</kbd> to multi-select

Expand Down
21 changes: 17 additions & 4 deletions functions/__fzf_search_current_dir.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ function __fzf_search_current_dir --description "Search the current directory us
# Make sure that fzf uses fish to execute __fzf_preview_file.
# See similar comment in __fzf_search_shell_variables.fish.
set --local --export SHELL (command --search fish)
set file_paths_selected (
fd --hidden --color=always --exclude=.git 2>/dev/null |
fzf --multi --ansi --preview='__fzf_preview_file {}' --query=(commandline --current-token)
)

set fd_arguments --hidden --color=always --exclude=.git
set fzf_arguments --multi --ansi --preview='__fzf_preview_file {}'
set token (commandline --current-token | string unescape)
PatrickF1 marked this conversation as resolved.
Show resolved Hide resolved

# If the current token a directory with a trailing slash,
# then use it as fd's base directory.
if string match --quiet "*/" $token && test -d $token
set --append fd_arguments --base-directory=$token
# use the directory name as fzf prompt to show the search is limited to that directory
set --append fzf_arguments --prompt=$token
set file_paths_selected $token(fd $fd_arguments 2>/dev/null | fzf $fzf_arguments)
else
set --append fzf_arguments --query=$token
set file_paths_selected (fd $fd_arguments 2>/dev/null | fzf $fzf_arguments)
end


if test $status -eq 0
# If this function was triggered with an empty commandline and the only thing selected is a directory, then
Expand Down