How to run a shell function with execute or become #4016
Replies: 6 comments 5 replies
-
This will depend on your shell. If you are using greet(){
echo "Hello $@"
}
export -f greet
ls | SHELL=$(which bash) fzf --bind '<some-key>:become(greet {})
If you are using The key lesson to take away is that Note fzf runs the command with |
Beta Was this translation helpful? Give feedback.
-
@LangLangBart Yes, I am using Is it possible to run the zsh function inside a bash subshell when fzf executes it ? I tried running the following from zshell but it din't work
|
Beta Was this translation helpful? Give feedback.
-
This seems to be the most convenient way if I put this inside a script like
But it still does not work if I run this script like Even with autoloading the function fzf somehow fails to correctly load the function, here is the snippet from
Now if I open a new shell and run
Even with explicit autoloading it isn't working.
Here is the functions directory and file named
|
Beta Was this translation helpful? Give feedback.
-
@LangLangBart I did some more testing with bash script method and noticed something really strange, the script works outside of tmux but not within tmux. Here is the example ❯ cat /tmp/test.sh
#!/usr/bin/env bash
greet() {
echo "Hello $@"
}
export -f greet
ls | SHELL=$(which bash) fzf --bind 'enter:become(greet {})'
❯ FZF_DEFAULT_OPTS=""
❯ bash /tmp/test.sh
Hello catpuccin.txt Now if I add the ❯ cat /tmp/test.sh
#!/usr/bin/env bash
greet() {
echo "Hello $@"
}
export -f greet
ls | SHELL=$(which bash) fzf --bind 'enter:become(greet {})'
❯ FZF_DEFAULT_OPTS="--tmux"
❯ bash /tmp/test.sh
/opt/homebrew/bin/bash: line 1: greet: command not found Not sure what's going on here. |
Beta Was this translation helpful? Give feedback.
-
@LangLangBart Sorry for stretching this, but I noticed another issue. tmux ls -F '#{session_name}' | fzf --bind 'enter:become(tmux attach -t {})'
open terminal failed: can't use /dev/tty It only happens when the command is run as fzf tmux ls -F '#{session_name}' | SHELL=`which bash` fzf --bind 'enter:become(tmux attach -t {})'
open terminal failed: can't use /dev/tty I assume this to be a known behavior but could not find any discussion/issues related to this. Also did you try running below from within a tmux session ? For me, it works outside of tmux but not from within tmux ? Not sure if I am missing something here but running above from within tmux throws the same
|
Beta Was this translation helpful? Give feedback.
-
This regular
But when I do |
Beta Was this translation helpful? Give feedback.
-
Lets say we have a shell function defined which can be do different things depending on the arguments passed to it. How can run this function by binding a key with
become
orexecute
action ? Here is the example codeUnfortunately the above does not seem to work key press fails with
command greet not found
. I have also tried exporting the function but with no success.One workaround is to simply run the command directly inside the
become
likebecome(echo "Hello " {}
which works but then if I have logic that I want to reuse across multiple bindings, I would have to repeat the same code.Another way is to put this function into its own script and then invoke that script, which works, but moving these functions into their own scripts can be painful and overkill if I am working on something more involved .
Am I missing something obvious here, I have searched through docs, example sections, advanced section but could not find anything.
Beta Was this translation helpful? Give feedback.
All reactions