Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OSC 52 clipboard updates from tmux & others #1054

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gwicke
Copy link

@gwicke gwicke commented Jul 17, 2019

Be more liberal in which options mosh accepts in clipboard updates, even
if those options are not preserved across the connection at this point.
This includes clipboard updates emitted by tmux, which is very
frequently used with mosh.

This eliminates the need for work-arounds like
https://gist.github.com/yudai/95b20e3da66df1b066531997f982b57b, which
did not even work for me.

Be more liberal in which options mosh accepts in clipboard updates, even
if those options are not preserved across the connection at this point.
This includes clipboard updates emitted by tmux, which is very
frequently used with mosh.

This eliminates the need for work-arounds like
https://gist.github.com/yudai/95b20e3da66df1b066531997f982b57b, which
did not even work for me.
@stlava
Copy link

stlava commented Jul 18, 2019

Hoping this can be merged in soon. The work around isn't working for us either.

@jdelman
Copy link

jdelman commented Sep 26, 2019

@gwicke I just built this from source and copying still isn't working as I expect - are you able to do it with a simple mouse highlight with set -g mouse on? Any special terminal escapes needed?

@gwicke
Copy link
Author

gwicke commented Sep 26, 2019

@jdelman, did you rebuild both ends of the connection?

For me this works with regular mouse highlights in tmux, as well as printf "\033]52;c;$(printf "%s" "blabla" | base64)\a" in a plain mosh shell. The main remaining limitation in mosh is that the copy buffer is size limited to a single UDP packet. In practice, copied text is truncated when it exceeds the size of one packet.

@jdelman
Copy link

jdelman commented Sep 26, 2019 via email

@jdelman
Copy link

jdelman commented Sep 26, 2019

@gwicke It's working - thanks so much.

@dvn0
Copy link

dvn0 commented Nov 7, 2019

Friendly bump on this. Would love to see OSC 52 support in Mosh

@fanktom
Copy link

fanktom commented Nov 21, 2019

I have build both client and server from master and it works if I run "\033]52;c;$(printf "%s" "blabla" | base64)\a" from a plain mosh shell. If I do the same from a tmux session it does not work.

Is there still any work (special copy symbols, etc.) needed on the tmux side?

Thanks for your help!

Client (Mac OSX):
mosh -v
mosh 1.3.2 [build mosh-1.3.2-86-g0cc492d-dirty]

Server (Debian):
mosh -v
mosh 1.3.2 [build mosh-1.3.2-86-g0cc492d]

@sunaku
Copy link

sunaku commented Nov 21, 2019

You need to escape your sequence one more time for tmux:

esc="\033]52;c;$(printf %s "blabla" | base64)\a"
esc="\033Ptmux;\033$esc\033\\" # again for tmux!
printf "$esc"

See also https://sunaku.github.io/tmux-yank-osc52.html

@fanktom
Copy link

fanktom commented Nov 22, 2019

Thanks for the heads up. With this and parts from https://github.com/samoshkin/tmux-config/blob/master/tmux/yank.sh it worked perfectly.

@gwicke
Copy link
Author

gwicke commented Nov 22, 2019

I am using this with native tmux clipboard support enabled in ~/.tmux.conf:

set -g set-clipboard on
set -g mouse on

With these two settings, both explicit tmux copy mode selections and mouse selections (up to a few hundred bytes) are copied to the clipboard via the mosh connection.

@sravan1611
Copy link

From Mac OSX (Mojave) - from iterm2 - initiated "mosh [email protected] -p 60001" to my remote linux server.

I have mosh 1.3.2 installed. And running tmux 3.0a on my remote server. With that I am not able to copy tmux buffer to osx clipboard.

If I do ssh instead of mosh, tmux buffer is getting copied to OSX clipboard. Attached my tmux config here.

Masters need your inputs

tmuxconf.txt

@gwicke
Copy link
Author

