aboutsummaryrefslogtreecommitdiff
path: root/docs/posts/OSC-52/index.html
blob: 6c87fef0474db0cb855b40376679a2879f26b4c5 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="/style.css">
    <link rel="stylesheet" href="/syntax.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1">
    <meta content="#ffffff" name="theme-color">
    <meta name="HandheldFriendly" content="true">
    <meta property="og:title" content="OSC-52">
    <meta property="og:type" content="website">
    <meta property="og:description" content="a static site {for, by, about} me ">
    <meta property="og:url" content="https://peppe.rs">
    <link rel="icon" type="image/x-icon" href="/favicon.png">
    <title>OSC-52 · peppe.rs</title>
    <body>
      <div class="posts">
        <div class="post">
          <a href="/" class="post-end-link">Home</a>
          <span>/</span>
          <a href="/posts" class="post-end-link">Posts</a>
          <span>/</span>
          <a class="post-end-link">OSC-52</a>
          <a class="stats post-end-link" href="https://git.peppe.rs/web/site/plain/posts/OSC-52.md
">View Raw</a>
          <div class="separator"></div>
          <div class="date">
            28/11 — 2024
            <div class="stats">
              <span class="stats-number">
                26.37
              </span>
              <span class="stats-unit">cm</span>
              &nbsp
              <span class="stats-number">
                1.9
              </span>
              <span class="stats-unit">min</span>
            </div>
          </div>
          <h1>
            OSC-52
          </h1>
          <div class="post-text">
            <p>I use <code>ssh</code> a lot. Copying text from the remote machine to
the host machine always sucked. But OSC-52 makes that easy.</p>
<p>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.</p>
<p>What this means is some <code>printf</code> magic can send text to
your clipboard. I store this one-liner in a script called
<code>oclip</code>:</p>
<div class="sourceCode" id="cb1"><pre
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>
<p>and I run it with:</p>
<div class="sourceCode" id="cb2"><pre
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>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<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>
<h3 id="the-catch">The catch</h3>
<p>Your terminal emulator must support OSC-52, <code>alacritty</code>
and <code>termux</code> seem to support this out of the box. In
<code>st</code>, OSC-52 works with this change to
<code>config.h</code>:</p>
<pre><code>int allowwindowops = 1;</code></pre>
<p>If you are using <code>tmux</code>, you need to flip this switch
on:</p>
<pre><code>set -s set-clipboard on</code></pre>
<p>If you are inside <code>nvim</code>, it may work as expected as long
as <code>$SSH_TTY</code> is set. I sometimes physically start a session,
and <code>ssh</code> into the same session later from another machine,
and <code>$SSH_TTY</code> remains unset, so I force OSC-52 in
<code>nvim</code> at all times (see <a
href="https://neovim.io/doc/user/provider.html#clipboard-osc52">nvimdoc</a>):</p>
<div class="sourceCode" id="cb5"><pre
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>
<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>
<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>
<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>
<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>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>  <span class="op">},</span></span>
<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>
<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>
<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>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a>  <span class="op">},</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>If you are inside <code>nvim</code> inside <code>tmux</code> inside
an <code>ssh</code> session inside <code>st</code>, you neeed all of the
above tweaks. <code>nvim</code> will pass the contents around to
<code>tmux</code>, which in turn will pass the contents to
<code>st</code>, which should pass it to your system clipboard.</p>

          </div>
          
    <div class="intro">
        Hi. 
        <div class="hot-links">
            <a href="/index.xml" class="feed-button">Subscribe</a>
        </div>
        <p>I'm Akshay, programmer and pixel-artist.
        I write <a href="https://git.peppe.rs">open-source stuff</a>. 
        I also design fonts: 
        <a href="https://git.peppe.rs/fonts/scientifica/about">scientifica</a>, 
        <a href="https://git.peppe.rs/fonts/curie/about">curie</a>.
        </p>
        <p>Reach out at [email protected].</p>
    </div>
    
          <a href="/" class="post-end-link">Home</a>
          <span>/</span>
          <a href="/posts" class="post-end-link">Posts</a>
          <span>/</span>
          <a class="post-end-link">OSC-52</a>
          <a class="stats post-end-link" href="https://git.peppe.rs/web/site/plain/posts/OSC-52.md
">View Raw</a>
        </div>
      </div>
    </body>
</html>