fix(tmux): list multiple tmux sessions if exists

This commit is contained in:
Victor Westerlund 2026-01-25 08:45:57 +01:00
parent 8027b321ee
commit 0fd946d6d6
Signed by: vlw
GPG key ID: D0AD730E1057DFC6

View file

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