Skip to content

Instantly share code, notes, and snippets.

@snOm3ad
Created May 17, 2020 18:25
Show Gist options
  • Save snOm3ad/0485804adc73821d70dd04b13aed95e1 to your computer and use it in GitHub Desktop.
Save snOm3ad/0485804adc73821d70dd04b13aed95e1 to your computer and use it in GitHub Desktop.
automatic session creator for tmux.
#!/bin/bash
create_session() {
# robustly split the space separated list of window names.
read -r -a WINDOWS <<< "$NAMES"
# override parameter list with names of windows.
set -- "${WINDOWS[@]}"
# create session with first window argument.
tmux new -d -s "$SESSION" -n "$1" || return 1
shift
# `seq 0` actually produces the output `1 0` thus the script would
# break if the user only wants 1 window, this check prevents that.
[ $# == 0 ] && return 0
# create the remaining windows
for i in $(seq $#)
do
# use indirect expansion to get the name of the window.
tmux new-window -d -n "${!i}" -t "$SESSION:$i" || return 1
done
return 0
}
bail() {
echo "[ERROR]: could not create session '$SESSION'."
exit 1
}
while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
PROG_NAME=$(basename "$0")
echo "$PROG_NAME"
echo "automatic session creator for tmux."
echo " "
echo "USAGE:"
echo " $PROG_NAME SESSION_NAME <NAMES> [OPTIONS]"
echo " "
echo "OPTIONS:"
echo " -h, --help show brief help"
echo " -a, --attach automatically attach to the session"
echo "NAMES:"
echo " -n, --names <LIST> names of windows, separated by space; ignored if the"
echo " session already exists and [-a|--attach] is present"
exit 0
;;
-a|--attach)
ATTACH=1
shift
;;
-n*|--names*)
shift
NAMES="$1"
shift
;;
*)
# NOTE: any misspelled arguments fall here as well, hence we are only
# interested in grabbing the first positional argument, ignore all else.
[ -z "$SESSION" ] && SESSION="$1"
shift
;;
esac
done
# if the SESSION name and window names are not empty then try to create a new session.
if [ -n "$SESSION" -a -n "$NAMES" ]; then
# check if the session already exists
# NOTE: this also checks if there is a server already running.
tmux has-session -t "$SESSION" 2>/dev/null
if [ $? == 0 ]; then
# if the session already exists but no `attach` flag was given then alert user.
[ -z "$ATTACH" ] && echo "[WARN]: '$SESSION' already exists, use [-a|--attach] to attach to it."
else
# otherwise create it, bail if something goes wrong.
create_session || bail
fi
# lastly, attach to the session if the `attach` flag was given.
[ -n "$ATTACH" ] && tmux a -t "$SESSION"
else
[ -z "$SESSION" ] && echo "[ERROR]: missing session name, use 'tmux-dev --help' for usage."
[ -z "$NAMES" ] && echo "[ERROR]: missing window names, use 'tmux-dev --help' for usage."
exit 1
fi
@snOm3ad
Copy link
Author

snOm3ad commented May 17, 2020

I got tired of having to manually create tmux sessions with multiple windows (which I use a lot), this became particularly annoying after I realized that sessions disappear once you restart your machine, so I made this script to make the process much faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment