]> git.madduck.net Git - etc/tmux.git/blob - .bin/mtmux

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

helper to initialise session with multiple windows
[etc/tmux.git] / .bin / mtmux
1 #!/bin/sh
2 #
3 # mtmux — initialise tmux session with multiple windows
4 #
5 # Usage:
6 #   1. mtmux [-n name] [-c cwd] -- cmd1 . windowname: cmd2 . shellname:
7 #
8 #      Spawns a tmux session, optionally named "name", and optionally with the
9 #      working directory set to "cwd", with three windows, one running cmd1,
10 #      one running cmd2 and named "windowname", and one named "shellname"
11 #      running the default command.
12 #
13 #   2. mtmux -n name -w 2
14 #
15 #      Attach to session "name" and switch to window 2
16 #
17 # Copyright © 2020 martin f. krafft <madduck@madduck.net>
18 # Released under the WTFPL.
19 #
20 set -eu
21
22 warn() {
23   echo >&2 "$@"
24 }
25
26 err() {
27   local exitcode; exitcode="$1"; shift
28   warn "$@"
29   exit $exitcode
30 }
31
32 for i in "$@"; do
33   case "${state:-}:$i" in
34     (:-c) state=cwd;;
35     (cwd:*)
36       [ -z "${cwd:-}" ] && cwd="$i" || err 1 "Option -c already specified"
37       state=
38       ;;
39
40     (:-n) state=name;;
41     (name:*)
42       [ -z "${name:-}" ] && name="$i" || err 1 "Option -n already specified"
43       state=
44       ;;
45
46     (:-w) state=win;;
47     win:*)
48       [ -z "${window:-}" ] && window="$i" || err 1 "Option -w already specified"
49       state=
50       ;;
51
52     (:--) shift; set -- "$@"; break
53   esac
54   shift 2>/dev/null || break
55 done
56
57 args=
58 if [ -n "${window:-}" ]; then
59
60   if [ -n "${cwd:-}" ]; then
61     warn "Both -w and -c don't make sense, ignoring -c"
62     cwd=
63   fi
64   if [ -n "$@" ]; then
65     warn "Ignoring commands specified with -w"
66     set --
67   fi
68   if [ -z "$name" ]; then
69     err 1 "Missing option -n, required with -w"
70   fi
71
72   args="new -t $name \; select-window -t $window"
73
74 else
75
76   subcmd="new${name:+ -s $name}"
77   for i in "$@" +; do
78     case "$i" in
79       (*:) wname="${i%:}";;
80       (+|.|\;)
81         args="${args}${subcmd}${cwd:+ -c $cwd}${wname:+ -n $wname}${cmd:-} \\; "
82         subcmd="neww -d"
83         cmd=
84         ;;
85       (*) cmd="${cmd:-} $i";;
86     esac
87   done
88
89 fi
90
91 eval exec tmux $args