zellij: Allow zellij run CLI command to block until command has completed

The zellij run command should have the ability to block until the command being run has exited.

This would allow zellij to be invoked in scripts or shortcuts to perform actions and then have control returned to the caller.

For example, my use case is using Helix keybinds to invoke the Yazi file browser to select a file to which will then be opened in Helix. However currently I am unable to block until I’ve selected a file as the zellij command immediately exits.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (1 by maintainers)

Most upvoted comments

I have this working with Yazi and Helix specifically:

# config.toml
[keys.normal."'"]
y = ":sh zellij run --floating -n 'yazi picker' -- /home/rboynton/.config/fish/scripts/open_in_helix_from_yazi.fish"
# open_in_helix_from_yazi.fish
set -l tmpfile /tmp/yazi-choice.txt
rm -f $tmpfile
yazi --chooser-file=$tmpfile
zellij action toggle-floating-panes
zellij action write 27 # send escape-key
zellij action write-chars ":open $(cat /tmp/yazi-choice.txt)"
zellij action write 13 # send enter-key
zellij action toggle-floating-panes
zellij action close-pane

@blixenkrone you can open multiple files at once if you change it to

files=(${(fqq)"$( yazi --chooser-file=/dev/stdout | cat )"})
zellij action toggle-floating-panes
zellij action write 27 # send escape-key
zellij action write-chars ":open $files"
zellij action write 13 # send enter-key
zellij action toggle-floating-panes
zellij action close-pane

This handles their quoting too.

Thanks @rockboynton and @postsolar!

Here’s (single file) using zsh:

# open_in_helix_from_yazi.zsh
file=$(yazi --chooser-file=/dev/stdout | cat)
zellij action toggle-floating-panes
zellij action write 27 # send escape-key
zellij action write-chars ":open $(command printf '%q' "$file")"
zellij action write 13 # send enter-key
zellij action toggle-floating-panes
zellij action close-pane

With this version i get the error “wrong argument count”.

You’re probably executing the script with bash, not zsh, perhaps due to not adding the shebang? Try adding #! /usr/bin/env zsh as the very first line. Or run the bash version from yazi docs

Thanks for suggesting this feature.

A few things:

  1. This is possible with plugins using the new pipe interface. The flow would be “pipe to a plugin from the CLI which will be launched if not running, the plugin could either do whatever it wants on its own or launch a command passed to it, then unblock the CLI once the command is done”.
  2. Zellij also has in a built-in filepicker that does exactly this. Maybe it’s not as nice as the dedicated Yazi, but it can at least serve as an example of how this can be achieved.

While I totally empathize with people wanting this to be part of core, the philosophy of this project is to offload as much as possible to plugins in order to help us maintainers keep up. If someone makes a generic plugin that blocks until a command is done, I’d be happy to link to it and refer to it when this comes up.

ADDENDUM: if someone is interested in implementing this plugin and wants support, they are welcome to join our Discord - there are lots of plugin authors there who’d be happy to help.

For anyone interested in an example of opening yazi in a side-pane (not a floating pane) from a helix keybind & persisting it in view, I’m posting here just because this took me a bit of digging to figure out but it turned out quite nice actually.

(referencing the note in yazi-docs):

Note: this uses a floating window, but you should also be able to open a new pane to the side, or in place. Review the Zellij documentation for more info.


config.toml

[keys.normal]
C-y = ":sh zellij run -d right -- bash ~/.config/helix/yazi-picker.sh"

yazi-picker.sh

#!/usr/bin/env bash

zellij action move-pane left

while true; do
    paths=$(yazi --chooser-file=/dev/stdout | while read -r; do printf "%q " "$REPLY"; done)

    if [[ -n "$paths" ]]; then
        zellij action focus-next-pane
        zellij action write 27 # send <Escape> key
        zellij action write-chars ":open $paths"
        zellij action write 13 # send <Enter> key
        zellij action focus-previous-pane
    fi
done

@JasterV just add it as an argument? Here is a one-liner for the latest release of Helix:

For any terminal with or without Zellij (using foot as an example here):

:open %sh{ foot -- sh -c "yazi --chooser-file=/proc/$$/fd/1 -- %{buffer_name}" 2>/dev/null }

For zellij:

:open %sh{ zellij run -fc -- sh -c "yazi --chooser-file=/proc/$$/fd/1 -- %{buffer_name}; kill -s CONT $$"; kill -s STOP $$ }

This assumes editor.shell in Helix configuration is set a POSIX-compatible shell (true by default on Linux, on MacOS you don’t have access to /proc/).

The bash script assumes that the escape key puts the mode back to normal. In my case, the escape key is mapped to capslock and within Helix, my escape key toggles between NORMAL and INSERT mode. So consider making it explicit by mapping the keys in the Helix config like this:

# ~/.config/helix/config.toml
[keys.normal]
C-y = [
  "normal_mode",
  ":sh zellij run -f -- bash ~/picker.sh"
]

and do not write the escape key in the sh script.

Cool, thank you for sharing it! @rockboynton

Would you like to submit a PR and add it to the Tips page of the Yazi document? This way more people can see it.

Edit: Added to https://yazi-rs.github.io/docs/tips/#helix-with-zellij, fell free to use it 😉

https://github.com/zellij-org/zellij/assets/17523360/4b47ec6b-0282-44ab-814d-d25c0e8d6cf5


BTW I saw this comment from https://www.reddit.com/r/HelixEditor/comments/1chvan1/how_are_people_using_their_cli_file_explorers, hope I’m not too late here 😉