]> 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:

0b85a48536607e8cb895c223e70af9a7bff4883d
[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 cmds=
33 for i in "$@"; do
34   case "${state:-}:$i" in
35     (:-c) state=cwd;;
36     (cwd:*)
37       [ -z "${cwd:-}" ] && cwd="$i" || err 1 "Option -c already specified"
38       state=
39       ;;
40
41     (:-n) state=name;;
42     (name:*)
43       [ -z "${name:-}" ] && name="$i" || err 1 "Option -n already specified"
44       state=
45       ;;
46
47     (:-w) state=win;;
48     win:*)
49       [ -z "${window:-}" ] && window="$i" || err 1 "Option -w already specified"
50       state=
51       ;;
52
53     (:--) shift; set -- "$@"; break;;
54
55     (:\;) cmds="$cmds \;";;
56     (:*) cmds="${cmds:+$cmds }$i";;
57   esac
58   shift 2>/dev/null || break
59 done
60
61 args=
62 if [ -n "${window:-}" ]; then
63
64   if [ -n "${cwd:-}" ]; then
65     warn "Both -w and -c don't make sense, ignoring -c"
66     cwd=
67   fi
68   if [ -n "$*" ]; then
69     warn "Ignoring commands specified with -w"
70     set --
71   fi
72   if [ -z "$name" ]; then
73     err 1 "Missing option -n, required with -w"
74   fi
75
76   args="new -t $name \;${cmds:+ $cmds \;} select-window -t $window"
77
78 else
79
80   subcmd="new${name:+ -s $name}"
81   for i in "$@" +; do
82     case "$i" in
83       (*:) wname="${i%:}";;
84       (+|.|\;)
85         args="${args}${subcmd}${cwd:+ -c $cwd}${wname:+ -n $wname}${cmd:-} \; "
86         args="$args${cmds:+$cmds \; }"
87         subcmd="neww -d"
88         cmd= cmds=
89         ;;
90       (*) cmd="${cmd:-} $i";;
91     esac
92   done
93
94 fi
95
96 eval exec tmux $args