Skip to content

Commit 5e15aeb

Browse files
authored
Allow quick open with pre-populated query and non-directories (#76)
Make the quick cd behavior trigger even if the user was starting to type a token and even if the path is not a directory. Before, it would only prepend ./ if the command line was empty and the selected path is a directory. Now, if the user was in the middle of inputting the first token and only one path is selected, then prepend ./ to the selected path so that - if the path is an executable, the user can hit Enter one more time to immediately execute it - if the path is a directory, the user can hit Enter one more time to immediately cd into it
1 parent 8bacfb3 commit 5e15aeb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Use `fzf.fish` to interactively find and insert into the command line:
2020
- **Key binding and mnemonic:** <kbd>Ctrl</kbd>+<kbd>F</kbd> (`F` for file)
2121
- **Preview window:** file with syntax highlighting, directory contents, or file type
2222
- **Remarks**
23-
- appends `./` to the selection if the selection is a single directory, allowing for quick cd into that directory (see [cd docs][])
23+
- prepends `./` to the selection if only one selection is made and it becomes the only token on the command line, making it easy to execute if an executable, or cd into if a directory (see [cd docs][])
2424
- if the current token is a directory with a trailing slash (e.g. `functions/<CURSOR>`), then search will be scoped to that directory
2525
- ignores files that are also ignored by git
2626
- <kbd>Tab</kbd> to multi-select

functions/__fzf_search_current_dir.fish

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ function __fzf_search_current_dir --description "Search the current directory. R
2121

2222

2323
if test $status -eq 0
24-
# If this function was triggered with an empty command line and the only thing selected is a directory, then
25-
# prepend ./ to the dir path. Because fish will attempt to cd implicitly if a directory name starting with a dot
26-
# is provided, this allows the user to hit Enter one more time to quickly cd into the selected directory.
27-
if test (count (commandline --tokenize)) = 0 && test (count $file_paths_selected) = 1 && test -d $file_paths_selected
28-
set file_paths_selected ./$file_paths_selected
24+
# If the user was in the middle of inputting the first token and only one path is selected,
25+
# then prepend ./ to the selected path so that
26+
# - if the path is an executable, the user can hit Enter one more time to immediately execute it
27+
# - if the path is a directory, the user can hit Enter one more time to immediately cd into it (fish will
28+
# attempt to cd implicitly if a directory name starts with a dot)
29+
if test (count $file_paths_selected) = 1
30+
set commandline_tokens (commandline --tokenize)
31+
set current_token (commandline --current-token)
32+
if test "$commandline_tokens" = "$current_token"
33+
set file_paths_selected ./$file_paths_selected
34+
end
2935
end
3036

3137
commandline --current-token --replace (string escape $file_paths_selected | string join ' ')

0 commit comments

Comments
 (0)