junegunn / fzf

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

Unable to execute bash/zsh functions in --bind #2762

Closed callumacrae closed 2 years ago

callumacrae commented 2 years ago

Info

Problem / Steps to reproduce

I feel like this must have come up before but was unable to find an issue!

I'm trying to bind a key to execute a function set in the shell—set locally for demo purposes but the same happens in .zshrc files.

It fails in zsh:

image

It fails in bash too:

image

I've tried to look into the source to figure it out myself but am unfamiliar with how Go works so struggled to make any progress: https://github.com/junegunn/fzf/blob/cd2340141100e057005d31e653c0ce7a0f7a4af7/src/util/util_unix.go#L12

junegunn commented 2 years ago
foo() {
  echo foo
}

# You should export the function
export -f foo

fzf --preview 'foo'
callumacrae commented 2 years ago

Thanks!

For anyone else who finds this issue, the problem was that functions aren't available to child processes.

Unfortunately in ZSH, it looks like you can't export functions - the recommended alternative is to use autoloaded functions instead, or you could make a proper script and add it to your $PATH.

I just put all my scripts in big strings 🤷‍♂️