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

Allow quick open with pre-populated query and non-directories #76

Merged
merged 15 commits into from
Jan 13, 2021
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Use `fzf.fish` to interactively find and insert into the command line:
- **Key binding and mnemonic:** <kbd>Ctrl</kbd>+<kbd>F</kbd> (`F` for file)
- **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][])
- prepends `./` to the selection if only one selection is made, allowing for quickly execute a program or cd into a directory (see [cd docs][])
- if the current token is a directory with a trailing slash (e.g. `functions/<CURSOR>`), then search will be scoped to that directory
- ignores files that are also ignored by git
- <kbd>Tab</kbd> to multi-select
Expand Down
16 changes: 11 additions & 5 deletions functions/__fzf_search_current_dir.fish
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ function __fzf_search_current_dir --description "Search the current directory. R


if test $status -eq 0
# If this function was triggered with an empty command line and the only thing selected is a directory, then
# prepend ./ to the dir path. Because fish will attempt to cd implicitly if a directory name starting with a dot
# is provided, this allows the user to hit Enter one more time to quickly cd into the selected directory.
if test (count (commandline --tokenize)) = 0 && test (count $file_paths_selected) = 1 && test -d $file_paths_selected
set file_paths_selected ./$file_paths_selected
# 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 execute it.
# - if the path is a directory, the user can hit Enter one more time to quickly cd into it (fish will
# attempt to cd implicitly if a directory name starts with a dot)
if test (count $file_paths_selected) = 1
set commandline_tokens (commandline --tokenize)
set current_token (commandline --current-token)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kidonng please LMK if you have a better solution to command substitution resulting in an empty string. Seems fish doesn't allow you to do "(commandline --tokenize)" so I have to use variables.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further research I think this is the only way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you have to use a variable (which IMO is not bad), see https://fishshell.com/docs/current/index.html#cartesian-products.

if test "$commandline_tokens" = "$current_token"
set file_paths_selected ./$file_paths_selected
end
end

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