Skip to content

Instantly share code, notes, and snippets.

@stevenringo
Created January 9, 2026 04:39
Show Gist options
  • Select an option

  • Save stevenringo/5e1f4dc68c857d0c66549b4363d9141c to your computer and use it in GitHub Desktop.

Select an option

Save stevenringo/5e1f4dc68c857d0c66549b4363d9141c to your computer and use it in GitHub Desktop.
Persistent terminal sessions with Zellij - auto-attach setup for local and SSH access

Persistent Terminal Sessions with Zellij

The Problem

I access my Mac both physically (sitting at the machine with Ghostty) and remotely via SSH/mosh. What I wanted was simple:

  1. Start a session locally, pick it up later via SSH
  2. Start a session remotely, continue it when I'm at the machine
  3. No manual "attach" commands - just open terminal and be there
  4. Minimal UI overhead - I don't need tabs or panes right now

Essentially: one terminal session, accessible from anywhere, invisibly.

The Solution

Auto-attach on shell start

Added this to ~/.zshrc, near the top:

# =============================================================================
# ZELLIJ AUTO-ATTACH
# =============================================================================
# Automatically attach to (or create) a persistent zellij session
# This allows the same session to be accessed locally or via SSH
if [[ -z "$ZELLIJ" ]] && [[ -n "$PS1" ]]; then
    exec zellij attach --create main
fi

How it works:

  • -z "$ZELLIJ" - only runs if not already inside zellij (prevents nesting)
  • -n "$PS1" - only runs for interactive shells (won't break scp, rsync, git over SSH, etc.)
  • exec - replaces the shell process, so exiting zellij logs you out cleanly
  • attach --create main - attaches to session "main" if it exists, creates it otherwise

No SSH configuration needed. SSH just starts a login shell on the remote machine, which sources .zshrc, which runs the auto-attach.

Hiding the UI

Zellij's default layout shows a status bar. For a minimal setup, enabled the compact layout in ~/.config/zellij/config.kdl:

default_layout "compact"

This removes the tab bar and shows only a minimal status line.

Dotfiles Management

The zellij config is managed via GNU stow from ~/.config/dotfiles:

~/.config/dotfiles/zellij/.config/zellij/config.kdl

Adopted the existing config with:

cd ~/.config/dotfiles && just adopt zellij

This moved the existing config into the dotfiles repo and created a symlink back.

Result

  • Open Ghostty locally: automatically in the "main" zellij session
  • SSH in from anywhere: same session, same state
  • Session persists across disconnects
  • No commands to remember, no UI clutter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment