Binu
Binu

Reputation: 157

How can I create multiple tmux sessions containing windows from command line

I am trying to write a template script for a development session using tmux. So i just need to run this script for opening a new dev environment. Each session will have multiple windows - say two. First window(Window1) can be created while creating the detached session as:

tmux new-session -s $TMUX_SESSION_NAME -d -n Window1 (Here TMUX_SESSION_NAME is the argument passed to the script to name the session).

However, how can I create another window under the same session?

Note that I can create it as below but that messes up when creating another session. Although tmux ls shows each session having 2 windows each, the second session contains all the env settings of the first session(Both are for completely different projects)

tmux new-window -n Window2 tmux attach -t $TMUX_SESSION_NAME

I suspect both/all sessions go under the same /tmp/tmux-SOME_ID/default socket and hence this problem.

Note that the first time i start a dev session all is good with both windows.

Any ideas?

Upvotes: 1

Views: 4192

Answers (1)

jeremysprofile
jeremysprofile

Reputation: 11425

TL;DR: probably with something like

tmux new-window -t $TMUX_SESSION_NAME
tmux rename-window -t $TMUX_SESSION_NAME:1 'second'

More info (my configuration):

Here's what I use to start my tmux sessions. The argument to the function would be the name of the session you want to create.

If this does not answer your question, please comment and edit your question to that it is more clear to me.

tmuxstart() {
    tmux new-session -d -s $1 >/dev/null
    tmux rename-window -t $1:0 'main'
    tmux splitw -v -p 10 -t $1:0.0
    tmux splitw -h -p 80 -t $1:0.1
    #required; otherwise pane numbering is bs
    tmux select-pane -t $1:0.0
    tmux splitw -h -p 5 -t $1:0.0
    tmux send-keys -t $1:0.2 'sudo htop' Enter
    tmux send-keys -t $1:0.1 'tmux clock -t $1:0.1' Enter
    tmux select-pane -t $1:0.0
    tmux new-window -t $1
    tmux rename-window -t $1:1 'second'
    tmux splitw -v -p 10 -t $1:1.0
    tmux splitw -h -p 80 -t $1:1.1
    tmux select-pane -t $1:1.0
    tmux splitw -h -p 5 -t $1:1.0
    tmux clock -t $1:1.1
    tmux new-window -t $1
    tmux rename-window -t $1:2 'scratch'
    tmux splitw -v -p 10 -t $1:2.0
    tmux select-pane -t $1:2.0
    tmux splitw -h -p 5 -t $1:2.0
    tmux clock -t $1:2.1
    tmux select-window -t $1:0.0
    tmux a -t $1
}

Upvotes: 4

Related Questions