Skip to content

Instantly share code, notes, and snippets.

@yottahmd
Created January 13, 2026 13:11
Show Gist options
  • Select an option

  • Save yottahmd/8e6d0a4213be6a559dfe3dcdd350ce09 to your computer and use it in GitHub Desktop.

Select an option

Save yottahmd/8e6d0a4213be6a559dfe3dcdd350ce09 to your computer and use it in GitHub Desktop.
Claude Code Session Fork for Ghostty

Claude Code Session Fork for Ghostty (Note: This is AI-generated)

Automatically fork your current Claude Code session into a new Ghostty split pane with a single command.

What This Does

Two Raycast commands:

  • Fork Claude Right - Split horizontally (right)
  • Fork Claude Down - Split vertically (down)

Each command will:

  1. Split the Ghostty terminal in the chosen direction
  2. Automatically fork the exact Claude session running in the current pane
  3. Both panes now have independent sessions that can diverge

Prerequisites

Install Dependencies

brew install jq

Setup

1. Create Claude Hook Script

Create ~/.claude/hooks/capture-session.sh:

#!/bin/bash
session_data=$(cat)
session_id=$(echo "$session_data" | jq -r '.session_id')
cwd=$(echo "$session_data" | jq -r '.cwd')
cwd_hash=$(echo -n "$cwd" | md5)
echo "$session_id" > "/tmp/claude_session_${cwd_hash}"
exit 0

Make it executable:

chmod +x ~/.claude/hooks/capture-session.sh

2. Configure Claude Settings

Add to ~/.claude/settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/capture-session.sh"
          }
        ]
      }
    ]
  }
}

3. Create Raycast Scripts

Create two scripts in ~/.config/raycast/scripts/:

fork-claude-right.sh (horizontal split)

#!/bin/bash

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Fork Claude Right
# @raycast.mode silent

# Optional parameters:
# @raycast.icon 🍴
# @raycast.packageName Claude Code
# @raycast.needsConfirmation false

# Documentation:
# @raycast.description Split Ghostty and fork current Claude session
# @raycast.author hamadayouta

# Prevent multiple executions
LOCK_FILE="/tmp/fork-claude-session.lock"
if [ -f "$LOCK_FILE" ]; then
  exit 0
fi
touch "$LOCK_FILE"
trap "rm -f $LOCK_FILE" EXIT

# Get session ID if Claude is running
session_id=""
if pgrep -x claude > /dev/null; then
  cwd=$(lsof -p $(pgrep -n claude) 2>/dev/null | grep cwd | awk '{print $NF}')
  if [ -n "$cwd" ]; then
    cwd_hash=$(echo -n "$cwd" | md5)
    session_file="/tmp/claude_session_${cwd_hash}"
    if [ -f "$session_file" ]; then
      session_id=$(cat "$session_file")
    fi
  fi
fi

# Build the fork command
if [ -n "$session_id" ]; then
  fork_cmd="claude --resume ${session_id} --fork-session"
else
  fork_cmd="claude --continue --fork-session"
fi

# Run everything in one AppleScript
osascript <<EOF
tell application "Ghostty" to activate
delay 0.3

tell application "System Events"
  tell process "Ghostty"
    click menu item "Split Right" of menu "File" of menu bar 1
  end tell

  delay 0.5

  -- Type the fork command
  keystroke "${fork_cmd}"
  delay 0.1
  key code 36 -- Return key
end tell
EOF

echo "Done"

fork-claude-down.sh (vertical split)

Same as above, but change:

  • # @raycast.title Fork Claude Down
  • # @raycast.description Split Ghostty down and fork current Claude session
  • click menu item "Split Down" of menu "File" of menu bar 1

Make both executable:

chmod +x ~/.config/raycast/scripts/fork-claude-right.sh
chmod +x ~/.config/raycast/scripts/fork-claude-down.sh

4. Add Scripts to Raycast

  1. Open Raycast (Cmd+Space)
  2. Press Cmd+, to open Preferences
  3. Go to Extensions tab
  4. Click + button at bottom left
  5. Select "Add Script Directory"
  6. Navigate to ~/.config/raycast/scripts and add it
  7. Both commands will now appear: "Fork Claude Right" and "Fork Claude Down"

5. Set Hotkeys (Recommended)

  1. In Raycast Preferences β†’ Extensions
  2. Find each command and set hotkeys:
    • Fork Claude Right: Cmd+Shift+D
    • Fork Claude Down: Cmd+Shift+Opt+D

Usage

  1. Start Claude Code in Ghostty: claude
  2. Work on your session
  3. Fork your session:
    • Cmd+Shift+D β†’ Split right
    • Cmd+Shift+Opt+D β†’ Split down
  4. Both panes now have independent session branches!

How It Works

  1. When Claude starts, a SessionStart hook captures the session ID
  2. Session ID is stored in /tmp/claude_session_<cwd_hash>
  3. When you run a Raycast command:
    • Ghostty is focused
    • A split pane is created via File β†’ Split Right/Down
    • The stored session ID is read
    • Runs claude --resume <session_id> --fork-session

Troubleshooting

Fork not working?

  • Check Raycast has Accessibility permissions (System Preferences β†’ Privacy & Security β†’ Accessibility)
  • Verify Claude is running in the terminal
  • Check /tmp/claude_session_* files exist

Wrong session forked?

  • Ensure you're forking from the pane where Claude is running
  • The hook captures session per working directory

Split not happening?

  • Make sure Ghostty is the active window
  • Check Raycast has permission to control your Mac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment