Mosh + Tmux + Copy Paste

OUTDATED

This guide is outdated. I’m keeping this up for my own records. The updated guide to setting this up will be placed

The overarching goal is to set up what I believe to be the ideal remote development terminal setup; mosh into remote -> tmux + ability to copy paste to local machine.

Background

To achieve the stated goal, we implement the OSC 52 escape command within the default terminal provided by the Ubuntu OS 16.04 (gnome-terminal) so I could copy and paste to the system clipboard through terminal escapes, ie printf "\033]52;c;$(printf "%s" "test" | base64)\a" should copy “test” into the local OS clipboard.

This particular version of vte was chosen due to the fact that its the default version of libvte for ubuntu 16.04, which is what I use. This patch won’t work for more modern versions of vte as the code has changed significantly.

Unsolved problems

Since the goal is for personal use on tmux, this OSC 52 patch does not cover all use cases for OSC 52. In fact this patch only supports the c; option (it assumes all OSC 52 escapes are trying to reach the clipboard). osc 52 documentation

Git Patch

patch code

git clone https://github.com/andrewmzhang/vte.git

To Install w/o Overriding Default libvte Install

# Dependencies

sudo apt-get install gtk-doc-tools gobject-introspection valac libvala-dev libgnutls-dev libgirepository1.0-dev gperf

# Will install to /opt/vte

./autogen.sh --prefix=/opt/vte
make
sudo make install

Setting Up gnome-terminal

Initially I tried to devise a way to have 2 different gnome-terminal executables, one that would launch with the doctored libvte library and the other untouched, since I don’t have functionality to turn off OSC 52 escapes once implemented. Unfortunately it seems that gnome-terminal uses the system global name “org.gnome.Terminal” to communicate through the dbus. I don’t know how dbus works, so I didn’t attempt to manipulate it.

When gnome-terminal launches, it actually communicates w/ gnome-terminal-server (or launches it) and gnome-terminal exits (see post on this: gnome-terminal-server-explained ). So we actually need to get gnome-terminal-server to use our doctored lib.

gnome-terminal/gnome-terminal-server version 3.18.3 on the defaul Ubuntu install seems to be a bit different from what sits at the gnome gitlab repository. Instead of trying to install whatever patches Ubuntu utilizes, I opted to relink the gnome-terminal-server executable to point to our doctored lib.

# Will show you what libvte links to

ldd /usr/lib/gnome-terminal/gnome-terminal-server | grep "vte"

# outputs: libvte-2.91.so.0 => /usr/lib/x86_64-linux-gnu/libvte-2.91.so.0

# Backup gnome-terminal-server

sudo cp /usr/lib/gnome-terminal/gnome-terminal-server /usr/lib/gnome-terminal/gnome-terminal-server.bak

# Relink the library; Run the patchelf on a non libvte based emulator (eg xterm)

sudo apt install -y patchelf
sudo patchelf --replace-needed libvte-2.91.so.0 /opt/vte/lib/libvte-2.91.so.0 /usr/lib/gnome-terminal/gnome-terminal-server

Fixing the colours

The colours might have changed (for me libvte put up a white background w/ black text). To fix this open gnome-terminal->profile preferences->colors

Change the text color to: #FEF8D9 Change the background color to: #300A24 Bold color to: [x] Same as text color

Setting Up tmux and mosh

First, we need to tell tmux to copy via OSC 52. To do this we need to set the clipboard.

Tmux supports OSC 52 but does not pass the “c;” option, according to yudai’s post. However we can force it to pass the “c;” option. Mosh is capable of catching the OSC 52 option c (the default mosh only accepts the “c;” option). Thus adding these lines to the tmux.conf should allow us to copy into local clipboard. I think if you install this mosh pr it supports the other OSC 52 options.

Note: This only works for more recent version of tmux.

# sets tmux to use OSC 52 escape

set -g set-clipboard on

# Forces tmux to use the "c;" option

set -ag terminal-overrides "vte*:XT:Ms=\\E]52;c;%p2%s\\7,xterm*:XT:Ms=\\E]52;c;%p2%s\\7"

Contributions

Big thanks to Kevin Zheng for providing most of the code :)




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Custom TLD Over Tailscale
  • Running step-ca in docker w/ Yubikey
  • Notes on Arduino GPS Library
  • Oracle Cloud VM Docker No Route To Host
  • Reverse Proxies With Custom ACME