gwicke commented Dec 13, 2019

@sravan1611, you need mosh builds including this diff on both the client & server. After that, tmux copying should work the same way it does on SSH, except that copy buffer sizes are limited to what fits into a single UDP packet.

@jianhuiz
Copy link

Works perfectly, thanks and hope this can get merged soon

@andrewmzhang andrewmzhang mentioned this pull request Jan 28, 2020
@andrewmzhang
Copy link

andrewmzhang commented Jan 28, 2020

I accidentally made a pull request that was a verbatim copy of this PR with the same name and code. The travis tests for this PR are now linked to the duplicate PR I made. I apologize for screwing this up.

I think the travis tests for my duplicate PR are failing since OSX is not building correctly due to homebrew problems. Thus if this PR is travis checked again it may fail due to the same build problems.

@varung
Copy link

varung commented Apr 1, 2020

when will this be merged? And after that, how long till it will be available in brew? If there is some way to help push this along, can someone post what that is? Thank you!

@mgulick
Copy link

mgulick commented Jul 16, 2020

Hey, so I just created a pull request which (I think) supersedes this one. The issue I was having when I tested this PR (on Linux w/ XTerm) was that my copied text always went into the CLIPBOARD, whereas with SSH it would go into the PRIMARY selection. This new PR #1104 preserves the parameter in the OSC52 escape sequence which specifies which selection to put the copied text into.

@asanakoy
Copy link

Any updates on this? I would also love to have this feature in mosh.

arg added a commit to arg/dotfiles that referenced this pull request Sep 8, 2021
* Removed auto-pairs plugin - it only causes difficulties
* Fixed OSC52 yank - no more crashing on "y" key press
* Added abbreviation for binding.pry that I use a lot

Also yet another attempt to fix OSC52 In mosh-tmux environment.
Unfortunately mosh project is abandoned, so there is no chance
mobile-shell/mosh#1054 will be merged.
And those workarounds actually never work.
@catskul
Copy link

catskul commented Oct 20, 2022

Can someone with permission re-trigger the CI on this? Looks like the last time it ran was >3yrs ago so the log is gone by now.

@achernya
Copy link
Collaborator

I think the PR needs to be re-pushed. I don't see a way to re-run the CI since we moved from Travis to Github Actions in the meantime.

@catskul
Copy link

catskul commented Oct 20, 2022

Hm. @gwicke could you re-push this branch?

@mgulick
Copy link

mgulick commented Oct 20, 2022

@achernya, any interest in merging #1104 instead? When I implemented it, I looked into the OSC 52 protocol in detail and I think that MR handles all cases. More details in https://www.xfree86.org/current/ctlseqs.html. The only thing I'm not sure about is if is compatible between mis-matched server/client versions (i.e. those with and without the patch). I could probably try to make it work if that is a concern.

This patch, #1054, doesn't preserve the OSC52 type (i.e. clipboard, primary, select, cut buffers 0-7), so the xterm on the other side always stuffs the selection into the clipboard, rather than the original destination. #1104 does this, and fixes a few other things as well.

@achernya
Copy link
Collaborator

