1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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`:
```bash
printf "\033]52;c;%s\007" "$(base64 <&0)"
```
and I run it with:
```bash
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](https://neovim.io/doc/user/provider.html#clipboard-osc52)):
```lua
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.
|