Recently started using tmux, much like screen that I used ages ago.
Is there a flag/option for tmux that would join an existing session if it exists, but if not, create a new one? Or is that something that would have to be handled with shell scripting and/or aliasing?
Here is a small shell script called tmux-start that takes session name as an argument, and attaches to it if it exists, and creates a new session if it does not exits. You can then run this script from your .bashrc - again with a condition only to run outside tmux:
[[ -n "$TMUX" ]] && tmux-start
#!/bin/bash
# Tmux Startup Script
# Usage: tmux-start [session_name]
# If no session name is provided, default session name is used
# If script is run inside tmux, exit without doing anything
if [[ -n "$TMUX" ]]; then
echo "Nested tmux sessions not supported!"
exit 1
fi
# Name session to $1, or default if $1 is empty
if [[ -z "$1" ]]; then
session="default"
else
session="$1"
fi
# Check if session is already running
tmux has-session -t "$session" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
# If already running, attach to it
tmux attach -t "$session"
else
tmux new-session -s "$session"
fi