diff options
Diffstat (limited to 'posts/OSC-52.md')
-rw-r--r-- | posts/OSC-52.md | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/posts/OSC-52.md b/posts/OSC-52.md new file mode 100644 index 0000000..8dccef9 --- /dev/null +++ b/posts/OSC-52.md | |||
@@ -0,0 +1,66 @@ | |||
1 | I use `ssh` a lot. Copying text from the remote machine to | ||
2 | the host machine always sucked. But OSC-52 makes that easy. | ||
3 | |||
4 | OSC-52 is an ANSI escape sequence to write text to the | ||
5 | terminal emulator. The terminal emulator, if it understands | ||
6 | what is going on, will in turn write this text to the system | ||
7 | clipboard. | ||
8 | |||
9 | What this means is some `printf` magic can send text to your | ||
10 | clipboard. I store this one-liner in a script called | ||
11 | `oclip`: | ||
12 | |||
13 | ```bash | ||
14 | printf "\033]52;c;%s\007" "$(base64 <&0)" | ||
15 | ``` | ||
16 | |||
17 | and I run it with: | ||
18 | |||
19 | ```bash | ||
20 | remote $ cat some_file.txt | oclip | ||
21 | |||
22 | # some_file.txt's contents are now the host's clipboard | ||
23 | ``` | ||
24 | |||
25 | ### The catch | ||
26 | |||
27 | Your terminal emulator must support OSC-52, `alacritty` and | ||
28 | `termux` seem to support this out of the box. In `st`, | ||
29 | OSC-52 works with this change to `config.h`: | ||
30 | |||
31 | ``` | ||
32 | int allowwindowops = 1; | ||
33 | ``` | ||
34 | |||
35 | If you are using `tmux`, you need to flip this switch on: | ||
36 | |||
37 | ``` | ||
38 | set -s set-clipboard on | ||
39 | ``` | ||
40 | |||
41 | If you are inside `nvim`, it may work as expected as long as | ||
42 | `$SSH_TTY` is set. I sometimes physically start a session, | ||
43 | and `ssh` into the same session later from another machine, | ||
44 | and `$SSH_TTY` remains unset, so I force OSC-52 in `nvim` at | ||
45 | all times (see | ||
46 | [nvimdoc](https://neovim.io/doc/user/provider.html#clipboard-osc52)): | ||
47 | |||
48 | ```lua | ||
49 | vim.g.clipboard = { | ||
50 | name = 'OSC 52', | ||
51 | copy = { | ||
52 | ['+'] = require('vim.ui.clipboard.osc52').copy('+'), | ||
53 | ['*'] = require('vim.ui.clipboard.osc52').copy('*'), | ||
54 | }, | ||
55 | paste = { | ||
56 | ['+'] = require('vim.ui.clipboard.osc52').paste('+'), | ||
57 | ['*'] = require('vim.ui.clipboard.osc52').paste('*'), | ||
58 | }, | ||
59 | } | ||
60 | ``` | ||
61 | |||
62 | If you are inside `nvim` inside `tmux` inside an `ssh` | ||
63 | session inside `st`, you neeed all of the above tweaks. | ||
64 | `nvim` will pass the contents around to `tmux`, which in | ||
65 | turn will pass the contents to `st`, which should pass it to | ||
66 | your system clipboard. | ||