Skip to content

Offer opening iTerm2 as a tab instead of a new window (issue #712) - #713

Merged
hannesa2 merged 1 commit into
masterfrom
open-in-terminal-as-tab
Sep 22, 2026
Merged

hannesa2 merged 1 commit into
masterfrom
open-in-terminal-as-tab

Conversation

@goneng

@goneng goneng commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

Reuse a terminal window that is already open, which is usually what is
wanted when a repository is sent to iTerm2, and fix two things the open
itself got wrong along the way.

  • Add an "Open as tab" preference beside the terminal handler, off by
    default so the existing behaviour is unchanged
  • Write the command into a new tab of iTerm2's current window when the
    preference is on, falling back to a new window when iTerm2 has none
    open
  • Take the window iTerm2 opens for itself when GitX is what launched it,
    rather than asking for a second one on top
  • Gray the preference out for Terminal.app, which has no Apple Event
    for opening a tab
  • Call tput clear in place of clear, so opening a repository no
    longer makes iTerm2 ask about clearing scrollback history
  • Raise the checkbox to sit directly under the terminal popup, so it
    reads as belonging to "Open in Terminal:" rather than to the Gist
    options below it

Screenshot

image

Launching iTerm2 leaves two sessions

Worth calling out because it is not obvious from the diff. Sending
createWindowWithDefaultProfileCommand: to an iTerm2 that is not running
launches it; iTerm2 opens a window of its own as it starts, and the event
then adds a second one on top. Both modes ended up with two sessions,
which is why the tab preference appeared to make no difference in that
case.

GitX now waits for the window iTerm2 opens for itself and uses that. If
none appears within five seconds it logs and asks for one, so a profile
configured to open no window on launch still works. The timeout is a
correctness knob rather than a comfort setting: firing it early would
reproduce the bug.

Measured on an Intel Mac with iTerm2 closed beforehand, driving the same
Scripting Bridge calls:

starting state path result
not running ask for a window (before) windows=2 sessions=2
not running take the launched window (after) windows=1 sessions=1, found after 1.63s
running, 1 window new tab windows=1, sessions 1 -> 2
running, 1 window new window windows 1 -> 2, sessions 2 -> 3

Test plan:

  • PBTerminalHandlerPreferenceTests passes, with seven new cases: which
    handlers are offered the tab option, the window/tab/launched-window
    decision for every combination of running, window open and preference,
    and the shell line GitX writes into the session
  • The scrollback case was written first and confirmed failing against
    clear before the fix went in
  • Driven against a real iTerm2: with the box ticked, Repository > Open
    in Terminal left the window count at 1 and took the tab count from 4
    to 5; unticked, it opened a second window
  • clear emits ESC[3J ESC[H ESC[2J on macOS and tput clear emits
    only ESC[H ESC[2J, measured with xxd; no announcement appeared in
    iTerm2 after the change
  • ibtool reports the same notices for the changed xib as for the one
    it replaces

Closes #712

@goneng
goneng force-pushed the open-in-terminal-as-tab branch from 2d7c234 to 5b318bb Compare September 21, 2026 23:20
@goneng goneng changed the title Offer opening iTerm2 as a tab instead of a new window (issue #712) [Terminal] Offer opening iTerm2 as a tab instead of a new window (issue #712) Sep 21, 2026
@goneng
goneng force-pushed the open-in-terminal-as-tab branch 2 times, most recently from fa18760 to f8a6b75 Compare September 22, 2026 05:33
@goneng goneng changed the title [Terminal] Offer opening iTerm2 as a tab instead of a new window (issue #712) Offer opening iTerm2 as a tab instead of a new window (issue #712) Sep 22, 2026
@goneng
goneng force-pushed the open-in-terminal-as-tab branch 2 times, most recently from 885c6b1 to 7ce0673 Compare September 22, 2026 10:50
Reuse a terminal window that is already open, which is usually what is
wanted when a repository is sent to iTerm2.

- Add an "Open as tab" preference beside the terminal handler, off by
  default so the existing behaviour is unchanged
- Write the command into a new tab of iTerm2's current window when the
  preference is on, falling back to a new window when iTerm2 has none
  open
- Take the window iTerm2 opens for itself when GitX is what launched it,
  rather than asking for a second one on top; sending the create-window
  event to a handler that is not running launched it, let it open its
  own window, and then added another, leaving two sessions in both
  modes
- Gray the preference out for Terminal.app, which has no Apple Event
  for opening a tab
- Clear the opened session with `tput clear` rather than `clear`, which
  emits ESC[3J and makes iTerm2 break into the new tab to ask whether
  discarding the scrollback is allowed
- Raise the checkbox to sit directly under the terminal popup, so it
  reads as belonging to "Open in Terminal:" rather than to the Gist
  options below it

@hannesa2 hannesa2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine for me. Let’s wait for the review by Copilot, in case it finds anything important.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Adds an optional iTerm2 “Open as tab” preference, reuses launched windows, and avoids scrollback prompts.

Changes:

  • Adds and persists the tab preference, disabled for Terminal.app.
  • Implements iTerm2 tab/window selection and launch-window reuse.
  • Replaces clear with tput clear and adds regression tests.
File Description
Resources/​en.lproj/​Preferences.xib Adds and positions the tab preference.
GitXTests/​PBTerminalHandlerPreferenceTests.m Tests preference and session behavior.
Classes/​Util/​PBTerminalUtil.m Implements tab/window selection and launch handling.
Classes/​Util/​PBTerminalUtil.h Declares terminal planning helpers.
Classes/​git/​PBGitDefaults.m Stores the new preference.
Classes/​git/​PBGitDefaults.h Exposes preference accessors.
Classes/​Controllers/​PBPrefsWindowController.m Wires preference state and enabling.
Classes/​Controllers/​PBPrefsWindowController.h Adds checkbox declarations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@hannesa2
hannesa2 merged commit e8d7649 into master Sep 22, 2026
4 checks passed
@hannesa2
hannesa2 deleted the open-in-terminal-as-tab branch September 22, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance: Open in Terminal: no option to open a new tab instead of a new window

3 participants