Clipboard integration using OSC 52

I use Kakoune in WSL and usually inside tmux, which made setting up hooks for copying to system clipboard and/or tmux buffers, and keeping track of both a bit cumbersome.

I recently learned about and set up copying to system clipboard using OSC 52 escape sequence, after seeing that tmux has a set-clipboard option. They also recently created a very nice wiki page explaining its usage and OSC 52.

I found a few advantages to using the escape sequence:

  1. tmux integration is transparent with the set-clipboard on option: If tmux is running, the escape sequence sent by an application will set tmux buffer which will get forwarded to the terminal by tmux, otherwise it will have directly sent it to the terminal
  2. The clipboard will get forwarded through SSH sessions, so that you can copy from terminal applications from the remote application

Kakoune setup

Following the examples in Kakoune wiki, we can hook up the escape sequence to a yank/delete/change operation:

hook global NormalKey y|d|c %{
    nop %sh{
        encoded=$(printf %s "$kak_main_reg_dquote" | base64 | tr -d '\n')
        printf "\e]52;;%s\e\\" "$encoded" >/dev/tty
    }
}

tmux setup

AFAICT tmux 2.6+ supports the set-clipboard on option, while the default is set-clipboard external which will let applications set the tmux buffer but tmux will not forward them to the terminal. set-clipboard on will also enable forwarding to the terminal to set the system clipboard.

Terminal support

Most terminals seem to have support for setting the system clipboard when the sequence is received. The aforementioned tmux wiki page details support for most terminals, some of which requires setting options; a notable holdout is VTE terminals like Gnome terminal.

I use WSLtty which is not documented there, but it supports it by setting AllowSetSelection=true in its config file.

1 Like