Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dingman/13a1d449f48ab8016f31a6b98bfa5a7b to your computer and use it in GitHub Desktop.

Select an option

Save dingman/13a1d449f48ab8016f31a6b98bfa5a7b to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# dangerclaude
#
# USAGE:
# dangerclaude
# -> creates a brand new session "superclaude-<timestamp>-<pid>" and attaches
#
# dangerclaude label
# -> creates (or reattaches to) session "superclaude-label"
#
# GUARANTEES:
# - Runs relative to the directory you're currently in.
# (We capture your cwd and cd into it before launching Claude.)
# - Works whether you pass a label or not.
# - Multiple sessions can run in parallel.
# - Each session runs Claude in a clean login+interactive shell (PuTTY-style env).
# - Mouse wheel scrolling works in PhpStorm (tmux mouse mode enabled).
# - Scrollback is huge so you can scroll way back in conversation.
#
# REQUIREMENTS:
# sudo apt-get install -y tmux
#
# NOTE:
# You trigger this manually. PhpStorm does NOT auto-run Claude unless you call this.
set -e
set -o pipefail
set -o nounset
##############################################################################
# Capture the directory where the user ran `dangerclaude`
# This guarantees Claude starts in *that* directory, not wherever tmux was.
##############################################################################
CALL_DIR="$(pwd)"
##############################################################################
# Resolve session name
##############################################################################
RAW_LABEL="${1:-}"
make_unique_label() {
# timestamp + pid -> extremely low collision chance
date +"%Y%m%d-%H%M%S"-$$
}
normalize_label() {
# Replace spaces and weird path-ish chars with dashes
echo "$1" | tr '[:space:]/:\\' '-----'
}
if [ -z "$RAW_LABEL" ]; then
BASE_LABEL=$(make_unique_label)
NORM_LABEL=$(normalize_label "$BASE_LABEL")
SESSION_NAME="superclaude-$NORM_LABEL"
else
NORM_LABEL=$(normalize_label "$RAW_LABEL")
SESSION_NAME="superclaude-$NORM_LABEL"
fi
##############################################################################
# Create session if it doesn't exist
##############################################################################
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
SESSION_EXISTS=0
else
SESSION_EXISTS=1
fi
if [ $SESSION_EXISTS -ne 0 ]; then
# Create the session detached
# Inside the session, we:
# - export NO_BANNER=1 to skip "press RETURN" pauses
# - cd into CALL_DIR so cwd matches where you launched dangerclaude
# - exec bash -ilc "claude ..." to:
# -i interactive -> loads aliases/functions from .bashrc/.bash_aliases
# -l login -> loads /etc/profile, ~/.bash_profile / ~/.profile,
# sets PATH like PuTTY
# -c command -> runs Claude immediately in that prepared shell
#
tmux new-session -d -s "$SESSION_NAME" \
"/bin/bash -ilc 'export NO_BANNER=1; cd \"${CALL_DIR}\"; claude --dangerously-skip-permissions'"
# Now configure quality-of-life for THIS session:
# 1. Big scrollback so you can wheel up through history
tmux set-option -t "$SESSION_NAME" history-limit 100000 > /dev/null 2>&1
# 2. Mouse on, so the mouse wheel in PhpStorm scrolls inside tmux
# In modern tmux, 'mouse on' lets you scroll the pane history with the wheel
tmux set-option -t "$SESSION_NAME" mouse on > /dev/null 2>&1
fi
##############################################################################
# Attach to that session (existing or newly created)
##############################################################################
exec tmux attach-session -t "$SESSION_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment