#!/bin/sh # # mtmux — initialise tmux session with multiple windows # # Usage: # 1. mtmux [-n name] [-c cwd] -- cmd1 . windowname: cmd2 . shellname: # # Spawns a tmux session, optionally named "name", and optionally with the # working directory set to "cwd", with three windows, one running cmd1, # one running cmd2 and named "windowname", and one named "shellname" # running the default command. # # 2. mtmux -n name -w 2 # # Attach to session "name" and switch to window 2 # # Copyright © 2020 martin f. krafft # Released under the WTFPL. # set -eu warn() { echo >&2 "$@" } err() { local exitcode; exitcode="$1"; shift warn "$@" exit $exitcode } for i in "$@"; do case "${state:-}:$i" in (:-c) state=cwd;; (cwd:*) [ -z "${cwd:-}" ] && cwd="$i" || err 1 "Option -c already specified" state= ;; (:-n) state=name;; (name:*) [ -z "${name:-}" ] && name="$i" || err 1 "Option -n already specified" state= ;; (:-w) state=win;; win:*) [ -z "${window:-}" ] && window="$i" || err 1 "Option -w already specified" state= ;; (:--) shift; set -- "$@"; break esac shift 2>/dev/null || break done args= if [ -n "${window:-}" ]; then if [ -n "${cwd:-}" ]; then warn "Both -w and -c don't make sense, ignoring -c" cwd= fi if [ -n "$@" ]; then warn "Ignoring commands specified with -w" set -- fi if [ -z "$name" ]; then err 1 "Missing option -n, required with -w" fi args="new -t $name \; select-window -t $window" else subcmd="new${name:+ -s $name}" for i in "$@" +; do case "$i" in (*:) wname="${i%:}";; (+|.|\;) args="${args}${subcmd}${cwd:+ -c $cwd}${wname:+ -n $wname}${cmd:-} \\; " subcmd="neww -d" cmd= ;; (*) cmd="${cmd:-} $i";; esac done fi eval exec tmux $args