@mgulick I'll have to deep-dive both PRs to be able to make an educated assessment. Based on your description, yes, I'm interested, post 1.4 release. (I need to go make sure the Debian packages build now that we've identified and worked through the tmux issue)

@willdumm
Copy link

willdumm commented Nov 4, 2022

Although mosh 1.4.0 claims to support OSC52 copy, it still doesn't seem to work with tmux.

By "copy works" I mean that running printf "\033]52;c;$(printf "%s" "hello" | base64)\a" in a terminal that supports OSC 52 copy (for me, iTerm2) sets the local clipboard contents to hello.
My local machine and server A are both running mosh 1.4.0.
tmux server A and B is version 2.6, with set -g set-clipboard on in tmux.conf.

  1. Connected via mosh (both mosh client and server v1.4.0), copy works.
  2. Connected via mosh, but running tmux on remote, copy doesn't work.
  3. Connected via mosh to bastion host (server A), then via ssh to another server (server B), copy doesn't work.
  4. Connected via mosh to bastion host (server A), then via ssh to another server (server B), and running tmux on server B, copy doesn't work.
  5. Any of 1-4, but replacing mosh with ssh, copy works.

However, adding
set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7" to tmux.conf may fix (2) and (4) on tmux 3.3 but not 2.6? I need to experiment more with this.
(suggestion from here: https://rdzhou.github.io/2018/05/23/tmux-with-mosh/)

EDIT:
Indeed, using tmux v3.3a and putting

set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7"

in tmux.conf, solves (2) and (4), while still allowing copy to work when using tmux over plain ssh.
(3) is not solved, but outside the context of tmux, native terminal copy works fine
This seems to be a perfect workaround, if you have the freedom to run a recent enough version of tmux. It would be nice if no workaround was necessary, but it's still exciting to get this feature on mosh!

@riordant
Copy link

riordant commented Apr 5, 2023

Although mosh 1.4.0 claims to support OSC52 copy, it still doesn't seem to work with tmux.

By "copy works" I mean that running printf "\033]52;c;$(printf "%s" "hello" | base64)\a" in a terminal that supports OSC 52 copy (for me, iTerm2) sets the local clipboard contents to hello. My local machine and server A are both running mosh 1.4.0. tmux server A and B is version 2.6, with set -g set-clipboard on in tmux.conf.

  1. Connected via mosh (both mosh client and server v1.4.0), copy works.
  2. Connected via mosh, but running tmux on remote, copy doesn't work.
  3. Connected via mosh to bastion host (server A), then via ssh to another server (server B), copy doesn't work.
  4. Connected via mosh to bastion host (server A), then via ssh to another server (server B), and running tmux on server B, copy doesn't work.
  5. Any of 1-4, but replacing mosh with ssh, copy works.

However, adding set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7" to tmux.conf may fix (2) and (4) on tmux 3.3 but not 2.6? I need to experiment more with this. (suggestion from here: https://rdzhou.github.io/2018/05/23/tmux-with-mosh/)

EDIT: Indeed, using tmux v3.3a and putting

set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7"

in tmux.conf, solves (2) and (4), while still allowing copy to work when using tmux over plain ssh. (3) is not solved, but outside the context of tmux, native terminal copy works fine This seems to be a perfect workaround, if you have the freedom to run a recent enough version of tmux. It would be nice if no workaround was necessary, but it's still exciting to get this feature on mosh!

Exactly what I needed, thank you so much!

@cosmojg
Copy link

cosmojg commented Jun 27, 2023

Although mosh 1.4.0 claims to support OSC52 copy, it still doesn't seem to work with tmux.

By "copy works" I mean that running printf "\033]52;c;$(printf "%s" "hello" | base64)\a" in a terminal that supports OSC 52 copy (for me, iTerm2) sets the local clipboard contents to hello. My local machine and server A are both running mosh 1.4.0. tmux server A and B is version 2.6, with set -g set-clipboard on in tmux.conf.

  1. Connected via mosh (both mosh client and server v1.4.0), copy works.
  2. Connected via mosh, but running tmux on remote, copy doesn't work.
  3. Connected via mosh to bastion host (server A), then via ssh to another server (server B), copy doesn't work.
  4. Connected via mosh to bastion host (server A), then via ssh to another server (server B), and running tmux on server B, copy doesn't work.
  5. Any of 1-4, but replacing mosh with ssh, copy works.

However, adding set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7" to tmux.conf may fix (2) and (4) on tmux 3.3 but not 2.6? I need to experiment more with this. (suggestion from here: https://rdzhou.github.io/2018/05/23/tmux-with-mosh/)

EDIT: Indeed, using tmux v3.3a and putting

set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7"

in tmux.conf, solves (2) and (4), while still allowing copy to work when using tmux over plain ssh. (3) is not solved, but outside the context of tmux, native terminal copy works fine This seems to be a perfect workaround, if you have the freedom to run a recent enough version of tmux. It would be nice if no workaround was necessary, but it's still exciting to get this feature on mosh!

Hmm, this workaround stopped working for me all of a sudden. I'm running mosh 1.4.0 on both the client and the server, and I'm running tmux 3.3a on the server. The clipboard works just fine with mosh on its own and with tmux via ssh, but as before, tmux via mosh breaks the clipboard.

Notably, the above terminal-overrides option breaks the clipboard for tmux via ssh as well. Maybe something changed on the tmux side?

@catskul
Copy link

catskul commented Jun 28, 2023

iTerm2 just updated. I wonder if that has something to do with it.

@IamGianluca
Copy link

IamGianluca commented Oct 12, 2023

Updating mosh to 1.4.0 (both local and server-side) and tmux to 3.3a, plus the changes listed below to the tmux.conf file as suggested by @willdumm worked for me.

set -g set-clipboard on
set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p2%s\\7"

The curious thing is that my TERM is screen-256color, not xterm-256color.

@mellery451
Copy link

mellery451 commented May 8, 2024

word of warning - I think tmux 3.4 might have broken something because I can't get it work at all (osc52 through tmux + mosh). I've eliminated mosh by testing without tmux and Tmux version just recently updated so that's what I suspect. tmux/tmux#3964 seems possibly involved.

UPDATE - did some more testing and I think for tmux 3.4 you don't want the terminal overrides above. YMMV

@arg
Copy link

arg commented May 9, 2024

This works for me:

Local system

[I] ~$ sw_vers
ProductName:		macOS
ProductVersion:		14.4.1
BuildVersion:		23E224

[I] ~$ mosh -v
mosh 1.4.0 [build mosh 1.4.0]

Remote system

[I] ~$ freebsd-version                                                                                                                                                                           
14.0-RELEASE-p6

[I] ~$ mosh -v                                                                                                                                                                                   
mosh 1.4.0 [build mosh 1.4.0]

[I] ~$ tmux -V                                                                                                                                                                                   
tmux 3.3a

tmux.conf

set -ag terminal-overrides ",screen-256color:colors=256:Tc:Ms=\\E]52;c;%p2%s\\7"
set -g set-clipboard on

Clipboard works in both Tmux (yank in Vim mode) and Neovim (running in Tmux session with nvim-osc52 plugin). However the remote Tmux is 3.3, not 3.4. Clipboard in local 3.4 also works fine (I assume it's because of set-clipboard option).

Also to note, I connect to the remote directly (mosh <server name), not through bastion host.

@paddor
Copy link

paddor commented Jun 27, 2024

This is the issue that keeps me from using mosh daily.

intelfx added a commit to intelfx/dotfiles that referenced this pull request Jul 21, 2024
…h#1054

(we don't need it because we patch mosh for more relaxed parsing of
OSC 52 sequences)
intelfx added a commit to intelfx/dotfiles that referenced this pull request Jul 21, 2024


(we don't need it because we patch mosh for more relaxed parsing of
OSC 52 sequences)
intelfx added a commit to intelfx/dotfiles that referenced this pull request Jul 21, 2024


(we don't need it because we patch mosh for more relaxed parsing of
OSC 52 sequences)
intelfx added a commit to intelfx/dotfiles that referenced this pull request Jul 22, 2024


(we don't need it because we patch mosh for more relaxed parsing of
OSC 52 sequences)
@zmgilbert
Copy link

I'm a new mosh user and was disheartened by the copy/paste issue, since I use tmux all the time. This PR fixed all my woes!

  • tmux version: 3.0a
  • Ubuntu 22.04 Linux server
  • MacOS Sonoma 14.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.