fix(tmux): list multiple tmux sessions if exists (#110)

Reviewed-on: https://codeberg.org/vlw/dotfiles/pulls/110
This commit is contained in:
Victor Westerlund 2026-01-25 08:47:14 +01:00
parent 8027b321ee
commit e3577a414c

View file

@ -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"
}