diff --git a/.bash_aliases b/.bash_aliases index d416a0f..d43b3da 100644 --- a/.bash_aliases +++ b/.bash_aliases @@ -272,29 +272,36 @@ alias qw="tmux split-window -h" alias qa="tmux split-window -v" # Create or attach tmux sessions -tmx() { - local session_name="$1" - - # Attach to the first available tmux session - if [[ -z "$session_name" ]]; then - # Check if any tmux sessions exist - if tmux has-session 2>/dev/null; then - # Attach to the first existing session - tmux attach-session -d -t $(tmux ls | head -n 1 | awk '{print $1}' | tr -d :) - - return 0 +tmx () { + # A session name is provided + if [[ -n "$1" ]]; then + # Attatch to existing named session + if tmux has-session -t "$1" 2>/dev/null; then + tmux attach -t "$1" + return fi - # Create a new session if none exists + # Create a new named session + tmux new-session -s "$1" -d + return + fi + + local sessions=$(tmux list-sessions -F "#S" 2>/dev/null) + local session_count=$(echo "$sessions" | wc -l) + + # Create a new session if none exists + if [ -z "$sessions" ]; then tmux new-session -s default - return 0 + return fi - # Create a new named tmux session - if ! tmux has-session -t "$session_name" 2>/dev/null; then - tmux new-session -s "$session_name" -d + # Attach to the first existing session + if [ "$session_count" -eq 1 ]; then + tmux attach-session -d -t $(tmux ls | head -n 1 | awk '{print $1}' | tr -d :) + return fi - # Attach to named tmux session - tmux attach -t "$session_name" + echo "$sessions" + echo "" + echo "Multiple sessions exist" }