OSC-52
I use ssh
a lot. Copying text from the remote machine to
the host machine always sucked. But OSC-52 makes that easy.
OSC-52 is an ANSI escape sequence to write text to the terminal emulator. The terminal emulator, if it understands what is going on, will in turn write this text to the system clipboard.
What this means is some printf
magic can send text to
your clipboard. I store this one-liner in a script called
oclip
:
printf "\033]52;c;%s\007" "$(base64 <&0)"
and I run it with:
remote $ cat some_file.txt | oclip
# some_file.txt's contents are now the host's clipboard
The catch
Your terminal emulator must support OSC-52, alacritty
and termux
seem to support this out of the box. In
st
, OSC-52 works with this change to
config.h
:
int allowwindowops = 1;
If you are using tmux
, you need to flip this switch
on:
set -s set-clipboard on
If you are inside nvim
, it may work as expected as long
as $SSH_TTY
is set. I sometimes physically start a session,
and ssh
into the same session later from another machine,
and $SSH_TTY
remains unset, so I force OSC-52 in
nvim
at all times (see nvimdoc):
vim.g.clipboard = {
name = 'OSC 52',
copy = {
['+'] = require('vim.ui.clipboard.osc52').copy('+'),
['*'] = require('vim.ui.clipboard.osc52').copy('*'),
},
paste = {
['+'] = require('vim.ui.clipboard.osc52').paste('+'),
['*'] = require('vim.ui.clipboard.osc52').paste('*'),
},
}
If you are inside nvim
inside tmux
inside
an ssh
session inside st
, you neeed all of the
above tweaks. nvim
will pass the contents around to
tmux
, which in turn will pass the contents to
st
, which should pass it to your system clipboard.
I'm Akshay, programmer and pixel-artist. I write open-source stuff. I also design fonts: scientifica, curie.
Reach out at oppili@irc.rizon.net.