]> git.madduck.net Git - etc/tmux.git/commitdiff

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
authormartin f. krafft <madduck@madduck.net>
Fri, 27 Mar 2020 08:17:45 +0000 (21:17 +1300)
committermartin f. krafft <madduck@madduck.net>
Fri, 27 Mar 2020 08:17:45 +0000 (21:17 +1300)
.bin/mtmux [new file with mode: 0755]
.gitignore.d/tmux

diff --git a/.bin/mtmux b/.bin/mtmux
new file mode 100755 (executable)
index 0000000..bee0bc8
--- /dev/null
@@ -0,0 +1,91 @@
+#!/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 <madduck@madduck.net>
+# 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
index 60810fdd7cce0ae2e9d705805078b03f907ff589..c398bca54a26feb1985ed6403d8a670509edff49 100644 (file)
@@ -1,4 +1,5 @@
 *
-!/.gitignore.d
+!/.bin/mtmux
+!/.bin/tmux
 !/.gitignore.d/tmux
 !/.tmux.conf