···1212 <language>en-us</language>
1313 <copyright>Creative Commons BY-NC-SA 4.0</copyright>
1414 <item>
1515+<title>OSC-52</title>
1616+<description><p>I use <code>ssh</code> a lot. Copying text from the remote machine to
1717+the host machine always sucked. But OSC-52 makes that easy.</p>
1818+<p>OSC-52 is an ANSI escape sequence to write text to the terminal
1919+emulator. The terminal emulator, if it understands what is going on,
2020+will in turn write this text to the system clipboard.</p>
2121+<p>What this means is some <code>printf</code> magic can send text to
2222+your clipboard. I store this one-liner in a script called
2323+<code>oclip</code>:</p>
2424+<div class="sourceCode" id="cb1"><pre
2525+class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">&quot;\033]52;c;%s\007&quot;</span> <span class="st">&quot;</span><span class="va">$(</span><span class="fu">base64</span> <span class="op">&lt;&amp;</span><span class="dv">0</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
2626+<p>and I run it with:</p>
2727+<div class="sourceCode" id="cb2"><pre
2828+class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">remote</span> $ cat some_file.txt <span class="kw">|</span> <span class="ex">oclip</span></span>
2929+<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
3030+<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># some_file.txt&#39;s contents are now the host&#39;s clipboard</span></span></code></pre></div>
3131+<h3 id="the-catch">The catch</h3>
3232+<p>Your terminal emulator must support OSC-52, <code>alacritty</code>
3333+and <code>termux</code> seem to support this out of the box. In
3434+<code>st</code>, OSC-52 works with this change to
3535+<code>config.h</code>:</p>
3636+<pre><code>int allowwindowops = 1;</code></pre>
3737+<p>If you are using <code>tmux</code>, you need to flip this switch
3838+on:</p>
3939+<pre><code>set -s set-clipboard on</code></pre>
4040+<p>If you are inside <code>nvim</code>, it may work as expected as long
4141+as <code>$SSH_TTY</code> is set. I sometimes physically start a session,
4242+and <code>ssh</code> into the same session later from another machine,
4343+and <code>$SSH_TTY</code> remains unset, so I force OSC-52 in
4444+<code>nvim</code> at all times (see <a
4545+href="https://neovim.io/doc/user/provider.html#clipboard-osc52">nvimdoc</a>):</p>
4646+<div class="sourceCode" id="cb5"><pre
4747+class="sourceCode lua"><code class="sourceCode lua"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="va">vim</span><span class="op">.</span><span class="va">g</span><span class="op">.</span><span class="va">clipboard</span> <span class="op">=</span> <span class="op">{</span></span>
4848+<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="va">name</span> <span class="op">=</span> <span class="st">&#39;OSC 52&#39;</span><span class="op">,</span></span>
4949+<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="va">copy</span> <span class="op">=</span> <span class="op">{</span></span>
5050+<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;+&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>copy<span class="op">(</span><span class="st">&#39;+&#39;</span><span class="op">),</span></span>
5151+<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;*&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>copy<span class="op">(</span><span class="st">&#39;*&#39;</span><span class="op">),</span></span>
5252+<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span>
5353+<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="va">paste</span> <span class="op">=</span> <span class="op">{</span></span>
5454+<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;+&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>paste<span class="op">(</span><span class="st">&#39;+&#39;</span><span class="op">),</span></span>
5555+<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;*&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>paste<span class="op">(</span><span class="st">&#39;*&#39;</span><span class="op">),</span></span>
5656+<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span>
5757+<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
5858+<p>If you are inside <code>nvim</code> inside <code>tmux</code> inside
5959+an <code>ssh</code> session inside <code>st</code>, you neeed all of the
6060+above tweaks. <code>nvim</code> will pass the contents around to
6161+<code>tmux</code>, which in turn will pass the contents to
6262+<code>st</code>, which should pass it to your system clipboard.</p></description>
6363+<link>https://peppe.rs/posts/OSC-52/</link>
6464+<pubDate>Wed, 27 Nov 2024 22:56:00 +0000</pubDate>
6565+<guid>https://peppe.rs/posts/OSC-52/</guid>
6666+</item>
6767+<item>
1568<title>Introducing Tablespoon</title>
1669<description><p><a href="https://git.peppe.rs/languages/tbsp">tbsp</a> (tree-based
1770source-processing language) is an awk-like language that operates on
+119
docs/posts/OSC-52/index.html
···11+<!DOCTYPE html>
22+<html lang="en">
33+ <head>
44+ <link rel="stylesheet" href="/style.css">
55+ <link rel="stylesheet" href="/syntax.css">
66+ <meta charset="UTF-8">
77+ <meta name="viewport" content="initial-scale=1">
88+ <meta content="#ffffff" name="theme-color">
99+ <meta name="HandheldFriendly" content="true">
1010+ <meta property="og:title" content="OSC-52">
1111+ <meta property="og:type" content="website">
1212+ <meta property="og:description" content="a static site {for, by, about} me ">
1313+ <meta property="og:url" content="https://peppe.rs">
1414+ <link rel="icon" type="image/x-icon" href="/favicon.png">
1515+ <title>OSC-52 · peppe.rs</title>
1616+ <body>
1717+ <div class="posts">
1818+ <div class="post">
1919+ <a href="/" class="post-end-link">Home</a>
2020+ <span>/</span>
2121+ <a href="/posts" class="post-end-link">Posts</a>
2222+ <span>/</span>
2323+ <a class="post-end-link">OSC-52</a>
2424+ <a class="stats post-end-link" href="https://git.peppe.rs/web/site/plain/posts/OSC-52.md
2525+">View Raw</a>
2626+ <div class="separator"></div>
2727+ <div class="date">
2828+ 28/11 — 2024
2929+ <div class="stats">
3030+ <span class="stats-number">
3131+ 26.37
3232+ </span>
3333+ <span class="stats-unit">cm</span>
3434+  
3535+ <span class="stats-number">
3636+ 1.9
3737+ </span>
3838+ <span class="stats-unit">min</span>
3939+ </div>
4040+ </div>
4141+ <h1>
4242+ OSC-52
4343+ </h1>
4444+ <div class="post-text">
4545+ <p>I use <code>ssh</code> a lot. Copying text from the remote machine to
4646+the host machine always sucked. But OSC-52 makes that easy.</p>
4747+<p>OSC-52 is an ANSI escape sequence to write text to the terminal
4848+emulator. The terminal emulator, if it understands what is going on,
4949+will in turn write this text to the system clipboard.</p>
5050+<p>What this means is some <code>printf</code> magic can send text to
5151+your clipboard. I store this one-liner in a script called
5252+<code>oclip</code>:</p>
5353+<div class="sourceCode" id="cb1"><pre
5454+class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">"\033]52;c;%s\007"</span> <span class="st">"</span><span class="va">$(</span><span class="fu">base64</span> <span class="op"><&</span><span class="dv">0</span><span class="va">)</span><span class="st">"</span></span></code></pre></div>
5555+<p>and I run it with:</p>
5656+<div class="sourceCode" id="cb2"><pre
5757+class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">remote</span> $ cat some_file.txt <span class="kw">|</span> <span class="ex">oclip</span></span>
5858+<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
5959+<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># some_file.txt's contents are now the host's clipboard</span></span></code></pre></div>
6060+<h3 id="the-catch">The catch</h3>
6161+<p>Your terminal emulator must support OSC-52, <code>alacritty</code>
6262+and <code>termux</code> seem to support this out of the box. In
6363+<code>st</code>, OSC-52 works with this change to
6464+<code>config.h</code>:</p>
6565+<pre><code>int allowwindowops = 1;</code></pre>
6666+<p>If you are using <code>tmux</code>, you need to flip this switch
6767+on:</p>
6868+<pre><code>set -s set-clipboard on</code></pre>
6969+<p>If you are inside <code>nvim</code>, it may work as expected as long
7070+as <code>$SSH_TTY</code> is set. I sometimes physically start a session,
7171+and <code>ssh</code> into the same session later from another machine,
7272+and <code>$SSH_TTY</code> remains unset, so I force OSC-52 in
7373+<code>nvim</code> at all times (see <a
7474+href="https://neovim.io/doc/user/provider.html#clipboard-osc52">nvimdoc</a>):</p>
7575+<div class="sourceCode" id="cb5"><pre
7676+class="sourceCode lua"><code class="sourceCode lua"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="va">vim</span><span class="op">.</span><span class="va">g</span><span class="op">.</span><span class="va">clipboard</span> <span class="op">=</span> <span class="op">{</span></span>
7777+<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="va">name</span> <span class="op">=</span> <span class="st">'OSC 52'</span><span class="op">,</span></span>
7878+<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="va">copy</span> <span class="op">=</span> <span class="op">{</span></span>
7979+<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">'+'</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">'vim.ui.clipboard.osc52'</span><span class="op">).</span>copy<span class="op">(</span><span class="st">'+'</span><span class="op">),</span></span>
8080+<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">'*'</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">'vim.ui.clipboard.osc52'</span><span class="op">).</span>copy<span class="op">(</span><span class="st">'*'</span><span class="op">),</span></span>
8181+<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span>
8282+<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="va">paste</span> <span class="op">=</span> <span class="op">{</span></span>
8383+<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">'+'</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">'vim.ui.clipboard.osc52'</span><span class="op">).</span>paste<span class="op">(</span><span class="st">'+'</span><span class="op">),</span></span>
8484+<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">'*'</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">'vim.ui.clipboard.osc52'</span><span class="op">).</span>paste<span class="op">(</span><span class="st">'*'</span><span class="op">),</span></span>
8585+<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span>
8686+<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
8787+<p>If you are inside <code>nvim</code> inside <code>tmux</code> inside
8888+an <code>ssh</code> session inside <code>st</code>, you neeed all of the
8989+above tweaks. <code>nvim</code> will pass the contents around to
9090+<code>tmux</code>, which in turn will pass the contents to
9191+<code>st</code>, which should pass it to your system clipboard.</p>
9292+9393+ </div>
9494+9595+ <div class="intro">
9696+ Hi.
9797+ <div class="hot-links">
9898+ <a href="/index.xml" class="feed-button">Subscribe</a>
9999+ </div>
100100+ <p>I'm Akshay, programmer and pixel-artist.
101101+ I write <a href="https://git.peppe.rs">open-source stuff</a>.
102102+ I also design fonts:
103103+ <a href="https://git.peppe.rs/fonts/scientifica/about">scientifica</a>,
104104+ <a href="https://git.peppe.rs/fonts/curie/about">curie</a>.
105105+ </p>
106106+ <p>Reach out at oppili@irc.rizon.net.</p>
107107+ </div>
108108+109109+ <a href="/" class="post-end-link">Home</a>
110110+ <span>/</span>
111111+ <a href="/posts" class="post-end-link">Posts</a>
112112+ <span>/</span>
113113+ <a class="post-end-link">OSC-52</a>
114114+ <a class="stats post-end-link" href="https://git.peppe.rs/web/site/plain/posts/OSC-52.md
115115+">View Raw</a>
116116+ </div>
117117+ </div>
118118+ </body>
119119+</html>
···11+I use `ssh` a lot. Copying text from the remote machine to
22+the host machine always sucked. But OSC-52 makes that easy.
33+44+OSC-52 is an ANSI escape sequence to write text to the
55+terminal emulator. The terminal emulator, if it understands
66+what is going on, will in turn write this text to the system
77+clipboard.
88+99+What this means is some `printf` magic can send text to your
1010+clipboard. I store this one-liner in a script called
1111+`oclip`:
1212+1313+```bash
1414+printf "\033]52;c;%s\007" "$(base64 <&0)"
1515+```
1616+1717+and I run it with:
1818+1919+```bash
2020+remote $ cat some_file.txt | oclip
2121+2222+# some_file.txt's contents are now the host's clipboard
2323+```
2424+2525+### The catch
2626+2727+Your terminal emulator must support OSC-52, `alacritty` and
2828+`termux` seem to support this out of the box. In `st`,
2929+OSC-52 works with this change to `config.h`:
3030+3131+```
3232+int allowwindowops = 1;
3333+```
3434+3535+If you are using `tmux`, you need to flip this switch on:
3636+3737+```
3838+set -s set-clipboard on
3939+```
4040+4141+If you are inside `nvim`, it may work as expected as long as
4242+`$SSH_TTY` is set. I sometimes physically start a session,
4343+and `ssh` into the same session later from another machine,
4444+and `$SSH_TTY` remains unset, so I force OSC-52 in `nvim` at
4545+all times (see
4646+[nvimdoc](https://neovim.io/doc/user/provider.html#clipboard-osc52)):
4747+4848+```lua
4949+vim.g.clipboard = {
5050+ name = 'OSC 52',
5151+ copy = {
5252+ ['+'] = require('vim.ui.clipboard.osc52').copy('+'),
5353+ ['*'] = require('vim.ui.clipboard.osc52').copy('*'),
5454+ },
5555+ paste = {
5656+ ['+'] = require('vim.ui.clipboard.osc52').paste('+'),
5757+ ['*'] = require('vim.ui.clipboard.osc52').paste('*'),
5858+ },
5959+}
6060+```
6161+6262+If you are inside `nvim` inside `tmux` inside an `ssh`
6363+session inside `st`, you neeed all of the above tweaks.
6464+`nvim` will pass the contents around to `tmux`, which in
6565+turn will pass the contents to `st`, which should pass it to
6666+your system clipboard.