How to run a shell function with execute or become #4016
Replies: 7 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.txtNow 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 foundNot 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/ttyIt 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/ttyI 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.
-
|
One straightforward way to retain locality of behavior relies on a conditional switch within one file, either to search or to run the bound function, as demonstrated in this The result will be simple to understand and flexible enough to handle complex scenarios as needed. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
becomeorexecuteaction ? 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
becomelikebecome(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