Skip to content

Conversation

@lucasoeth
Copy link

Summary

  • Add --tmux flag to wt switch that runs worktree creation in a new tmux window/session
  • Add --detach flag to run the tmux session in background instead of attaching
  • All hooks (post-create, post-start, post-switch) run inside tmux
  • Shell stays open at worktree path after completion

Usage

# Create worktree in new tmux session (attached)
wt switch --create feature --tmux

# Create worktree in detached tmux session (background)
wt switch --create feature --tmux --detach

Behavior

Context Default (--tmux) With --detach
Already in tmux New window, switches to it New window, switches to it
Not in tmux New session, attaches New session, detached

Test plan

  • --tmux conflicts with --execute (clap enforced)
  • --detach requires --tmux (clap enforced)
  • Help page snapshot updated
  • Manual testing with tmux

🤖 Generated with Claude Code

Add --tmux flag to `wt switch` that runs the entire worktree creation
(including all hooks) in a new tmux window/session instead of the
current terminal.

Behavior:
- If already in tmux: creates new window and switches to it
- If not in tmux: creates new session and attaches (default)
- With --detach: creates detached session in background

The shell stays open at the worktree path after completion, so you can
start working immediately.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@max-sixty
Copy link
Owner

I'm open to something like this. Can we discuss the options?

Currently I use zellij, and my workflow is "new-tab", and then wsl expands to wt switch --create --execute=claude, and I add -- 'Please fix foo' to the end.

What's yours? What's the advantage of having "new-tab" be within worktrunk?

lucasoeth and others added 2 commits January 8, 2026 21:55
Instead of failing or creating duplicates, --tmux now:
- Attaches to existing session (when outside tmux)
- Switches to existing window (when inside tmux)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Pass --execute and args through to the inner wt command when using --tmux.
This enables launching editors/agents in the tmux session:

  wt switch --create feature --tmux --execute claude -- 'Fix bug'

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@lucasoeth
Copy link
Author

The use case is agent-initiated worktree creation — Claude Code spawning parallel worktrees for handoffs that continue in the background. Your workflow works because you do the "new-tab" step manually, but when Claude wants to spawn a handoff there's no human to press new-tab first. It needs one atomic command that creates a detached session, creates the worktree inside it, runs the handoff, and continues in background.

That said, open to a more general approach. Could do something like --detach-via "zellij run --" where the user provides the wrapper command. That would be multiplexer-agnostic but puts more on the user. The tmux integration is what Claude Code needs today, but happy to generalize if there's a cleaner design.

lucasoeth and others added 7 commits January 8, 2026 22:19
When a detached tmux session fails quickly (within 2 seconds), capture
the output and display it in the original shell. This helps diagnose
errors without having to attach to a dead session.

Uses tmux's remain-on-exit option to keep the pane around for capture,
then cleans up the session after extracting the error output.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Replace consecutive str::replace() calls with char-based iteration
- Reduce function arguments by introducing TmuxSwitchOptions struct
- Apply cargo fmt formatting

Co-Authored-By: Claude Opus 4.5 <[email protected]>
tmux.rs uses .output() for quick status checks (session exists,
window exists, capture pane) that don't need shell_exec logging.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Update help snapshots and skill reference docs to include the new
--tmux and --detach flags on wt switch.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Environment variables from the current shell are now exported in the
tmux session, so hooks and commands have access to API keys, custom
paths, and other env vars.

Excludes internal vars like PWD, TMUX, SHLVL, etc.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Instead of embedding all env exports in the command string (which can
exceed shell limits), write them to a temp file and source it. The file
is removed after sourcing.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@max-sixty
Copy link
Owner

this is actually a very cool pattern, I haven't tried it much myself, it could be great


though I'm not sure it should be something in Worktrunk... am I right that this could be:

tmux new-session -d -s fix-auth-bug "wt switch --create fix-auth-bug -x claude -- \
  'The login session expires after 5 minutes. Find the session timeout config and extend it to 24 hours.'"

...which is verbose, but tractable

could we start by having it as a Tip / Pattern at first? my preference would be to get more reps from folks running this sort of workflow before adding a feature to Worktrunk, unless it unlocks something significant we can't otherwise do — wdyt?

@lucasoeth
Copy link
Author

@max-sixty I like your suggestion, what about adding it as a skill that people can install?

@lucasoeth lucasoeth closed this Jan 9, 2026
@max-sixty
Copy link
Owner

yes great!

@claude can you draft a PR which adds this as a Tip & Pattern, and a section in our existing Claude Code Skill?

@claude
Copy link
Contributor

claude bot commented Jan 9, 2026

Claude finished @max-sixty's task in 2m 16s —— View jobclaude/pr-476-20260109-1449Create PR ➔


Task completed

Todo List:

  • Explore docs structure to find where tips/patterns belong
  • Explore Claude Code Skill structure
  • Add tmux pattern to Tips/Patterns documentation
  • Add tmux pattern to Claude Code Skill
  • Commit and push changes
  • Provide PR URL

Summary

I've added the tmux pattern for agent handoffs to the documentation as a Tip & Pattern. The documentation includes:

Pattern: Launch worktree creation in a detached tmux session for parallel agent workflows:

tmux new-session -d -s fix-auth-bug "wt switch --create fix-auth-bug -x claude -- \
  'The login session expires after 5 minutes. Find the session timeout config and extend it to 24 hours.'"

Files updated:

  1. docs/content/tips-patterns.md - Added new "Agent handoffs in tmux" section
  2. docs/content/claude-code.md - Added same section to Claude Code integration page
  3. .claude-plugin/skills/worktrunk/reference/tips-patterns.md - Updated skill reference
  4. .claude-plugin/skills/worktrunk/reference/claude-code.md - Updated skill reference

The pattern includes examples for both tmux and zellij users, and explains that all hooks run inside the multiplexer session, with the shell staying open at the worktree path after completion.


Branch: claude/pr-476-20260109-1449

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants