Skip to content

feat: add multi-session terminal support#164

Open
snirt wants to merge 27 commits intocoder:mainfrom
snirt:main
Open

feat: add multi-session terminal support#164
snirt wants to merge 27 commits intocoder:mainfrom
snirt:main

Conversation

@snirt
Copy link
Copy Markdown

@snirt snirt commented Dec 17, 2025

Add ability to run multiple concurrent Claude Code terminal sessions with session management, smart ESC handling, and session-aware selection tracking.

New commands:

  • ClaudeCodeNew: Create a new terminal session
  • ClaudeCodeSessions: Show session picker (supports fzf-lua)
  • ClaudeCodeSwitch: Switch to session by number
  • ClaudeCodeCloseSession: Close session by number or active session

New features:

  • Smart ESC handling: double-tap ESC to exit terminal mode, single ESC sends to terminal (configurable via esc_timeout)
  • Session-aware selection tracking and message routing
  • OSC title handler for capturing terminal title changes
  • Configurable terminal keymaps (terminal.keymaps.exit_terminal)

New modules:

  • lua/claudecode/session.lua: Session lifecycle management
  • lua/claudecode/terminal/osc_handler.lua: Terminal title detection

Add ability to run multiple concurrent Claude Code terminal sessions with
session management, smart ESC handling, and session-aware selection tracking.

New commands:
- ClaudeCodeNew: Create a new terminal session
- ClaudeCodeSessions: Show session picker (supports fzf-lua)
- ClaudeCodeSwitch: Switch to session by number
- ClaudeCodeCloseSession: Close session by number or active session

New features:
- Smart ESC handling: double-tap ESC to exit terminal mode, single ESC
  sends to terminal (configurable via esc_timeout)
- Session-aware selection tracking and message routing
- OSC title handler for capturing terminal title changes
- Configurable terminal keymaps (terminal.keymaps.exit_terminal)

New modules:
- lua/claudecode/session.lua: Session lifecycle management
- lua/claudecode/terminal/osc_handler.lua: Terminal title detection
@snirt
Copy link
Copy Markdown
Author

snirt commented Dec 18, 2025

Found a bug in this PR - killing the claude-code using ctrl-c closes the instance, but does not remove the session record from the session list. I'll fix it soon.

Snir Turgeman and others added 25 commits December 18, 2025 17:53
Add jobresize() calls to notify the terminal job of window dimensions
when switching between sessions. This fixes the cursor appearing in
the wrong position and line shifting after session switch.

The terminal job needs to know its window dimensions to correctly
calculate cursor position and line wrapping. Without this, the
terminal renders based on stale window state from the previous session.
- Add clickable tabbar for session switching (floating and winbar modes)
- Support left-click to switch sessions, middle-click to close
- Add close button (✕) on each tab with same background as tab
- Add new session button (+) for creating new sessions
- Add scroll wheel support to cycle sessions in floating tabbar
- Add mouse selection tracking (LeftRelease/LeftDrag) for better selection capture
- Fix intentional close handling to suppress exit error on X click
- Add config validation for terminal.tabs options
… available

When a Claude terminal session exits (e.g., via Ctrl-C), the window now
stays open and switches to display another available session instead of
closing entirely. This provides a smoother multi-session experience.

Changes:
- Add session switching logic to TermClose handlers in both providers
- Disconnect old terminal instance from window before buffer switch
- Check session existence before destroying to prevent double-destruction
- Add close_session_keep_window() for explicit session switching
Add dedicated window_manager module that owns the single terminal window.
This separates window lifecycle from buffer lifecycle, fixing issues where:
- Creating new sessions would reset window to default size
- Switching between tabs could cause window duplication
- Closing tabs could leave windows in wrong positions

Changes:
- Add window_manager.lua: singleton that manages THE terminal window
- Refactor snacks.lua: create buffers only, delegate window to manager
- Refactor native.lua: simplified buffer-only management
- Update terminal.lua: initialize window_manager, improve tab navigation
- New tab now selects the created session
- Closing tab selects previous tab (or next if first)
- Add "Fork Features" section highlighting multi-session support and
  visual tab bar features unique to this fork
