Skip to content

Instantly share code, notes, and snippets.

@simoninglis
Last active February 19, 2026 08:00
Show Gist options
  • Select an option

  • Save simoninglis/febc834941b809847448474e0fc58ea6 to your computer and use it in GitHub Desktop.

Select an option

Save simoninglis/febc834941b809847448474e0fc58ea6 to your computer and use it in GitHub Desktop.
.tmux-session starter + tmux-join helper
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SESSION="${TMUX_SESSION_NAME:-$(basename "$SCRIPT_DIR")}"
if ! tmux has-session -t "=$SESSION" 2>/dev/null; then
tmux new-session -d -s "$SESSION" -c "$SCRIPT_DIR" -n editor
tmux new-window -t "$SESSION" -n shell -c "$SCRIPT_DIR"
tmux send-keys -t "$SESSION:editor" 'nvim .' C-m
fi
tmux attach-session -t "=$SESSION"
#!/usr/bin/env bash
# tmux-join: Smart tmux session attach/switch - prevents nested tmux
# Usage: tmux-join SESSION_TARGET
#
# When called from within tmux: switches to target session
# When called from outside tmux: attaches to target session
set -euo pipefail
if [[ $# -ne 1 ]]; then
echo "Usage: tmux-join SESSION_TARGET" >&2
echo "Example: tmux-join '=dotfiles:0'" >&2
exit 1
fi
SESSION_TARGET="$1"
# Wait for session to be created (race condition protection)
MAX_RETRIES=10
RETRY_DELAY=0.1 # 100ms
for ((i=1; i<=MAX_RETRIES; i++)); do
if tmux has-session -t "$SESSION_TARGET" 2>/dev/null; then
break
fi
if [[ $i -eq MAX_RETRIES ]]; then
echo "Error: Session '$SESSION_TARGET' not found after ${MAX_RETRIES} retries" >&2
exit 1
fi
sleep "$RETRY_DELAY"
done
# Try switch-client first (works when inside tmux)
# Falls back to attach if not in tmux or switch fails
tmux switch-client -t "$SESSION_TARGET" 2>/dev/null || tmux attach -t "$SESSION_TARGET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment