zellij
zellij copied to clipboard
Question: Is it possible to start a session with layout but detached immediately?
I have a layout that I would like to have run some servers or persistent tasks in the background but I don't want to attach to to the session immediately but just detached and then at some point later I can attach to it if needed. Kind of like running tmux in detached mode like tmux new -s foo -d.
This is a direly missing feature. Please prioritize its implementationas there is no other ways of achieving it.
Came to Issues looking for exactly this. In my tmux workflow, I have a fish function I call that sets up a session per repo, executing the desired layout for each, all without ever attaching to a session in particular. Rather surprised to find that missing here. I do enjoy the otherwise vast improvement in sanity from tmux however.
I'd like this too. My use-case is similar to @bnwa 's.
My use-case
With Tmux I use smug to have predefined layouts in YAML config files - one layout per-project. When I boot my computer I have a small init.sh Bash script to start up all my usual projects in separate Tmux sessions:
tmux start-server
smug start logs --detach
smug start proxy --detach
smug start website --detach
smug start default-project --detach
Workaround
For now I have a workaround of scripting my terminal app (wezterm) to process the right key-presses to start and then detach each session in series. My new init.sh:
#! /usr/bin/env bash
function start-session() {
local session_name="$1"
# Send Zellij cli to shell
wezterm cli send-text "zellij --layout $session_name --session $session_name"
# send Enter key
wezterm cli send-text --no-paste '^M'
# Wait a moment so Zellij has time to start
sleep 2
# send "detach" keybinding to Zellij
wezterm cli send-text --no-paste 'Ctrl-t d'
}
function attach-session() {
local session_name="$1"
wezterm cli send-text "zellij attach $session_name"
wezterm cli send-text --no-paste '^M'
}
start-session logs
start-session proxy
start-session website
start-session default-project
attach-session default-project
Then run it with ./init.sh & (note the & to run in background).
NB: the ^M should not be a carat character followed by an M - it's a literal newline character (typed with Ctrl-v Enter in Vim). Similarl story with Ctrl-t d, which is my configured key-binding for detaching from Zellij. I've edited them here to look readable, but the real script uses special characters to work properly.
Since #3257 this behavior (or at least something similar) can be achieved by the following commands:
zellij attach --create-background example-session
ZELLIJ_SESSION_NAME=example-session zellij action new-tab --layout example-layout.kdl
This still leaves an extra empty tab open, which could be closed by something like:
ZELLIJ_SESSION_NAME=example-session zellij action go-to-previous-tab
ZELLIJ_SESSION_NAME=example-session zellij action close-tab
Ideally the first command would also allow you to specify the desired layout. I'll try to take a look into this to see if I can submit a PR to add that functionality.
P.S.: Another possible workaround would be temporarily overriding the default layout before launching the background session.
Ideally the first command would also allow you to specify the desired layout. I'll try to take a look into this to see if I can submit a PR to add that functionality.
I came here to say that you could do zellij attach --create-background my-session-name options --default-layout <layout> but then found that this isn't working. This is a bug - I'll try to add it to the fixes for the next patch version.
I came here to say that you could do
zellij attach --create-background my-session-name options --default-layout <layout>but then found that this isn't working. This is a bug - I'll try to add it to the fixes for the next patch version.
Fixed in https://github.com/zellij-org/zellij/pull/3288 I want to get a few more small fixes in and will then release a patch. Likely a matter of days.