From 92a1fd5c5ee1fb0b4079128a0b7c6a1d78a2399d Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 28 Nov 2024 04:31:48 +0530 Subject: new post: OSC-52 --- posts/OSC-52.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 posts/OSC-52.md (limited to 'posts/OSC-52.md') 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 @@ +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. -- cgit v1.2.3