Skip to content

Instantly share code, notes, and snippets.

@ericduran
Last active May 1, 2026 16:03
Show Gist options
  • Select an option

  • Save ericduran/bc7fefeb4a1377cc9fcd82225c9e4b7a to your computer and use it in GitHub Desktop.

Select an option

Save ericduran/bc7fefeb4a1377cc9fcd82225c9e4b7a to your computer and use it in GitHub Desktop.
function tmux-ssh-resume
# Only run in interactive shells (skip for SCP, rsync, etc.)
if not status is-interactive
return
end
# Check if we're in an SSH session
if test -n "$SSH_CLIENT" -o -n "$SSH_TTY"
# Check if we're not already in a tmux session
if test -z "$TMUX"
set sessions (tmux list-sessions 2>/dev/null)
if test -n "$sessions"
echo ""
echo "═══════════════════════════════════════════════════════"
echo " Available tmux sessions:"
echo "═══════════════════════════════════════════════════════"
tmux list-sessions -F " #{session_name} (#{session_windows} windows, #{session_attached} attached)"
echo "═══════════════════════════════════════════════════════"
echo ""
echo "Options:"
echo " [1-N] - Join session"
echo " [n] - Create new session"
echo " [q] - Skip (don't join any session)"
echo ""
while true
read -P "Choose an option: " choice
set choice (string trim $choice)
if string match -qr '^[0-9]+$' $choice
set session_count (tmux list-sessions 2>/dev/null | wc -l | string trim)
if test $choice -ge 1 -a $choice -le $session_count
set session_name (tmux list-sessions -F "#{session_name}" 2>/dev/null | sed -n "$choice p")
echo "Attaching to session: $session_name"
exec tmux attach-session -t "$session_name"
return
else
echo "Invalid session number. Please choose between 1 and $session_count"
end
else if test "$choice" = "n" -o "$choice" = "new"
set session_name "ssh-"(date +%Y%m%d-%H%M%S)
read -P "Session name (default: $session_name): " custom_name
if test -n "$custom_name"
set session_name (string trim $custom_name)
end
echo "Creating new tmux session: $session_name"
exec tmux new-session -s "$session_name"
return
else if test "$choice" = "q" -o "$choice" = "quit" -o "$choice" = "skip"
echo "Skipping tmux session attachment."
return
else
echo "Invalid option. Please enter a number, 'n' for new, or 'q' to skip."
end
end
else
echo ""
echo "No tmux sessions found."
read -P "Create a new session? [Y/n]: " create_new
if test -z "$create_new" -o "$create_new" = "y" -o "$create_new" = "Y"
set session_name "ssh-"(date +%Y%m%d-%H%M%S)
read -P "Session name (default: $session_name): " custom_name
if test -n "$custom_name"
set session_name (string trim $custom_name)
end
echo "Creating new tmux session: $session_name"
exec tmux new-session -s "$session_name"
else
echo "Skipping tmux session creation."
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment