Skip to content

feat(tmux): focus tmux agent session on toggle#317

Open
MartinLoeper wants to merge 1 commit into
folke:mainfrom
MartinLoeper:fix/toggle-tmux-session
Open

feat(tmux): focus tmux agent session on toggle#317
MartinLoeper wants to merge 1 commit into
folke:mainfrom
MartinLoeper:fix/toggle-tmux-session

Conversation

@MartinLoeper
Copy link
Copy Markdown

@MartinLoeper MartinLoeper commented May 6, 2026

Summary

  • Track tmux window ids for discovered Sidekick CLI sessions.
  • Let require("sidekick.cli").toggle() focus external tmux sessions when there is no embedded Neovim terminal.
  • Select both the tmux window and pane so split-based sessions land on the agent pane.

Validation

  • stylua lua/sidekick/cli/init.lua lua/sidekick/cli/session/tmux.lua
  • git diff --check
  • ./scripts/test

Edit

Probably very related to: #298

Co-authored-by: Codex <noreply@openai.com>
@github-actions github-actions Bot added the size/m Medium PR (<50 lines changed) label May 6, 2026
@MartinLoeper MartinLoeper changed the title Focus tmux agent session on toggle feat: focus tmux agent session on toggle May 6, 2026
@MartinLoeper
Copy link
Copy Markdown
Author

Demo

Replay_2026-05-07_00-26-10.mp4

@MartinLoeper
Copy link
Copy Markdown
Author

I find the workflow above really convenient as I think it makes sense to reuse the muscle memory around C-. to switch between the lazyvim and agent windows. The tmux side looks like the following (needed to toggle back from agent to lazyvim).

$HOME/scripts/tmux-editor-toggle:

#!/usr/bin/env bash
# Toggle to/from the editor (lvim/nvim) window in the current tmux session.
# - If already in an editor window: forward C-. to the pane so nvim/sidekick handles it.
# - Otherwise: switch to the first lvim/nvim window in the session.
# - If no editor window exists: open lvim in a new window using the current pane's cwd.

CURRENT_NAME=$(tmux display-message -p '#{window_name}')

if [[ "$CURRENT_NAME" =~ ^(lvim|nvim) ]]; then
  # Send the literal CSI-u sequence for C-. (period=46, mods=ctrl=5) to the pane.
  # send-keys writes to the pty directly, bypassing tmux's own key bindings.
  tmux send-keys -l $'\e[46;5u'
  exit 0
fi

EDITOR_WID=$(tmux list-windows -F '#{window_name}|#{window_id}' \
  | awk -F'|' '$1 ~ /^(lvim|nvim)/ { print $2; exit }')

if [ -n "$EDITOR_WID" ]; then
  tmux select-window -t "$EDITOR_WID"
  exit 0
fi

# No editor window — open lvim in current pane's cwd
PANE_PATH=$(tmux display-message -p '#{pane_current_path}')
tmux new-window -n lvim -c "$PANE_PATH" lvim

mapped via .tmux.conf:

bind-key -n C-. run-shell '$HOME/scripts/tmux-editor-toggle'

@MartinLoeper MartinLoeper changed the title feat: focus tmux agent session on toggle feat(tmux): focus tmux agent session on toggle May 6, 2026
@MartinLoeper MartinLoeper marked this pull request as ready for review May 13, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m Medium PR (<50 lines changed)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant