junegunn / fzf

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

Catch STDERR of execute() commands #3656

Closed TLINDEN closed 6 months ago

TLINDEN commented 6 months ago

Info

Problem / Steps to reproduce

Let's say I have this fzf call:

printf "foo\nbar\nhowdy"|fzf --bind "enter:execute:ssh {}"

Now if the executed command fails, it prints some error message to STDERR and exists, which I can't see, because once the program exits, the fzf selection re-appears, "overwriting" the error.

My current workaround looks like this:

#!/bin/sh
exec 3>&2

$* 2>&1 >&3 | tee stderr.txt

if test -s stderr.txt; then
    less stderr.txt
fi

rm -f stderr.txt

And then:

printf "foo\nbar\nhowdy"|fzf --bind "enter:execute:exec.sh ssh {}"

Now if the command (as ssh in this case) fails, the script pipes into less, so I can read and act on it. If no error occured, it just removes the copy and exits itself.

Is there a solution for this kind of problem or would it be possible to add some feature to catch errors and display them (like, in a [configurable?] popup or something like this)?

junegunn commented 6 months ago

You want fzf to reappear or not? If you don't want it to reappear, use become action instead.

If you do want it to reappear, but just want to see the error, you could add || read to wait for the enter key.

printf "foo\nbar\nhowdy"|fzf --bind "enter:execute:ssh {} || read"

Does this answer your question?

TLINDEN commented 6 months ago

Yeah, that's the solution, indeed. Many many thanks!