junegunn / fzf

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

[Feature Request] Customizable shell command flag for command execution #3732

Closed LangLangBart closed 2 months ago

LangLangBart commented 3 months ago

Checklist

Output of fzf --version

0.50.0 (brew)

OS

Shell

Problem / Steps to reproduce

This report was created from a discussion on a discarded PR: #3726


Issue

fzf currently executes commands with $SHELL and a hardcoded -c flag.

https://github.com/junegunn/fzf/blob/f97d2754134607b24849fc4a2062dbfcaafddd6a/src/util/util_unix.go#L22-L29

If a user wants to use a different tool with a different flag to execute commands, they must prepend the desired command to each string, making proper quoting more cumbersome and leading to repetitive addition of the command.

# [bun] https://github.com/oven-sh/bun
fzf --bind "start:reload:bun --print \"Object.keys(Bun.env).sort().join('\n')\""

Feature Proposal

Ability to change the flag that is passed to the $SHELL command.

If the flag would be subject to change, then one would only need to write the string.

# Syntax example, not actual usage
SHELL=$(which bun) FZF_SHELL_FLAG="--print" \
  fzf --bind "start:reload:Object.keys(Bun.env).sort().join('\n')"

# Different syntax example proposed by the maintainer
fzf --with-shell "bun --print" \
  --bind "start:reload:Object.keys(Bun.env).sort().join('\n')"

Here are a few examples of tools with their corresponding flags to illustrate the variety of flags that might be used:


Workaround

The maintainer provided a workaround [^4].

fzf2() (
  export SHELL=/tmp/fzfsh
  if ! [[ -x $SHELL ]]; then
    echo '#!/bin/sh' > "$SHELL"
    echo 'shift; echo "$@"' >> "$SHELL"
    chmod +x "$SHELL"
  fi
  command fzf "$@"
)

fzf2 --preview 'run this command: {}'

[^1]: JSC – WebKit [^2]: Bun — A fast all-in-one JavaScript runtime [^3]: JXA - JavaScript for Automation [^4]: PR #3726 (comment)

junegunn commented 2 months ago

The suggestion here is to add an option similar to shellcmdflag of Vim: https://vimdoc.sourceforge.net/htmldoc/options.html#'shellcmdflag'

Personally, I don't like having to set two environment variables (or options) to make it work. It doesn't feel much more ergonomic than writing a wrapper script. I'd prefer to add an option that looks like fzf --with-shell "$SHELL -c" as commented in the previous pull request though it has it's own issue.

LangLangBart commented 2 months ago

Sure, using --with-shell is much clearer and simpler to write than using two environment variables.


It's important to mention in the documentation that, if this feature is implemented, one should not set the flags to -ic for the typical SHELL (zsh, …), as it usually ends badly. Below are some instances where doing so has caused issues: