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

Add flag to select single result immediately #27

Closed
wellle opened this issue Mar 19, 2014 · 8 comments
Closed

Add flag to select single result immediately #27

wellle opened this issue Mar 19, 2014 · 8 comments

Comments

@wellle
Copy link
Contributor

wellle commented Mar 19, 2014

When providing a --query=foo parameter it could be useful to select the only match immediately when there is only one. When there are multiple matches the selection happens as usual.

Could look like this:

fzf --query=foo --select-single

This is related to #26.

@junegunn
Copy link
Owner

I see. It could be useful. One fact that we have to consider is that fzf doesn't block user input while it's receiving lines from stdin. So for example,

(echo hello; sleep 1; echo world) | fzf --query o

fzf will first match hello, then after 1 second, it will match world as well.
It's possible to defer selecting the only match until fzf completely consumed the source, but during the period the user can type in more characters, which I think can lead to some confusion.

Hmm, I might make fzf block only when the option is given. What do you think?

@wellle
Copy link
Contributor Author

wellle commented Mar 19, 2014

That sounds very reasonable.

I'm not sure if this makes matters worse, but you could stop blocking when you find the second match. Because at that point it's clear that it won't be the only match. Also typing more characters in between could just reset that option.

But all of this is highly optional. In the use case I had in mind there aren't too many files, so blocking would be no issue there.

@junegunn
Copy link
Owner

Those are indeed good ideas.

By the way, I have given some thought, and it seems like we can implement the feature with some shell scripting using --filter option.

fq1() {
  local lines
  lines=$(fzf --filter="$1" --no-sort)
  if [ -n "$lines" -a $(wc -l <<< "$lines") -eq 1 ]; then
    echo "$lines"
  else
    echo "$lines" | fzf --query="$1"
  fi
}

This is not as compact or easy as the suggested option, however I usually try to take the minimalist approach and tend to not add features that can be accomplished otherwise. So do you still feel this option should be implemented? I'm open to your opinion.

@wellle
Copy link
Contributor Author

wellle commented Mar 19, 2014

Oh that's amazing! This is what I am using now:

function fq1() {
  local lines
  lines=$(fzf --filter="$1" --no-sort)
  if [ -n "$lines" -a $(wc -l <<< "$lines") -eq 1 ]; then
    echo "$lines"
  else
    echo "$lines" | fzf --query="$1"
  fi
}

function ze() {
    FILE=$(fq1 $1) && vim "$FILE"
}

I am pleased with this and agree that there is no need to bake it into fzf. You might want to mention that function in the Readme as I believe it's very useful.

Thank you!

@wellle wellle closed this as completed Mar 19, 2014
@junegunn
Copy link
Owner

Okay, I'm glad you like it, thanks!

@junegunn
Copy link
Owner

A slight improvement to the function: Exit when there's no match

function fq1() {
  local lines
  lines=$(fzf --filter="$1" --no-sort)
  if [ -z "$lines" ]; then
    return 1
  elif [ $(wc -l <<< "$lines") -eq 1 ]; then
    echo "$lines"
  else
    echo "$lines" | fzf --query="$1"
  fi
}

@wellle
Copy link
Contributor Author

wellle commented Mar 19, 2014

👍

@mragab
Copy link

mragab commented Jun 20, 2023

If all you need is to get the best match in a non-interactive way:

fzf --filter="$1" | head -n 1

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

No branches or pull requests

3 participants