junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
65.97k stars 2.41k forks source link

[Feature request] Placeholder expression for space-separated query string #4033

Closed letuanhai closed 1 month ago

letuanhai commented 1 month ago

Checklist

Output of fzf --version

0.55.0

OS

Shell

Problem / Steps to reproduce

Any other suggestions without the need for the additonal placeholder expression would also be great.

LangLangBart commented 1 month ago
  • evironment variable $FZF_QUERY in the event binding (i.e. --bind "start:reload:$RG_PREFIX $FZF_QUERY"), but it does not get replaced with the query string.

The FZF_QUERY is an environment variable exported by fzf that you must escape when used with double quotes; otherwise, it will expand immediately.

--bind "start:reload:$RG_PREFIX \$FZF_QUERY"

[!NOTE] fzf runs the command with $SHELL -c if SHELL is set, otherwise with sh -c

essentially doing shell word splitting.

Try the (z)[^1][^2] and (Q)[^3] parameter expansion flags on FZF_QUERY.

export test_string='--glob !*.go "immediately after"'
zsh -fxc 'print -- ${(Q)${(z)test_string}}' 
# +zsh:1> print -- --glob '!*.go' 'immediately after'
# --glob !*.go immediately after

TEST: Search for the string immediately after in the fzf repo with the option --glob !*.go passed along.

SHELL=$(which zsh) fzf \
  --bind "start:become:rg --column --line-number --no-heading \${(Q)\${(z)FZF_QUERY}}" \
  --disabled \
  --query '--glob !*.go "immediately after"'

# CHANGELOG.md:2132:33:- Fixed to update "inline-info" immediately after terminal resize

Instead of SHELL=$(which zsh), one can also use the --with-shell "$(which zsh) -c" flag. The latter allows passing additional options. This feature was added in version 0.51.0[^4].

[^1]: zsh: 14 Expansion [^2]: Zsh Parameter Expansion z [^3]: Zsh Parameter Expansion Q [^4]: fzf/CHANGELOG.md - 0.51.0

letuanhai commented 1 month ago

Wow, it works like a charm 😀 Thank you for your reply.

The caveat is that the parameter options (z) and (Q) are specific to zsh, though I'm fine with that since I'm using zsh.

The FZF_QUERY is an environment variable exported by fzf that you must escape when used with double quotes; otherwise, it will expand immediately.

I think this note should be added to the documentation somewhere, since I didn't find example about the use of the environment variable FZF_QUERY in my research, and someone who is not so familiar with shell script, like myself, would miss this point.

junegunn commented 1 month ago

I think this note should be added to the documentation somewhere, since I didn't find example about the use of the environment variable FZF_QUERY in my research

You can find it in the man page.

https://github.com/junegunn/fzf/blob/e54ec05709cf18fd0f76b885f6ae7f5f3a1095ad/man/man1/fzf.1#L1095-L1134

letuanhai commented 1 month ago

Hi, thanks for checking the issue again.

You can find it in the man page.

Yes I've found out about the environment variable from the man page. That's why I've mentioned it in my original post. However, I didn't find any example of its usage, and was failed to use it correctly.

What I think should be documented is the following note about its usage, as it's not very straightforward, and someone who is inexperienced with shell scripting may encounter trouble:

The FZF_QUERY is an environment variable exported by fzf that you must escape when used with double quotes; otherwise, it will expand immediately. --bind "start:reload:$RG_PREFIX \$FZF_QUERY"