Skip to content

Instantly share code, notes, and snippets.

@yudai
Created November 29, 2017 01:04
Show Gist options
  • Save yudai/95b20e3da66df1b066531997f982b57b to your computer and use it in GitHub Desktop.
Save yudai/95b20e3da66df1b066531997f982b57b to your computer and use it in GitHub Desktop.
tmux + mosh OSC 52 clipboard paste hack

Problem

Now tmux, mosh and iTerm2 support the OSC 52 sequence that enables clipboard sharing. However, there is a trap that prevents them from working together.

Mosh accepts OSC 52 sequences with the c; option. However, tmux doesn't send that option when it emits OSC 52 sequences, which means you cannot use tmux and mosh together with the default configuration.

Workaround

You can override the OSC 52 sequence generated by tmux by adding the following line to your tmux.conf.

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

Instead of using the default Ms sequence Ms=\\E]52;%p1%s;%p2%s\\7", whose %p1%s; is the option part, this configuration forces use c; as the option value.

Note that xterm-256color must be replaced with your $TERM value.

@agriffis
Copy link

agriffis commented Feb 5, 2018

Thanks, just finished getting tmux + mosh + hterm (Chrome OS) cooperating with OSC 52, and this was a big help!

@tylerhoran
Copy link

@agriffis do you mind sharing your tmux.conf for tmux + mosh + chrome os ?

@agriffis
Copy link

agriffis commented Jul 3, 2018

@tylerhoran Here are the most interesting bits

# tmux-256color instead of screen-256color enables italics
set -g default-terminal "tmux-256color"

# Tc enables true color
set -ag terminal-overrides ",*256col*:colors=256:Tc"

# Ms modifies OSC 52 clipboard handling to work with mosh, see
# https://gist.github.com/yudai/95b20e3da66df1b066531997f982b57b
set -ag terminal-overrides "vte*:XT:Ms=\\E]52;c;%p2%s\\7,xterm*:XT:Ms=\\E]52;c;%p2%s\\7"

# enable OSC 52 clipboard
# https://medium.freecodecamp.org/tmux-in-practice-integration-with-system-clipboard-bcd72c62ff7b
set -g set-clipboard on

# use bracketed paste, if the running application (vim/emacs/weechat) has
# sent the terminal code to enable it.
bind-key ] paste-buffer -p

setw -g aggressive-resize on

# http://comments.gmane.org/gmane.emacs.vim-emulation/1557
set -s escape-time 0

@tylerhoran
Copy link

thanks @agriffis!

@gwicke
Copy link

gwicke commented Jul 17, 2019

PR for mosh that avoids the need for custom tmux settings: mobile-shell/mosh#1054

@dasizeman
Copy link

@yudai @tylerhoran @agriffis @gwicke Do you know what the minimum mosh version needed for this is? It's not working for me and I wish it did, I would love to get this working without having to build a custom mosh right now (but thanks for your work @gwicke!)

@agriffis
Copy link

@dasizeman There isn't a release of mosh with truecolor support. You need unreleased trunk. mobile-shell/mosh#974

If you're running Fedora, you can use my COPR https://copr.fedorainfracloud.org/coprs/agriffis/mosh-nightly/

@ilyagr
Copy link

ilyagr commented Nov 29, 2022

Alternative PR that claims to improve on the one @gwicke suggested: mobile-shell/mosh#1104

But actually, this seems to be a bug in tmux that is already fixed on the master branch (and thus hopefully in the next release): tmux/tmux#3192

@ipatch
Copy link

ipatch commented May 31, 2023

not sure if the clipboard option was removed or not but running

tmux show-options -g clipboard

yields

invalid option: clipboard

@jaclu
Copy link

jaclu commented Jul 9, 2023

not sure if the clipboard option was removed or not but running

tmux show-options -g clipboard

yields

invalid option: clipboard

You need to do: tmux show-options -g set-clipboard

@mellery451
Copy link

warning that this seems to break osc52 with tmux 3.4 which has added some additional OSC features/support

@popjdh
Copy link

popjdh commented Aug 16, 2024

set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p1%s%p2%s\\7" is ok in tmux 3.4. But I dont find any description about %p1%s , It seems that no content is generated by default.

Updated on August 24th
According to tmux#4081, set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c%p1%.0s;%p2%s\\7" should be a better choice. @syu-id

@syu-id
Copy link

syu-id commented Aug 18, 2024

set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c;%p1%s%p2%s\\7" is ok in tmux 3.4. But I dont find any description about %p1%s , It seems that no content is generated by default.

Thanks @popjdh! This works for both tmux 3.4 and 3.3a on my machines.

@jaclu
Copy link

jaclu commented Dec 18, 2024

For me set-option -ag terminal-overrides ",xterm-256color:Ms=\\E]52;c%p1%.0s;%p2%s\\7" works for all tmux versions I have tested down to 1.7. I wouldn't normally run ancient versions just for the fun of it, but since i often test compatibility with tmux plugins, I tend to see how far back a given plugin works.

However when testing various versions I came across a pretty annoying cut-paste issue.

If you use asdf to use multiple versions of tmux, be aware that if you start a remote tmux in a folder with a local tmux version, anytime you select something with the tmux buffers, tmux insta-craches! This happens even if the local tmux version is the same as the global one.
You can still use the terminals cut & paste features.

On localhost sessions, using other asdf versions of tmux as local overrides works fine, but on a remote session, be it ssh or mosh, you need to use set -g set-clipboard off in order to be able to use the tmux buffers.

this is regardless if the OSC 52 fixes mentioned here are used or not.

kind of an odd issue, so perhaps I'm doing something wrong, but disabling tmux clipboard was the only solution I could find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment