exupero's blog
RSSApps

Tmux window launcher

Besides launching Tmux sessions, I also have scripts to launch windows within a session. A simple one looks like this:

dir=$(pwd)
session=$(tmux display-message -p '#S')

tindow "$session:daemons" << EOF
$dir/frontend ./watch.sh
$dir/backend ./serve.sh
EOF

tmux move-window -t 9
tmux select-layout even-horizontal

Most of the work is done by the tindow command, which takes a target window and reads lines from stdin, splitting each line into a directory and a shell command, creating a Tmux pane, cding into the directory, then running the shell command. Here's its code:

target=$1

IFS=':' read -a sessionAndWindow <<< "$target"
session=${sessionAndWindow[0]}
window=${sessionAndWindow[1]}

pane=0
while read -a line; do
  dir=${line[0]}
  cmd=$(echo ${line[@]:1})
  if [[ $pane -eq 0 ]]; then
    tmux new-window -t "$session" -n $window
  else
    tmux split-window -t "$target" -l 100
  fi
  tmux send -t "$target.$pane" "cd $dir" C-m "$cmd" C-m
  pane=$(expr $pane + 1)
done

A script that invokes tindow can decide whether to include certain windows:

dir=$(pwd)
session=$(tmux display-message -p '#S')

(
  echo $dir/frontend ./watch.sh
  echo $dir/backend ./server.sh
  if [[ -n $1 ]]; then
    echo $dir/extensions ./watch-extension.sh $1
  fi
) | tindow "$session:watchers"