- Add "Recommended Configuration" with practical floating window setup
- Update all repo references from coder/claudecode.nvim to snirt/claudecode.nvim
- Add multi-session keymaps to installation example
- Update CHANGELOG with new features and bug fixes
Fixes #1

- Add defense-in-depth PID recovery from sessions and terminal buffers
- Kill entire process tree (not just direct children) using process groups
- Follow up with SIGKILL for any survivors after graceful SIGTERM
- Add retry mechanism for PID tracking in snacks.lua (handles delayed job_id)
- Track PIDs in external terminal provider
- Add VimLeavePre autocmd to call cleanup_all() before server stops
- Validate cleanup_strategy config option

Tests:
- Unit tests for defense-in-depth PID recovery
- Integration tests with real processes verifying actual termination
Add WinEnter autocommand to selection tracking so keyboard navigation
(ctrl-h, ctrl-l, :wincmd) properly updates the file reference in Claude
Code. Previously only mouse clicks triggered updates.

The fix cancels pending debounce timers and uses a 10ms delay to ensure
window/buffer state is settled, matching the existing mouse handler
behavior.

Closes #2
When switching back to a tab containing the Claude Code terminal,
Neovim re-equalizes window widths causing the terminal to appear
narrow. Add restore_configured_width() helper that recalculates
and applies the configured split_width_percentage on TabEnter
and VimResized events. WinEnter is intentionally not modified
to preserve manual user resizing within a tab.
Downgrade ECONNRESET/EOF/EPIPE client read errors to debug-level
logs since they are expected when the terminal closes. Also make
session.destroy_session idempotent by logging at debug level
instead of warn when a session is already destroyed.
…der#165)

Add snacks_picker_list filetype to the exclusion list in:
- find_main_editor_window(): prevents picker from being selected as target
- _create_diff_view_from_window(): creates split when picker is focused

This fixes diff view behavior when using snacks.nvim picker.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* fix: ide diagnostics without URI schema

* mock method
Cherry-picked from upstream coder/claudecode.nvim with conflict resolution.
Preserves on_disconnect_cleanup for expected disconnection errors while
adopting centralized _disconnect_client pattern.
Cherry-picked from upstream coder/claudecode.nvim.
Adds a dedicated describe block with 8 tests covering the planned
3-state ESC machine behavior (1x/2x ESC + timeout sends raw bytes,
3x ESC exits terminal mode). Tests currently produce 2 failures
against the existing 2-state implementation.
Tests 3 and 4 now assert the key 3-state invariant: 2nd ESC must
not exit terminal mode. Failing tests: double-ESC timeout sends 2x
ESC, triple-ESC exits (not double), rapid triple-ESC exits, stale
callback is a no-op.
- Assert timer:stop() called before timer:start() on 2nd ESC (libuv safety)
- Add mode assertion ("t") to Path 2 esc_timeout=0 keymap test
- Add clarifying comment on stale_callback capture in stale-timer test
…e Code

Extends smart ESC handler from 2-state to 3-state machine.
Single ESC (timeout) and double ESC (timeout) now forward raw ESC
bytes to Claude Code, enabling cancel and rewind features.
Triple ESC exits Neovim terminal mode.

Closes #4
The previous implementation stopped the timer on 2nd ESC but never
restarted it, permanently breaking the double-ESC timeout path that
sends 2x raw ESC bytes to Claude Code for the rewind feature.

Also update the test mock to track _stop_calls so the restart assertion
remains accurate after stop+start leaves _stopped=false.
- Update state shape doc comment: count is 1|2 (never 0 stored in table)
- Update setup_terminal_keymaps doc to not assume double-ESC fallback
- Remove dead nil-timer guard in count=1 branch (new_timer() nil would
  have already crashed in the count=0 branch that created the state)
@Beloin
Copy link
Copy Markdown

Beloin commented Mar 21, 2026

Hi @snirt . Will you keep pushing this forward? This will close two open issues:

Sidebar toggles (file explorer, etc.) were triggering SIGWINCH via
jobresize on every WinEnter, causing the TUI to redraw and reset its
scroll position. Now jobresize is only sent immediately when the user
enters the terminal window itself; otherwise the resize is marked
pending and flushed on TermEnter.
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.

5 participants