Skip to main content
czerasz.com: notes
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Tmux

Automating tmux session:

#!/usr/bin/env bash

set -eu
set -o pipefail

script_directory="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_directory="${script_directory}/.."

session='czerasz-test'

if [ "${RECREATE:-undefined}" != 'undefined' ]; then
  echo "[info] killing old (${session}) session"
  tmux kill-session -t "${session}" > /dev/null 2>&1 || true
fi

if ! tmux has-session -t "${session}" > /dev/null 2>&1; then
  echo "[info] session (${session}) does not exist"

  window=$(tmux new-session -s "${session}" \
    -c "${project_directory}" \
    -d -F '#{window_index}' -P)

  # first window
  tmux rename-window -t "${session}:${window}" 'aws'
  tmux send-keys -t "${session}:${window}" \
    'export AWS_DEFAULT_REGION=eu-central-1' C-m
  tmux send-keys -t "${session}:${window}" \
    'ls -al' C-m
  tmux split-window -t "${session}:${window}" -v 'echo hi'

  # second window
  ((window++))
  tmux new-window -t "${session}:${window}" -n 'logs'
  tmux send-keys -t "${session}:${window}" \
    'ls -al' C-m
  tmux split-window -t "${session}:${window}" -v
  tmux send-keys -t "${session}:${window}" \
    'echo second pane with logs' C-m
fi

tmux select-window -t "${session}:1"
tmux attach -t "${session}"