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

[suggestion] be able to preselect the first exact match #1703

Closed
3 tasks done
FractalWire opened this issue Sep 28, 2019 · 2 comments
Closed
3 tasks done

[suggestion] be able to preselect the first exact match #1703

FractalWire opened this issue Sep 28, 2019 · 2 comments

Comments

@FractalWire
Copy link

FractalWire commented Sep 28, 2019

  • Category
    • scripting
  • OS
    • all
  • Shell
    • all

In a scripting perspective, I'm trying to preselect an entry on fzf when it starts.
That selection might be different from the --query option.
This option would allow fzf to keep context between two calls and could be called --selection for example.

Here a typical use case:

while true
do
    {read query; read key; read ret } <<< $(
        fzf --selection="$ret" \
            --print-query --query="$query" \
            --expect ctrl-h --preview "$cmd {}"
    )
    if test "$key" = 'ctrl-h'
    then
        cmd='somenewcommand'
    fi
done

Currently, the only way to pre-select an entry is to modify the query with the exact match. By doing that we lose the context of fzf, as both the query and the selection might matter.

@junegunn
Copy link
Owner

See my previous response to the suggestion: #1296 (comment). And also #395.

The point is, making fzf a fully-featured building block for terminal applications was never a goal to me. Even though there are ambitious users who try to implement all kinds of things with fzf, I'd like to keep fzf conceptually simple and small; an interactive Unix filter.

It's not clear to me what you're trying to achieve here (dynamically changing the preview command?), but I'd consider rearranging the order of input lines before feeding it to fzf, i.e. feed "preselected" entry as the first line. Or you could provide execute bindings to give extra options to "preview" the entry differently.

fzf --bind 'ctrl-h:execute:less {} < /dev/tty > /dev/tty'

@FractalWire
Copy link
Author

FractalWire commented Sep 29, 2019

Yes, I get your idea of simplicity over feature-complete for fzf. This is one of the reason this tool is so powerful.
I'll try to petition this feature a bit more none the less. Especially given that I actually implemented it yesterday in fzf.

It's not clear to me what you're trying to achieve here (dynamically changing the preview command?)

I'm trying exactly that. To give a bit more context, it's about building a fuzzy file explorer. We are discussing it here : #1679

In fact, in my use case, I need to pass a variable to the preview command for it to change. Given that the preview command looks like a black box, the easiest way to achieve that is to restart fzf with a preview command changed slightly.
It works fine, but I loose the cursor position, which, in my use case, is annoying.

I'd consider rearranging the order of input lines before feeding it to fzf, i.e. feed "preselected" entry as the first line

I tried that, but it is not satisfactory for me. keeping the cursor position is important, as you have similar enough entry around the one selected.

Now, the way I implemented it is a bit different from the way I asked for it. I don't select the first match of a string, but I select an entry at a particular index.
That index might be given by a similar option as --print-query, named --print-sel-idx, or it might be obtained by parsing the input and getting the line you're interested in, before feeding it to fzf.

I'll make a PR, just so that we can discuss this a bit further if you'd like: if it's simple enough for you or if you see some corner cases not covered by my implementation.

This change is just a few lines long, so I don't think it introduces a lot of new code complexity.
This is definitely an advanced scripting feature but it should not affect the way fzf works otherwise.

For my specific use case, see here: https://github.com/FractalWire/fexp
The master branch uses the current version of fzf, while, the working branch is meant to be used with the PR fzf version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants