From 6433f7a86cdbf4db6acdd1cbbdbd7d84bc2d361a Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Wed, 17 Dec 2025 17:04:35 +0100 Subject: [PATCH] feat(tmux): add create and attach named sessions to `tmx()` (#78) Reviewed-on: https://codeberg.org/vlw/dotfiles/pulls/78 --- .bash_aliases | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/.bash_aliases b/.bash_aliases index e684fb4..95cde5f 100644 --- a/.bash_aliases +++ b/.bash_aliases @@ -222,14 +222,30 @@ sshl () { alias qw="tmux split-window -h" alias qa="tmux split-window -v" +# Create or attach tmux sessions tmx() { - # 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 :) + 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 + fi + + # Create a new session if none exists + tmux new-session -s default return 0 fi - tmux new-session -s default + # Create a new named tmux session + if ! tmux has-session -t "$session_name" 2>/dev/null; then + tmux new-session -s "$session_name" -d + fi + + # Attach to named tmux session + tmux attach -t "$session_name" }