Taufiq Septryana
devops kodi

Fish Shell + Tmux: Auto-Attach on Every Terminal Open

A simple fish config snippet that automatically attaches to a tmux session (or creates one) every time you open a terminal — except when you’re already in tmux or inside VS Code’s integrated terminal.

The Config

if status is-interactive
    if not set -q TMUX
        and test "$TERM_PROGRAM" != "vscode"
        tmux new-session -A -s main
    end
end

How It Works

ConditionPurpose
status is-interactiveOnly runs for interactive shells (not scripts)
not set -q TMUXSkip if already inside a tmux session (prevents nested tmux)
test "$TERM_PROGRAM" != "vscode"Skip in VS Code terminal — VS Code has its own session management and tmux can interfere with integrated features
tmux new-session -A -s mainCreate session “main” if it doesn’t exist, or attach to it if it does (-A = attach if exists)

Why This Pattern Matters

Without the guards:

  • Nested tmux — opening a terminal inside tmux creates a session inside a session, breaking key bindings and confusing the prefix key
  • VS Code conflicts — the integrated terminal expects direct shell access; tmux can swallow scrollback, copy mode, and某些 extensions that depend on shell integration

Variations

Named sessions per project:

set session_name (basename (pwd))
tmux new-session -A -s $session_name

Fallback to plain shell if tmux isn’t installed:

if command -v tmux >/dev/null 2>&1
    tmux new-session -A -s main
end

Source