ghostty icon indicating copy to clipboard operation
ghostty copied to clipboard

macOS: Not possible to cmd+c/cmd+x from rename tab, search, other text fields

Open mitchellh opened this issue 9 months ago • 29 comments

Discussed in https://github.com/ghostty-org/ghostty/discussions/10623

Originally posted by jimmystridh February 8, 2026 When the "Change Tab Title" dialog is open (introduced in #4217), standard Copy/Cut keyboard shortcuts like Cmd+C, Cmd+X don't work in the text field. You can type and use the mouse to select text, and use the right-click menu to perform the operations, but the usual copy/paste/cut shortcuts are silently swallowed.

Steps to Reproduce

  1. Open a Ghostty tab
  2. Trigger "Change Tab Title" (View menu → Change Tab Title)
  3. Type some text in the text field
  4. Select text and press Cmd+C or CMD+X - nothing is copied or cut
  5. Pressing Cmd+V pastes whatever was in pasteboard before

mitchellh avatar Feb 16 '26 18:02 mitchellh

Note I don't like the proposed fix in #10623. I'd prefer an alternate, simpler approach if possible.

mitchellh avatar Feb 16 '26 18:02 mitchellh

Implemented in https://github.com/ghostty-org/ghostty/pull/11557 - adds a small NSTextField subclass that overrides performKeyEquivalent to forward Cmd+C/X/V/A to the field editor.

mvanhorn avatar Mar 16 '26 06:03 mvanhorn

As additional notes, the solution here I'm pretty sure is not to hardcode values like #11557 or some other PRs have done. If I'm wrong about that, I need to be shown some proper material to justify it.

Copy/paste and so on should emit standard operations on first responder chains. As long as the input editor is part of the proper responder chain or can become part of the proper responder chain, then we should be able to hook into that.

I could very much be wrong and this could be a non-starter approach, but I haven't seen anyone explain why that doesn't work yet. I've only seen some really brute force solutions that obviously would work but in the sense that if Google just hardcoded "if you search for news you get BBC" that would work for that one case.

I'm looking for the right solution, not just the mostly right one.

mitchellh avatar Mar 16 '26 23:03 mitchellh

Okay I decided to dedicate 20 minutes of my actual human brain to this and figured out the real root cause pretty easily:

We strip the keyboard shortcuts from the menu during startup because our reverse lookup on keyboard shortcuts ignores performable bindings because we want to ensure non-performed actions get to the terminal. We don't even have "cut" in our menu. So these can't be triggered.

I don't actually know what the right solution to this looks like. Looking at Terminal.app, it has "Cut" in the menu and it never works on a terminal (expected) and just becomes possible when a native text field is focused. This makes sense.

Now what "what" and "why" are known, but the "how" to fix is still unknown to me. We can't put performable bindings in the menu for reasons previously established. I wonder if we need to actually resync keyboard shortcuts when a terminal surface isn't focused and always include performable bindings there...

mitchellh avatar Mar 17 '26 00:03 mitchellh

Note to the above: I slopped up a fix that puts performable shortcuts into the menu and it does indeed fix this. That isn't the right solution though, as noted. But it verifies the root cause analysis was accurate.

mitchellh avatar Mar 17 '26 00:03 mitchellh

So we've now spent way too much time on this. Thought this would be a fun easy first one to get in before I took on something more ambitious.... I went back at it!! here's what I found...

thank you for the root cause analysis. That unlocked everything.

I built a working fix on top of your finding. The approach is exactly what you described - resync performable key equivalents when a native text field is focused, remove them when the terminal surface regains focus. Here's what it does:

Zig layer: A reverse_performable map (parallel to the existing reverse) that stores only performable triggers. Exposed via ghostty_config_trigger_performable so the Swift layer can query the actual configured key equivalents without hardcoding anything.

Swift layer: TerminalWindow.makeFirstResponder detects when the first responder becomes a native text input (NSTextView/NSTextField) and calls AppDelegate.syncPerformableMenuShortcuts(enabled:), which sets key equivalents on Copy/Paste/SelectAll from the config. When the terminal surface regains focus, it re-syncs from config (which strips them back out). A temporary Cut menu item is inserted during editing since the Edit menu doesn't normally include one.

Event routing: TerminalWindow.sendEvent bypasses the content view hierarchy during inline editing - tries the menu first (which now has the key equivalents), then falls through to keyDown on the first responder for non-menu bindings like Ctrl+A/E.

Tested locally - Cmd+C/V/X/A, Ctrl+A/E, Escape, Enter all work. Cmd+T/W still route to the menu normally during editing.

Two things I'd want your input on before submitting:

  1. The sendEvent bypass - I needed it because SurfaceView's performKeyEquivalent still intercepts during editing even though the field editor is the first responder. If focus management should handle this naturally (SurfaceView.focused = false when the field editor has focus), then the sendEvent override is papering over a separate issue. Would you rather I investigate why the normal dispatch doesn't work with just the menu resync, or is the sendEvent approach acceptable?

  2. The temporary Cut menu item - Ghostty doesn't have Cut in the Edit menu (makes sense for a terminal). I'm inserting/removing it programmatically during editing. Would you prefer Cut just live in the XIB permanently like Terminal.app does, where it's always visible but only activates when a native text field is focused?

Happy to submit as-is or adjust based on your feedback.

mvanhorn avatar Mar 17 '26 01:03 mvanhorn

Sorry Matt. This sounds like more AI slop and I'm tired of it because it keeps producing the wrong results. Putting you on a temporary ban.

mitchellh avatar Mar 17 '26 15:03 mitchellh

I think this also fix paste into the search field #11530

mitchellh avatar Mar 17 '26 15:03 mitchellh

@mitchellh here: https://github.com/suneeshtr/ghostty/tree/fix-performable-menu-shortcuts

suneeshtr avatar Mar 19 '26 01:03 suneeshtr

If I'm reading this right, then #11253 should fix this, except it doesn't because the cmd-v combo causes the paste to go into the terminal surface and not into the text field. Any ideas on why, and how we might address this case (where we need to pass the text field to the command as the current focus so we don't paste to the wrong place?) Maybe the issue is that the standard text field I employed for that bug doesn't properly tell the command apparatus who has the actual focus?

pauley-unsaturated avatar Mar 21 '26 06:03 pauley-unsaturated

Okay I decided to dedicate 20 minutes of my actual human brain to this and figured out the real root cause pretty easily:

We strip the keyboard shortcuts from the menu during startup because our reverse lookup on keyboard shortcuts ignores performable bindings because we want to ensure non-performed actions get to the terminal. We don't even have "cut" in our menu. So these can't be triggered.

I don't actually know what the right solution to this looks like. Looking at Terminal.app, it has "Cut" in the menu and it never works on a terminal (expected) and just becomes possible when a native text field is focused. This makes sense.

Now what "what" and "why" are known, but the "how" to fix is still unknown to me. We can't put performable bindings in the menu for reasons previously established. I wonder if we need to actually resync keyboard shortcuts when a terminal surface isn't focused and always include performable bindings there...

@mitchellh If you could take a look this #11718.

The fix avoids resyncing on focus changes. Two things instead:

Menu shortcuts: Added a second reverse map (reverse_menu) that includes performable bindings, used only for menu shortcut display via a new ghostty_config_trigger_for_menu API. The existing reverse map stays untouched for terminal key routing.

Key interception: performKeyEquivalent now checks window.firstResponder directly — if the surface isn't actually focused, it bails and lets the event reach whatever text field has focus. The focused flag can go stale because SwiftUI's @FocusState doesn't always trigger AppKit's resignFirstResponder.

This also lets goto_tab/last_tab be unconditionally performable, dropping the macOS workaround from #7786.

suneeshtr avatar Mar 21 '26 08:03 suneeshtr

Workaround by forcing adding keybind for paste_from_clipboard currently. And it won't block normal paste without search tab.

keybind = super+v=paste_from_clipboard

qianheng-aws avatar Mar 26 '26 05:03 qianheng-aws

Workaround by forcing adding keybind for paste_from_clipboard currently. And it won't block normal paste without search tab.

keybind = super+v=paste_from_clipboard

This WAR would cause more problems with starship. After adding this configuration, Ctrl+C noticeably pauses every time, and pressing Ctrl+C repeatedly causes it to freeze completely.

fiigii avatar Apr 06 '26 21:04 fiigii

This needs to be fixed by default. We should be able to copy/paste into searchbar without having to manually right click and paste.

FrederickEngelhardt avatar May 16 '26 12:05 FrederickEngelhardt

I see several merge requests that are related to this issue, which one should we follow?

Shagon94 avatar Jun 17 '26 19:06 Shagon94

I always have this bug in the search bar (CMD+f), not only during "Change Tab Title" (I usually don't use any tabs). The right mouse button does work for copying/pasting. I am using v1.3.1 (build 15212). As a workaround I had to add the custom keybinds (+ghostty restart):

keybind = super+v=paste_from_clipboard
keybind = super+c=copy_to_clipboard

iocron avatar Jun 28 '26 11:06 iocron

Same issue here — Cmd+V does nothing while the search overlay (Cmd+F) is focused, on macOS 27.0 (build 26A5388g) with Ghostty 1.3.2-main (tip, commit 156bc8c81). Right-click → Paste in the search field works as expected. Happy to help test PR #12522 if needed.

NightGlowww avatar Aug 10 '26 06:08 NightGlowww

The bug is still in place, and the fix too, but this issue is forgotten

kvokka avatar Aug 29 '26 14:08 kvokka

I haven't looked at this in a lot of depth, but I can say that Ghostty is not behaving natively by stripping the menu action key equivalents from the menu bar for Copy, Paste, Find, etc. Invoking the keyboard actions like command c when you select text in the terminal window should trigger the menu bar action (for example, you should see the action flash/blink in the menu bar). So you should have performable bindings, and then whatever view is first responder should handle it, whether that's the surface view, the find bar, etc. It's unclear to me why that would prevent the surface view from receiving non-performed actions, but you may also want to cross-check the behavior with other native terminal apps (Terminal, iTerm2).

zorgiepoo avatar Aug 30 '26 01:08 zorgiepoo

I just switched from iTerm2 to Ghostty@tap a couple of days ago, and everything was smooth and/or configurable with reasonable defaults. This is the only confusing thing: the patch from @iocron is working (super+x & super+z could be benificial, but not critical)

kvokka avatar Aug 30 '26 01:08 kvokka