Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to start a session with layout but detached immediately? #2090

Open
asartalo opened this issue Jan 13, 2023 · 6 comments

Comments

@asartalo
Copy link

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.

@thisismygitrepo
Copy link

thisismygitrepo commented Jan 17, 2023

This is a direly missing feature. Please prioritize its implementationas there is no other ways of achieving it.

@bnwa
Copy link

bnwa commented Feb 22, 2023

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.

@jayeheffernan
Copy link

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.

@isti115
Copy link
Contributor

isti115 commented Apr 19, 2024

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.

@imsnif
Copy link
Member

imsnif commented Apr 19, 2024

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.

@imsnif
Copy link
Member

imsnif commented Apr 22, 2024

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 #3288
I want to get a few more small fixes in and will then release a patch. Likely a matter of days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants