Support additional OSC 52 clipboard types#1104
Conversation
008c27c to
1586486
Compare
1586486 to
e922128
Compare
|
will it be merged? this MR works fine on my Linux server & Mac client. |
|
Seems to work okay with tmux copy mode. If you are migrating from #1054, then make sure to rebuild both client & server from this PR, since the wire format changed. |
That is true about the wire format. I believe it is incompatible with the current wire format used by the master branch. I don't quite grok the wire format used by mosh and haven't been able to figure out where in the code fields used by the framebuffer get turned into bits sent on the wire. Since there hasn't been an official mosh release since clipboard support was added, I don't believe it causes any compatibility issues with previous releases. If it does introduce incompatibility, then I believe the original commit adding clipboard support would also have introduced the same sort of incompatibility issues. If this wire format compatibility is a concern, it might be possible to make this backwards compatible, but I might need some help understanding the wire format a little more. |
|
Confirming this fixes all remaining clipboard issues for me on a local client running macOS Catalina v10.15.6 + iTerm2 v3.3.12 + mosh v1.3.2 [build mosh-1.3.2-92-ge922128 using: It's taken over ten years to get to this point; the holy grail has finally been achieved!!! Well done, @mgulick!! |
|
+1 to merge |
I saw from other issues that there were a couple of blockers to a new release. Creating a pinned issue and a milestone for the next release would help communicate this. (This is offtopic to this PR though) |
|
@eminence another ping - would be great to have this/ and sounds like you have offers for more help to support mosh. |
|
Can anyone familiar with this PR see if 4b240ac resolves the same issues noted in the first message? |
No, this PR builds on that commit to add some additional functionality that is missing. |
|
This almost works. But I cannot make it work in one scenario What worked successfully remote copy to local clipboard. But if I put this in ~/.tmux.conf. (~/bin/yank is copied from this well-known script: https://sunaku.github.io/tmux-yank-osc52.html). However, in a ssh session, this works without any issue. I tweaked it further. then in another window, if I I dont know what the root cause here. but hunch is related to tmux's pane_tty. |
|
Hi @delbao, what happens if you change the |
Tried SSH_TTY, nothing changed. This issue is specific to this patch of mosh. SSH works fine. $ tmux -V |
|
@delbao Here is a copy of my (disclaimer: the following info may be out of date) Older(?) tmux needs the last line because:
|
thanks for the help. However, sunaku's script already did what you described. it doesn't work for Mosh with this patch. |
|
bumping this thread b/c this pr saved me hours of time. working flawlessly on a debian vps connected from my ipad using blink shell in zsh, tmux, and neovim using osc52 tooling |
Support additional OSC 52 clipboard types
|
I have applied the patch but when copying to clipboard in emacs I get this error But it can be fixed if I start emacs like so unset SSH_TTY; emacsAnyway, thanks for this PR, it works now but not out of the box. |
|
Scrolled through this thread but failed to figure out: Is there any good reason why this was not merged? If this repo is abandoned, is there another more actively-maintained fork? |
|
Hi, This PR hasn't been merged for two reasons:
Just because there are PRs open doesn't mean the repository is abandoned. |
|
I absolutely respect maintainers' right to handle any PRs in any way. Glad to know there's no technical reason against this PR so far. OSC 52 is not working for me (and many others I believe), and this is the only blocker for me to use mosh. I hope maintainers consider prioritizing this PR. After all it's a relatively small code change for such an important feature. To be fair, the second reason doesn't stand to me - the merge conflicts are more of a consequence, not reason, of that this PR hasn't been got to for 3+ years. |
|
I just rebased these patches onto master. There were no "real" conflicts, just some indentation changes. I built these changes again and did a quick test to confirm they still appear to be working. I've been using these patches in my daily workflow for a few years, albeit on an older mosh version based on the original PR patch series. The XTerm bug mentioned at the end of the PR description has been fixed as of xterm version 359. I did a quick test using this patched mosh and an unpatched server. While clipboard syncing does not work (to be expected), mosh itself appears to function normally, so maybe there are no backwards-compatibility issues with this change. |
|
I'd like to try out this PR, but I cannot easily compile the client. I can easily compile the server, though. Is that enough? |
|
@joaotavora both the client and server need this change, however you don't need to install them in the usual locations. As long as you have the This does not require updating the |
|
Thanks @mgulick. Though somehow, compiling just the server worked. at least for very limited testing. I was able to copy to the clipboard from Emacs and paste in my host's browser. I can see the client binary linking in the terminal subsystem though, but I don't know enough of the architecture to understand why that is or why my test worked. |
|
@joaotavora I think on recent mosh releases, standard clipboard copying will work without this patch. I'm able to do the standard clipboard updates going from a 1.4.0 client to a 1.4.0 server without this patch. However the other issues, such as primary, secondary, and cutbuffer support, as well as the issue with duplicate clipboard updates not getting properly synchronized (#1090), will still exist. |
|
Thanks, could have sworn it didn't work before I compiled and installed your version though, maybe I misdiagnosed 👍 |
|
so i've been using the older version of this patch for a long time with no issues beyond when copying higher level unicode and emojis. strings like this would not be correctly copied to the clipboard instead replaced with a placeholder for each character.
|
|
@ezzieyguywuf If you have permission to merge, PTAL |
|
@achernya any news on this PR merge possibility ?? |
|
@mgulick Clipboard does not work for me when using mosh 1.4.0 on both client and server. It works with SSH. Perhaps something relevant about this, is that I'm using it on a fairly old CentOS 7 server. |
|
It doesn't look like mosh has much activity in terms of commits these days, but I'm still interested in getting this PR merged if there is an opportunity to do so. It would be great to be able to eventually have this upstreamed and in distros so that I can stop building a custom mosh on every system that I access. |
|
FWIW I have run @jdrouhard's fork https://github.com/jdrouhard/mosh/tree/patched containing this PR and some others for over a year. Thanks to everyone that has produced these fixes. Community in action 🌞. |
There is recent activity from maintainers, so I hope they can take another look at this. They aren't listed as assigned reviewers, so I'm not sure they get pings. Speaking for myself, when acting as a maintainer, it's easy for these much-requested PRs to fly under the radar, so pinging @achernya @vapier @eminence I applied the predecessor PR locally and confirmed it made things work and I was preparing to rebase and submit a new PR, but I hadn't dug up any actual material on how the OSC protocol is supposed to work to really understand why these changes (ignoring 'c' options and skipping to the next ';') are correct. |
I've actually reviewed the PR code, pull request message, referenced the original archived xterm plain text docs (helpfully linked in the PR) , and the ghostty docs on OSC 52, which are more readable and modern. I think there's no wire protocol backwards compatibility concern. I tried to dig into how the clipboard gets serialized, but and it looks like the OSC string goes directly over the wire, so if we have an old client with a non Regarding testing, I used an LLM to add tests using the existing testing conventions and pushed them in this commit. I confirm the tests pass after and fail before these source changes as expected, and the error messages are useful. If that's a blocking concern, feel free to merge those changes into this PR. |
|
If there is renewed interest from a maintainer on getting this merged, I'm happy to help with whatever is necessary as well. |
|
i'm not a maintainer, just a rando who got lucky and had some fixes merged |
Stock mosh 1.4.0 drops OSC 52 sequences from tmux because it doesn't handle the selection buffer parameter. Use jdrouhard's fork which includes the fix from PR #1104 (mobile-shell/mosh#1104). This enables tmux copy-paste through mosh to reach the local clipboard via Ghostty's OSC 52 support.
Under tmux 3.6a + mosh 1.4.0, both the set-clipboard built-in and DCS
passthrough via #{pane_tty} fail — tmux writes OSC 52 to its own
controlling pty, which is the mosh-server's *internal* pty, not the
outer pty mosh forwards to the client. Result: copy-mode y never
updated the macOS clipboard when attached over mosh.
tmux-osc52-copy now resolves the most-recently-active #{client_tty}
for the pane's session and writes raw OSC 52 directly there, bypassing
tmux entirely. That pty is owned by mosh-server and reaches Ghostty.
Also: allow-passthrough all → on (OSC 52 path no longer uses DCS
passthrough; 'on' still covers OSC 11 bg-color query for TUIs).
Verified: writing OSC 52 to /dev/ttysXXX (client_tty) reaches the
client pasteboard; writing to #{pane_tty} does not.
Ref: mobile-shell/mosh#1104 (delbao, 2022)
Constraint: stock Homebrew mosh 1.4.0 + tmux 3.6a — no custom builds
Rejected: mosh PR #1104 patched build | adds non-stock package maintenance
Rejected: pane-stdout write via cat trick | run-shell -b has no TTY
Confidence: high
Scope-risk: narrow
Directive: tmux-osc52-copy MUST scope client lookup to current pane's session; cross-session write would clobber another client's clipboard
Not-tested: multi-user tmux server where target_tty belongs to another user (guarded by [ -w "\$tty" ] silent-skip)
Picks up mobile-shell/mosh#1104 to fix tmux→mosh→Ghostty clipboard forwarding without the tmux-side copy-pipe workaround. Applied to both the user mosh client (config/home.nix) and the mosh-server module (modules/mosh.nix) so the fix lands end-to-end.
## Why On a `local terminal -> mosh -> tmux -> shell` stack, OSC 52 clipboard writes silently disappear. mosh-server 1.4 accepts only the `c` (CLIPBOARD) selector and drops every other variant (PRIMARY, `s0`, empty). tmux's default `Ms` capability passes through whatever selector copy-mode or the inner app picked, so most of the time tmux hands mosh a sequence mosh refuses to relay. ## What In `home/tmux.nix`: - `set -g set-clipboard on` makes tmux relay copy events via the `Ms` terminfo capability instead of only storing them in its internal buffer. - `set -ag terminal-overrides ',*:Ms=\E]52;c;%p2%s\7'` patches `Ms` to always emit `\e]52;c;<base64>\a` regardless of the requested selector, which is the form mosh-server 1.4 accepts. With both, double-click selection, copy-mode (`prefix [` ... `Enter`), and inner-app OSC 52 emissions (nvim `osc52`, scripts that print the escape) all reach the local Ghostty clipboard. ## Refs - tmux/tmux#3423 tmux interferes with OSC 52 when running in mosh - mobile-shell/mosh#1054 broaden accepted OSC 52 selectors - mobile-shell/mosh#1104 additional clipboard selector types ## Test plan - nix build .#nixosConfigurations.spark.config.system.build.toplevel built clean on this branch - The two set-option lines were live-applied to the running tmux server and verified via tmux show-options - After just switch, restart tmux server and confirm copy persists - Verify with: printf '\e]52;c;%s\a' "$(printf hello | base64 -w0)" then paste locally Reviewed-on: https://git.harivan.sh/harivansh-afk/nix/pulls/78 Co-authored-by: Harivansh Rathi <rathiharivansh@gmail.com> Co-committed-by: Harivansh Rathi <rathiharivansh@gmail.com>
|
@mgulick thanks for the patch, looks like I could use it too. Does it work with tmux? |
|
I'm also happy to help! (also not a maintainer I think) |
|
Thanks for this, @mgulick and @ezzieyguywuf — preserving the OSC 52 selection parameter is exactly the behaviour I was after. This works perfectly with tmux. I love it🙏 For anyone who'd like to try the PR before it's merged, here's how to build a working mosh from this branch. The OSC 52 changes live in both 1. Install build dependenciesmacOS (Homebrew): brew install autoconf automake pkgconf protobuf(On macOS mosh uses the system CommonCrypto and ncurses, so OpenSSL isn't needed.) Debian / Ubuntu: sudo apt-get install autoconf automake pkg-config protobuf-compiler \
libprotobuf-dev libutempter-dev zlib1g-dev libncurses-dev libssl-dev2. Get the PR and build itgit clone https://github.com/mobile-shell/mosh.git
cd mosh
git fetch origin pull/1104/head:osc52 && git checkout osc52
./autogen.sh
./configure
makeThis produces sudo make install # installs to /usr/local by default3. Use itmosh user@your-servermosh runs the client locally and the server on the host you connect into, so install this build on both machines. If you only built the new server on the remote host, you can point an existing local mosh at it with: mosh --server=/path/to/mosh-server user@your-serverVerified on macOS (Apple Silicon, Homebrew) and the standard autotools flow on Linux. 🎉 macOS: make a binary package you can copy to another Mac (no recompiling)If you want to build once and copy the result to other Macs, wrap the branch in a tiny local Homebrew formula and bottle it: brew tap-new "$USER/local"
cat > "$(brew --repository "$USER/local")/Formula/mosh-osc52.rb" <<'RUBY'
class MoshOsc52 < Formula
desc "mosh with OSC 52 clipboard types (PR #1104)"
homepage "https://mosh.org"
url "https://github.com/mgulick/mosh.git", branch: "osc-52-clipboard-types"
version "1.4.0-osc52"
license "GPL-3.0-or-later"
depends_on "autoconf" => :build
depends_on "automake" => :build
depends_on "pkgconf" => :build
depends_on "protobuf"
uses_from_macos "ncurses"
def install
ENV.append_to_cflags "-DNDEBUG"
system "./autogen.sh"
system "./configure", "--prefix=#{prefix}"
system "make", "install"
end
end
RUBY
brew install --build-bottle "$USER/local/mosh-osc52"
brew bottle "$USER/local/mosh-osc52" # emits a relocatable .bottle.tar.gzCopy that |
|
I built and tested this, it seems to work as expected. I can provide a code review too if that will help move this along. |
|
@mgulick I rebased this on master (it has some commits a few months ago) and ran the tests. A few are failing: I'm not sure if these are related to your changes or not, but it may be worth looking into |
|
XFAIL is "expected to fail and did" |
Oh sweet, then everything passes! |
Fixes #967.
Fixes #1090.
Fixes #637.
NOTE: This pull request is similar to #1054, but improves it by preserving the selection buffer into which to place the copied text. #1054 always places the copied text into CLIPBOARD, but this preserves the OSC 52 parameter so that the copied text may go into CLIPBOARD, PRIMARY, SECONDARY, or one of the cut buffers. This matches the behavior of
sshand plain XTerm.The OSC 52 escape sequence supports specifying which X selection buffer to place the selection into. The protocol format is:
The C parameter determines the selection buffer. It consists of zero or more of the following characters:
c: CLIPBOARD
p: PRIMARY
q: SECONDARY
s: XTerm-selected (based on XTerm.vt100.selectToClipboard)
0-7: Numbered cut buffer
The default if left blank is 's0', representing the configurable primary/clipboard selection and cut buffer 0. [1]
D is the base64 encoded text to place in the selection buffer.
This patch modifies the transferred clipboard data to include both the selection parameter and the base64 text. I.e. previously the
transferred clipboard data only contained 'D', and now it contains 'C;D'.
To test this functionality:
$ printf "\033]52;c;$(echo test1234 | base64)\007"$ printf "\033]52;p;$(echo test2345 | base64)\007"$ printf "\033]52;q;$(echo test3456 | base64)\007"$ printf "\033]52;;$(echo testdefault | base64)\007"(This should update either the clipboard or primary selection based on the Xterm settings)
xcutselto transfer the data from the cut buffer to the PRIMARY selection, where it can then be viewed withxclip.Note, I observed that XTerm does not currently (as of XTerm patch 358) support specifying more than one value in C. The specification does support it, and this appears to just be a simple bug in XTerm (a patch has been submitted to the maintainer to fix it).
[1] https://github.com/ThomasDickey/xterm-snapshots/blob/master/ctlseqs.txt
This PR also adds a clipboard sequence number to allow duplicate copied text to update the client's clipboard.