2

I'd like to do the next from a bash script:

  • Create a session named "my_session".
  • Create a window named "my_window" inside of that session.
  • Create two pans named "my_pan1" and "my_pan2" inside of that window.
  • Send a command inside of "my_pan1".
  • Send a command inside of "my_pan2".

How would you approach this?

3
  • 1
    Hi Adrian ! Did you finally try the below answer and was it satisfying ?
    – jeremy
    Oct 5, 2020 at 8:57
  • Done. Also provided an aditional example. Thank you! Oct 5, 2020 at 18:36
  • Thanks for trying this out ! Even though the pan names disappear while the shell is filled and first command needs the session name to already exist to work in your version, it is still cool to look at this alternative way of doing this ! Thanks again
    – jeremy
    Oct 6, 2020 at 9:46

2 Answers 2

3

This tmux.sh script worked on my tmux 3.0a and after adding this: set -g pane-border-format "#{@mytitle}" in my .tmux.conf (see why in comments bellow the scipt). You might have to add this: set -g pane-border-status bottom in your .tmux.conf as well. With following command you name your session "ses", your window "win", pane 0 "p1" and pane 1 "p2":

tmux.sh ses win p1 p2

#!/bin/bash
    
session=$1
window=$2
pan1=$3
pan2=$4
    
#Get width and lenght size of terminal, this is needed if one wants to resize a detached session/window/pane
#with resize-pane command here
set -- $(stty size) #$1=rows, $2=columns

#start a new session in dettached mode with resizable panes
tmux new-session -s $session -n $window -d -x "$2" -y "$(($1 - 1))"
tmux send-keys -t $session 'echo "first command in 1st pane"' C-m
    
#rename pane 0 with value of $pan1
tmux set -p @mytitle "$pan1"

#split window vertically
tmux split-window -h
tmux send-keys -t $session 'echo "first command in 2nd pane"' C-m
tmux set -p @mytitle "$pan2"

#At the end, attach to the customized session
tmux attach -t $session

I had a lot of trouble to rename the panes because tmux select-pane -t $window.0 -T $pan1 should work but as stated here: https://stackoverflow.com/questions/60106672/prevent-tmuxs-pane-title-from-being-updated some updates of pane titles can be done by applications inside tmux. So I used the tricks given in the answer in the previous link (Nicholas Marriott gave also a solution for tmux version older than 3.0a)

2

A shorter way to do it. This example spawns 4 panes in grid.

#!/bin/bash
sh="/usr/bin/env sh" # You can also choose zsh.
SESSION="misc"       # Session name.
WINDOW="experiments" # Window name.

tmux kill-session -t "$SESSION" 2>&1
tmux start \; new-session  -d -s "$SESSION" -n "$WINDOW" "$sh -c \"echo 'first shell'\"; $sh -i" \;
tmux split-window "$sh -c \"echo 'second shell'\"; $sh -i" \;
tmux split-window "$sh -c \"echo 'third shell'\"; $sh -i" \;
tmux split-window "$sh -c \"echo 'fourth shell'\"; $sh -i" \;
tmux select-layout tiled \;
tmux attach -t "$SESSION" \;
tmux switch -t "$SESSION"

You must log in to answer this question.

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