Skip to content

Instantly share code, notes, and snippets.

@okwme
Last active March 27, 2026 13:43
Show Gist options
  • Select an option

  • Save okwme/680823f7ff1f3ceae4440610605e8659 to your computer and use it in GitHub Desktop.

Select an option

Save okwme/680823f7ff1f3ceae4440610605e8659 to your computer and use it in GitHub Desktop.
Tmux setup: config + session picker menu + plugins (curl-able)
#!/bin/bash
#
# Tmux Setup Installer
# Installs: config, session picker menu, and plugins (TPM, resurrect, continuum)
#
# Run:
# curl -fsSL https://gist.githubusercontent.com/okwme/680823f7ff1f3ceae4440610605e8659/raw/install-tmux-setup.sh | bash
#
# What you get — every new terminal shows a session picker:
#
# ┌─────────────────────────────────────────────────┐
# │ │
# │ Available tmux sessions: │
# │ │
# │ 1) main │
# │ 2) project-abc │
# │ 3) server-logs │
# │ │
# │ n) Create new session │
# │ s) Skip tmux (start shell without tmux) │
# │ │
# │ Select option [1-3/n/s] (default: 1): _ │
# │ │
# └─────────────────────────────────────────────────┘
#
# If no sessions exist, it auto-creates one called "main".
# Skips automatically inside VS Code, Cursor, and other IDEs.
# Set NO_TMUX=1 to bypass.
#
set -e
echo "Installing tmux config..."
# 1. Write .tmux.conf
cat > ~/.tmux.conf << 'EOF'
# Enable mouse support
set -g mouse on
# Big scrollback buffer
set -g history-limit 100000
# Use vi keys in copy mode (easier on mobile)
setw -g mode-keys vi
# Allow scrolling with mouse wheel to enter copy mode
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"
# Plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# Initialize TPM
run '~/.tmux/plugins/tpm/tpm'
EOF
# 2. Write .tmux-menu.sh (session picker on new terminal)
cat > ~/.tmux-menu.sh << 'EOF'
#!/bin/bash
# Tmux session menu
# Displays a menu to attach to existing sessions or create a new one
tmux_menu() {
# Skip if NO_TMUX is set, tmux is not installed, or already inside tmux
[ -n "$NO_TMUX" ] && return
command -v tmux >/dev/null 2>&1 || return
[ -n "$TMUX" ] && return
# Skip if running inside VS Code, Cursor, or other IDEs
[ -n "$VSCODE_INJECTION" ] && return
[ "$TERM_PROGRAM" = "vscode" ] && return
[ -n "$VSCODE_GIT_IPC_HANDLE" ] && return
# Must be interactive shell
case $- in *i*) ;; *) return ;; esac
# Get list of sessions
local sessions_raw
sessions_raw=$(tmux list-sessions -F '#S' 2>/dev/null)
# If no sessions exist, create first one
if [ -z "$sessions_raw" ]; then
echo "No tmux sessions found. Starting new session 'main'..."
exec tmux new -s main
fi
# Display menu
echo
echo "Available tmux sessions:"
echo
# Read sessions into array
local -a sessions
local i=1
while IFS= read -r session; do
[ -n "$session" ] && {
sessions[$i]="$session"
echo " $i) $session"
i=$((i + 1))
}
done <<< "$sessions_raw"
echo
echo " n) Create new session"
echo " s) Skip tmux (start shell without tmux)"
echo
# Get user choice
while true; do
printf "Select option [1-%d/n/s] (default: 1): " "$((i-1))"
read -r choice
# Default to first session
[ -z "$choice" ] && choice=1
case "$choice" in
s|S)
# Skip tmux
return
;;
n|N)
# Create new session
printf "Enter new session name (default: 'main'): "
read -r newname
[ -z "$newname" ] && newname="main"
exec tmux new -s "$newname"
;;
[0-9]*)
# Attach to numbered session
if [ -n "${sessions[$choice]}" ]; then
exec tmux attach -t "${sessions[$choice]}"
else
echo "Invalid selection. Please choose 1-$((i-1)), n, or s."
fi
;;
*)
echo "Invalid selection. Please choose 1-$((i-1)), n, or s."
;;
esac
done
}
# Auto-run for interactive shells only
if [ -z "$TMUX" ]; then
case $- in
*i*)
tmux_menu
;;
esac
fi
EOF
# 3. Add source line to shell rc if not already present
SHELL_RC="$HOME/.$(basename "$SHELL")rc"
SOURCE_LINE='[ -f "$HOME/.tmux-menu.sh" ] && source "$HOME/.tmux-menu.sh"'
if ! grep -qF '.tmux-menu.sh' "$SHELL_RC" 2>/dev/null; then
echo "" >> "$SHELL_RC"
echo "# Tmux session menu on new terminal" >> "$SHELL_RC"
echo "$SOURCE_LINE" >> "$SHELL_RC"
echo "Added tmux-menu sourcing to $SHELL_RC"
else
echo "tmux-menu already sourced in $SHELL_RC"
fi
# 4. Install TPM if not present
if [ ! -d ~/.tmux/plugins/tpm ]; then
echo "Installing TPM..."
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
fi
# 5. Install plugins
echo "Installing tmux plugins..."
~/.tmux/plugins/tpm/bin/install_plugins
echo ""
echo "Done! Open a new terminal to see the session menu."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment