Add tmux and OSC 52 clipboard fallbacks for headless hosts#5816
Open
PatWie wants to merge 1 commit into
Open
Conversation
On a headless SSH host there is no X11 or Wayland display, so none of xclip, xsel, or wl-copy can reach a clipboard. atotto/clipboard then reports itself unsupported and every copy action fails with "No clipboard utilities available", even though installing those tools cannot help without a display server. This breaks copying branch names, commit hashes, file paths, and diffs in the common setup of developing on a remote box over SSH inside tmux. Extend CopyToClipboard and PasteFromClipboard with a built-in fallback chain that only engages when no os.copyToClipboardCmd is configured and clipboard.Unsupported is true, so any setup with a working native utility or a configured command is completely unaffected. When running inside tmux, copy via `tmux load-buffer -w -` and paste via `tmux save-buffer -`. The -w flag relays the buffer to the outer terminal's system clipboard through tmux's own OSC 52 emission (with `set-clipboard on`), and because the tmux buffer is readable this gives real bidirectional clipboard support within the session, including paste. Without tmux, emit an OSC 52 escape sequence to the controlling terminal, so terminals that support it still receive the copy. OSC 52 read-back is refused by most terminals for security reasons, so paste returns the last value copied in-session, matching how Neovim's and Helix's clipboard providers behave. The sequence framing is factored into a pure buildOSC52Sequence so it can be verified without touching the terminal. Failing both, fall through to the native atotto path unchanged. Fixes jesseduffield#5724
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On a headless SSH host there is no X11 or Wayland display, so none of xclip, xsel, or wl-copy can reach a clipboard. atotto/clipboard then reports itself unsupported and every copy action fails with "No clipboard utilities available", even though installing those tools cannot help without a display server. This breaks copying branch names, commit hashes, file paths, and diffs in the common setup of developing on a remote box over SSH inside tmux.
Extend CopyToClipboard and PasteFromClipboard with a built-in fallback chain that only engages when no os.copyToClipboardCmd is configured and clipboard.Unsupported is true, so any setup with a working native utility or a configured command is completely unaffected.
When running inside tmux, copy via
tmux load-buffer -w -and paste viatmux save-buffer -. The -w flag relays the buffer to the outer terminal's system clipboard through tmux's own OSC 52 emission (withset-clipboard on), and because the tmux buffer is readable this gives real bidirectional clipboard support within the session, including paste.Without tmux, emit an OSC 52 escape sequence to the controlling terminal, so terminals that support it still receive the copy. OSC 52 read-back is refused by most terminals for security reasons, so paste returns the last value copied in-session, matching how Neovim's and Helix's clipboard providers behave. The sequence framing is factored into a pure buildOSC52Sequence so it can be verified without touching the terminal.
Failing both, fall through to the native atotto path unchanged.
Fixes #5724
Disclosure and context
AI disclosure. The implementation was written by an AI coding agent that I
pointed at a private project of mine with the exact same problem and a
hand-written (pre-AI) fix. I reviewed every line, tested it end-to-end on the
headless SSH setup this exists for, and will handle review feedback myself.
Related issues (searched the tracker manually):
details in Feature request: built-in clipboard fallback (tmux / OSC 52) for headless SSH hosts #5724
Testing. Unit tests cover the fallback selection (
FakeCmdObjRunner) andthe OSC 52 framing (pure helper, incl. the empty-payload/clear-clipboard
guard). No integration test: the fallback gates on
clipboard.Unsupported,which the harness can't control. tmux and OSC 52 paths verified manually over
real SSH.
Untranslated errors. The new error strings stay plain English on purpose:
the pre-existing errors on this path come untranslated from atotto/clipboard
anyway, and exact messages are grep-able in bug reports. Happy to move them to
english.goif preferred.On the no-PR policy. I don't expect a merge. Posting for the reasons
CONTRIBUTING.md itself allows: it's the prototype for my feature request
#5724, it runs in my fork today, and others hitting the same wall can merge it
into theirs. Feel free to close it unmerged. No response owed.