zellij-org / zellij

A terminal workspace with batteries included
https://zellij.dev
MIT License
20.62k stars 639 forks source link

Add ability to run commands in the current session via external scripts #3581

Open pauschuu opened 2 weeks ago

pauschuu commented 2 weeks ago

Disclaimer: I know this is a tmux comparison, but it's (in my opinion) not some special feature, rather a core functionality that Zellij should have.

In tmux, I frequently run actions or commands in the current session using external scripts. For example:


# in some script that could be run from anywhere on the machine

# creates a new pane in the last active tmux session (the session with the most recent client activity)
tmux split-window

or

# sends the keys "foobar" to the last active pane
tmux send-keys foobar

Zellij can do this, but as soon as I have more than one session it wants me to explicitly state the session name in my command. That's a significant limitation for scripting around zellij.

I assume Zellij already internally tracks session activity, so it should be relatively straightforward to add an option like --in-current-session (or something like that) to the zellij command.

Implementing this feature would greatly enhance Zellij's usability for users who rely on external scripts to manage their terminal sessions.

Zykino commented 2 weeks ago

Zellij set the env var ZELLIJ_SESSION_NAME. So you can use that ;)

pauschuu commented 2 weeks ago

Zellij set the env var ZELLIJ_SESSION_NAME. So you can use that ;)

How should this help when running an external script that does not run in any Zellij context? The point is to that the script doesn't know the current active session (the one with the latest client interaction) so it has to rely on Zellij giving the option to say zellij --current-session action <some_action> so that Zellij would then take care of applying the action to the current session / pane.

Zykino commented 2 weeks ago

How should this help when running an external script that does not run in any Zellij context?

How does --in-current-session know the "current session" if it is not in it? Should it use the last created? You say "the one with the latest client interaction", but is this even reliable? Some peoples use zellij inside their IDE (vscodium among other). Some peoples use sessions for mid to long running command a bit like daemon (even if it is not the correct tool for it). We can’t know everyone’s setups. If you connect by SSH, then come back on your DE, which is the "current"? If no session is active does it create one? If no session is active and there are resurrect, does it pick one to resurrect? Which one? If 2 peoples are connected as the same user and both use a different session, the "current" session change at each keystroke by one or the other? …?

Most importantly, does your answers apply only to you or to every workflow?

To me the best zellij can provide is a way to specify the session. You can list all with zellij ls. You can also name your sessions. So you know you have "work", "personal", …

If you describe a bit more what you want this script do outside of zellij to a specific session I would be happy to brainstorm with you. For starter I don’t understand the need for this script to be run from outside of zellij. Is it run as a cron job?

pauschuu commented 2 weeks ago

Use case:

In tmux I had some keyboard shortcuts (defined outside of tmux) that run some scripts and also call some command or print some characters into the current tmux session.

Regarding reliability:

Worked pretty stable in tmux, only case where it didn't work was: Bringing a terminal window up front (with mouse-click on titlebar i.e.). This terminal wasn't the "last used" because tmux itself didn't see any action yet, since the mouse-click on some terminal window isn't recognizable by tmux itself. But apart from that it works fine. Type one letter, or perform any action at all (even mouse actions) and this session will be the last recent one.

Zellij kitty protocol support:

I guess the kitty protocol support (hopefully coming soon with the next release of Zellij) also makes these use-cases quite possible, since more keyboard combinations will (hopefully, think Ctrl+Shift+Super+p) be supported.

Conclusion:

To enhance scriptability, it would be a valuable addition to Zellij if users could send actions without specifying a particular session, allowing Zellij to automatically target the most recently used session.

Note:

To clear up: Last recent used should only be determined by last recent user interaction. Not any command output or other stuff that happens in the background.