0

I'm writing a zsh script that setup nested session. I have a session and a session inside that session.

sessionA
   sessionB

I can create sessionA in detach mode -d and then call tmux inside that to create nested sessionB but how to switch to sessionB?

I checked manpage and there's no switch-session option. Much appreciated.

3 Answers 3

1

You would first start a session:

tmux new-session -s [session-name] -n [first-tmux-screen-name] -d

We can now separate this window in sections by using splitw options:

tmux splitw -h
tmux splitw -v

we can select a section too:

tmux selectp -t0
tmux splitw -v

And depending on what we want to display, or run in each nested window, we use the send-key option. If we want to add a new window to this tmux session we can use this command:

tmux new-window -n [name-of-new-window] -t [session-name]

zsh works for me:

#!/bin/zsh

tmux new-session -s test -n four-screen -d
tmux splitw -h
tmux splitw -v
tmux selectp -t0
tmux splitw -v

After your run the script: zsh scriptname.zsh then attach to the session: tmux attach

7
  • Why two -d in first session creation?
    – Tuyen Pham
    Sep 6, 2018 at 4:03
  • was suppose to be a -n sorry.
    – NiteRain
    Sep 6, 2018 at 4:04
  • Forgot to mention, after you do the last new-window, that is what you will see when you tmux attach -t [session], so to go to the first screen, you would have to do a tmux select-window -t [first-tmux-screen-name] in your script.
    – NiteRain
    Sep 6, 2018 at 4:08
  • I'm doing in zsh script so your answer probably won't help. Right now I have trouble to rename nested session. Output keep saying can't find session <NAME>
    – Tuyen Pham
    Sep 6, 2018 at 4:14
  • This is question about how to switch between nested session, your zsh script is about creating a new named session and window then spliting some panes - probably unrelated to the original question but thanks for the help.
    – Tuyen Pham
    Sep 6, 2018 at 4:48
0

An easy solution is to detach and then attached directly to sessionB.

tmux attach-session -t sessionB

1
  • In a script I'd need to switch to sessionB silently, so that the question.
    – Tuyen Pham
    Sep 6, 2018 at 2:08
0

Seem like my question is a little bit not clear.

Here I want to do tasks on nested session but after googling I can achieve that purpose via send-keys with option that specify session name to act on. So no need to switch to session that I'd need to work on - I'm in an zsh script perspective.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .