File: function.html

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (325 lines) | stat: -rw-r--r-- 27,041 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>function - create a function &#8212; fish-shell 4.2.1 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4da8bbd6" />
    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=f89b4716" />
    <script src="../_static/documentation_options.js?v=6fb65176"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="functions - print or erase functions" href="functions.html" />
    <link rel="prev" title="funcsave - save the definition of a function to the user’s autoload directory" href="funcsave.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/fish.png" />
     

  </head><body><div id="fmain">  
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
    <li><img src="../_static/fish.png" alt=""
             style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://fishshell.com/">fish-shell</a> &#187;</li>
    
    <a href="../index.html">fish-shell 4.2.1 documentation</a> &#187;
    

          <li class="nav-item nav-item-1"><a href="../commands.html" accesskey="U">Commands</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">function - create a function</a></li>
    <li class="right">
        

    <div class="inline-search" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    </li>
    
    <div id="old-docs-notice" style="display: none">
        This documents an old version of fish.
        <a href="../../current/">See the latest release.</a>
    </div>

      </ul>
    </div>    

    <div class="document">
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
<div>
<h3><a href="../index.html">Documents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../interactive.html">Interactive use</a></li>
<li class="toctree-l1"><a class="reference internal" href="../language.html">The fish language</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../commands.html">Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fish_for_bash_users.html">Fish for bash users</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../completions.html">Writing your own completions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../prompt.html">Writing your own prompt</a></li>
<li class="toctree-l1"><a class="reference internal" href="../design.html">Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../relnotes.html">Release notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../terminal-compatibility.html">Terminal Compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">Contributing To Fish</a></li>
<li class="toctree-l1"><a class="reference internal" href="../license.html">License</a></li>
</ul>

</div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<div>
<h4><a href="../index.html">Sections</a></h4>
<ul>
<li><a class="reference internal" href="#">function - create a function</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#description">Description</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#notes">Notes</a></li>
<li><a class="reference internal" href="#see-more">See more</a></li>
</ul>
</li>
</ul>

</div>
        </div>
      </div>
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="function-create-a-function">
<h1>function - create a function<a class="headerlink" href="#function-create-a-function" title="Link to this heading">¶</a></h1>
<section id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-synopsis notranslate"><div class="highlight"><pre><span></span><span class="nf">function</span><span class="w"> </span><span class="no">NAME</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">OPTIONS</span><span class="g g-Other">]</span><span class="p">;</span><span class="w"> </span><span class="nf">BODY</span><span class="p">;</span><span class="w"> </span><span class="nf">end</span>
</pre></div>
</div>
</section>
<section id="description">
<h2>Description<a class="headerlink" href="#description" title="Link to this heading">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">function</span></code> creates a new function <em>NAME</em> with the body <em>BODY</em>.</p>
<p>A function is a list of commands that will be executed when the name of the function is given as a command.</p>
<p>The following options are available:</p>
<dl>
<dt><strong>-a</strong> <em>NAMES</em> or <strong>--argument-names</strong> <em>NAMES</em></dt><dd><p>Assigns the value of successive command-line arguments to the names given in <em>NAMES</em> (separated by spaces). These are the same arguments given in <span class="target" id="index-0"></span><a class="reference internal" href="../language.html#envvar-argv"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">argv</span></code></a>, and are still available there (unless <code class="docutils literal notranslate"><span class="pre">--inherit-variable</span> <span class="pre">argv</span></code> was used or one of the given <em>NAMES</em> is <code class="docutils literal notranslate"><span class="pre">argv</span></code>). See also <a class="reference internal" href="../language.html#variables-argv"><span class="std std-ref">Argument Handling</span></a>.</p>
</dd>
<dt><strong>-d</strong> <em>DESCRIPTION</em> or <strong>--description</strong> <em>DESCRIPTION</em></dt><dd><p>A description of what the function does, suitable as a completion description.</p>
</dd>
<dt><strong>-w</strong> <em>WRAPPED_COMMAND</em> or <strong>--wraps</strong> <em>WRAPPED_COMMAND</em></dt><dd><p>Inherit completions from the given <em>WRAPPED_COMMAND</em>.
This is used to say that this function completes like that command,
for example if you’re creating an alias.
See the documentation for <a class="reference internal" href="complete.html"><span class="doc">complete</span></a> for more information.
If the wrapped command is the same as the function name, this will be ignored.</p>
</dd>
<dt><strong>-e</strong> <em>EVENT_NAME</em> or <strong>--on-event</strong> <em>EVENT_NAME</em></dt><dd><p>Run this function when the specified named event is emitted. Fish internally generates named events, for example, when showing the prompt. Custom events can be emitted using the <a class="reference internal" href="emit.html"><span class="doc">emit</span></a> command.</p>
</dd>
<dt><strong>-v</strong> <em>VARIABLE_NAME</em> or <strong>--on-variable</strong> <em>VARIABLE_NAME</em></dt><dd><p>Run this function when the variable <em>VARIABLE_NAME</em> changes value. Note that <strong class="program">fish</strong> makes no guarantees on any particular timing or even that the function will be run for every single <code class="docutils literal notranslate"><span class="pre">set</span></code>. Rather it will be run when the variable has been set at least once, possibly skipping some values or being run when the variable has been set to the same value (except for universal variables set in other shells - only changes in the value will be picked up for those).</p>
</dd>
<dt><strong>-j</strong> <em>PID</em> or <strong>--on-job-exit</strong> <em>PID</em></dt><dd><p>Run this function when the job containing a child process with the given process ID <em>PID</em> exits. Instead of a PID, the string ‘caller’ can be specified. This is only allowed when in a command substitution, and will result in the handler being triggered by the exit of the job which created this command substitution.
This will not trigger for <a class="reference internal" href="disown.html"><span class="doc">disowned</span></a> jobs.</p>
</dd>
<dt><strong>-p</strong> <em>PID</em> or <strong>--on-process-exit</strong> <em>PID</em></dt><dd><p>Run this function when the fish child process with process ID PID exits. Instead of a PID, for backward compatibility, “<code class="docutils literal notranslate"><span class="pre">%self</span></code>” can be specified as an alias for <code class="docutils literal notranslate"><span class="pre">$fish_pid</span></code>, and the function will be run when the current fish instance exits.
This will not trigger for <a class="reference internal" href="disown.html"><span class="doc">disowned</span></a> jobs.</p>
</dd>
<dt><strong>-s</strong> <em>SIGSPEC</em> or <strong>--on-signal</strong> <em>SIGSPEC</em></dt><dd><p>Run this function when the signal <code class="docutils literal notranslate"><span class="pre">SIGSPEC</span></code> is delivered. <code class="docutils literal notranslate"><span class="pre">SIGSPEC</span></code> can be a signal number, or the signal name, such as <code class="docutils literal notranslate"><span class="pre">SIGHUP</span></code> (or just <code class="docutils literal notranslate"><span class="pre">HUP</span></code>). Note that the signal must have been delivered to <strong class="program">fish</strong>; for example, <kbd class="kbd docutils literal notranslate">ctrl</kbd>-<kbd class="kbd docutils literal notranslate">c</kbd> sends <code class="docutils literal notranslate"><span class="pre">SIGINT</span></code> to the foreground process group, which will not be <strong class="program">fish</strong> if you are running another command at the time. Observing a signal will prevent fish from exiting in response to that signal.</p>
</dd>
<dt><strong>-S</strong> or <strong>--no-scope-shadowing</strong></dt><dd><p>Allows the function to access the variables of calling functions. Normally, any variables inside the function that have the same name as variables from the calling function are “shadowed”, and their contents are independent of the calling function.</p>
<p>It’s important to note that this does not capture referenced variables or the scope at the time of function declaration! At this time, fish does not have any concept of closures, and variable lifetimes are never extended. In other words, by using <strong>--no-scope-shadowing</strong> the scope of the function each time it is run is shared with the scope it was <em>called</em> from rather than the scope it was <em>defined</em> in.</p>
</dd>
<dt><strong>-V</strong> or <strong>--inherit-variable NAME</strong></dt><dd><p>Snapshots the value of the variable <code class="docutils literal notranslate"><span class="pre">NAME</span></code> and defines a local variable with that same name and value when the function is defined. This is similar to a closure in other languages like Python but a bit different. Note the word “snapshot” in the first sentence. If you change the value of the variable after defining the function, even if you do so in the same scope (typically another function) the new value will not be used by the function you just created using this option. See the <code class="docutils literal notranslate"><span class="pre">function</span> <span class="pre">notify</span></code> example below for how this might be used.</p>
</dd>
</dl>
<p>The event handler switches (<code class="docutils literal notranslate"><span class="pre">on-event</span></code>, <code class="docutils literal notranslate"><span class="pre">on-variable</span></code>, <code class="docutils literal notranslate"><span class="pre">on-job-exit</span></code>, <code class="docutils literal notranslate"><span class="pre">on-process-exit</span></code> and <code class="docutils literal notranslate"><span class="pre">on-signal</span></code>) cause a function to run automatically at specific events. New named events for <code class="docutils literal notranslate"><span class="pre">--on-event</span></code> can be fired using the <a class="reference internal" href="emit.html"><span class="doc">emit</span></a> builtin. Fish already generates a few events, see <a class="reference internal" href="../language.html#event"><span class="std std-ref">Event handlers</span></a> for more.</p>
<p>Functions names cannot be reserved words. These are elements of fish syntax or builtin commands which are essential for the operations of the shell. Current reserved words are <code class="docutils literal notranslate"><span class="pre">[</span></code>, <code class="docutils literal notranslate"><span class="pre">_</span></code>, <code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">argparse</span></code>, <code class="docutils literal notranslate"><span class="pre">begin</span></code>, <code class="docutils literal notranslate"><span class="pre">break</span></code>, <code class="docutils literal notranslate"><span class="pre">builtin</span></code>, <code class="docutils literal notranslate"><span class="pre">case</span></code>, <code class="docutils literal notranslate"><span class="pre">command</span></code>, <code class="docutils literal notranslate"><span class="pre">continue</span></code>, <code class="docutils literal notranslate"><span class="pre">else</span></code>, <code class="docutils literal notranslate"><span class="pre">end</span></code>, <code class="docutils literal notranslate"><span class="pre">eval</span></code>, <code class="docutils literal notranslate"><span class="pre">exec</span></code>, <code class="docutils literal notranslate"><span class="pre">for</span></code>, <code class="docutils literal notranslate"><span class="pre">function</span></code>, <code class="docutils literal notranslate"><span class="pre">if</span></code>, <code class="docutils literal notranslate"><span class="pre">not</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code>, <code class="docutils literal notranslate"><span class="pre">read</span></code>, <code class="docutils literal notranslate"><span class="pre">return</span></code>, <code class="docutils literal notranslate"><span class="pre">set</span></code>, <code class="docutils literal notranslate"><span class="pre">status</span></code>, <code class="docutils literal notranslate"><span class="pre">string</span></code>, <code class="docutils literal notranslate"><span class="pre">switch</span></code>, <code class="docutils literal notranslate"><span class="pre">test</span></code>, <code class="docutils literal notranslate"><span class="pre">time</span></code>, and <code class="docutils literal notranslate"><span class="pre">while</span></code>.</p>
</section>
<section id="example">
<h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">function</span><span class="w"> </span><span class="no">ll</span>
<span class="w">    </span><span class="nf">ls</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="o">$argv</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>will run the <code class="docutils literal notranslate"><span class="pre">ls</span></code> command, using the <code class="docutils literal notranslate"><span class="pre">-l</span></code> option, while passing on any additional files and switches to <code class="docutils literal notranslate"><span class="pre">ls</span></code>.</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">function</span><span class="w"> </span><span class="no">debug</span><span class="w"> </span><span class="no">-a</span><span class="w"> </span><span class="no">name</span><span class="w"> </span><span class="no">val</span>
<span class="w">    </span><span class="nf">echo</span><span class="w"> </span><span class="no">[DEBUG]</span><span class="w"> </span><span class="o">$name</span><span class="no">:</span><span class="w"> </span><span class="o">$val</span><span class="w"> </span><span class="p">&gt;&amp;2</span>
<span class="nf">end</span>

<span class="nf">set</span><span class="w"> </span><span class="no">foo</span><span class="w"> </span><span class="no">bar</span>
<span class="nf">debug</span><span class="w"> </span><span class="no">foo</span><span class="w"> </span><span class="no">bar</span>
<span class="c"># prints: [DEBUG] foo: bar</span>

<span class="c"># OR</span>

<span class="nf">function</span><span class="w"> </span><span class="no">debug2</span><span class="w"> </span><span class="no">-a</span><span class="w"> </span><span class="no">var</span>
<span class="w">    </span><span class="nf">echo</span><span class="w"> </span><span class="no">[DEBUG]</span><span class="w"> </span><span class="o">$var</span><span class="no">:</span><span class="w"> </span><span class="o">$$var</span><span class="w"> </span><span class="p">&gt;&amp;2</span>
<span class="nf">end</span>

<span class="nf">set</span><span class="w"> </span><span class="no">foo</span><span class="w"> </span><span class="no">bar</span>
<span class="nf">debug2</span><span class="w"> </span><span class="no">foo</span>
<span class="c"># prints: [DEBUG] foo: bar</span>
</pre></div>
</div>
<p>will create a <code class="docutils literal notranslate"><span class="pre">debug</span></code> command to print chosen variables to <cite>stderr</cite>.</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">function</span><span class="w"> </span><span class="no">mkdir</span><span class="w"> </span><span class="no">-d</span><span class="w"> </span><span class="s2">&quot;Create a directory and set CWD&quot;</span>
<span class="w">    </span><span class="nf">command</span><span class="w"> </span><span class="nf">mkdir</span><span class="w"> </span><span class="o">$argv</span>
<span class="w">    </span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="o">$status</span><span class="w"> </span><span class="no">=</span><span class="w"> </span><span class="no">0</span>
<span class="w">        </span><span class="nf">switch</span><span class="w"> </span><span class="o">$argv[(</span><span class="nf">count</span><span class="w"> </span><span class="o">$argv)]</span>
<span class="w">            </span><span class="nf">case</span><span class="w"> </span><span class="s1">&#39;-*&#39;</span>

<span class="w">            </span><span class="nf">case</span><span class="w"> </span><span class="s1">&#39;*&#39;</span>
<span class="w">                </span><span class="nf">cd</span><span class="w"> </span><span class="o">$argv[(</span><span class="nf">count</span><span class="w"> </span><span class="o">$argv)]</span>
<span class="w">                </span><span class="nf">return</span>
<span class="w">        </span><span class="nf">end</span>
<span class="w">    </span><span class="nf">end</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>This will run the <code class="docutils literal notranslate"><span class="pre">mkdir</span></code> command, and if it is successful, change the current working directory to the one just created.</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">function</span><span class="w"> </span><span class="no">notify</span>
<span class="w">    </span><span class="nf">set</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">job</span><span class="w"> </span><span class="o">(</span><span class="nf">jobs</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">-g</span><span class="o">)</span>
<span class="w">    </span><span class="o">or</span><span class="w"> </span><span class="nf">begin</span><span class="p">;</span><span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="s2">&quot;There are no jobs&quot;</span><span class="w"> </span><span class="p">&gt;&amp;2;</span><span class="w"> </span><span class="nf">return</span><span class="w"> </span><span class="no">1</span><span class="p">;</span><span class="w"> </span><span class="nf">end</span>

<span class="w">    </span><span class="nf">function</span><span class="w"> </span><span class="no">_notify_job_</span><span class="o">$job</span><span class="w"> </span><span class="no">--on-job-exit</span><span class="w"> </span><span class="o">$job</span><span class="w"> </span><span class="no">--inherit-variable</span><span class="w"> </span><span class="no">job</span>
<span class="w">        </span><span class="nf">echo</span><span class="w"> </span><span class="no">-n</span><span class="w"> </span><span class="se">\a</span><span class="w"> </span><span class="c"># beep</span>
<span class="w">        </span><span class="nf">functions</span><span class="w"> </span><span class="no">-e</span><span class="w"> </span><span class="no">_notify_job_</span><span class="o">$job</span>
<span class="w">    </span><span class="nf">end</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>This will beep when the most recent job completes.</p>
</section>
<section id="notes">
<h2>Notes<a class="headerlink" href="#notes" title="Link to this heading">¶</a></h2>
<p>Events are only received from the current fish process as there is no way to send events from one fish process to another.</p>
</section>
<section id="see-more">
<h2>See more<a class="headerlink" href="#see-more" title="Link to this heading">¶</a></h2>
<p>For more explanation of how functions fit into fish, see <a class="reference internal" href="../language.html#syntax-function"><span class="std std-ref">Functions</span></a>.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div> 
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
    <li><img src="../_static/fish.png" alt=""
             style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://fishshell.com/">fish-shell</a> &#187;</li>
    
    <a href="../index.html">fish-shell 4.2.1 documentation</a> &#187;
    

          <li class="nav-item nav-item-1"><a href="../commands.html" >Commands</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">function - create a function</a></li>
    <li class="right">
        

    <div class="inline-search" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    </li>
    
    <div id="old-docs-notice" style="display: none">
        This documents an old version of fish.
        <a href="../../current/">See the latest release.</a>
    </div>

      </ul>
    </div>  
    <div class="footer">
    &copy; Copyright fish-shell developers.
    <br />
    <a href="https://github.com/fish-shell/fish-shell/issues">Found a bug</a>?
    <br />

    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
    </div>
</div>

<script type="text/javascript">
 FISH_DOCS_VERSION = "4.2";

 function copy_to_clipboard(it) {
     // Find the pre tag we're interested in.
     var pre = it.target;
     while (pre.tagName != "PRE") pre = pre.parentNode;
     var txt = "";
     // Cheesy: If we have a prompt,
     // we only copy prompted lines,
     // by splitting and matching and stuff
     if (pre.querySelector('span.gp')) {
         var texts= [];
         for (var line of pre.innerText.split('\n')) {
             if (line.match(/^>_?.*/)) {
                 texts.push(line.replace(/^>_?/, ""));
             }
         }
         txt = texts.join("\n");
     } else {
         // Even cheesier: If we don't have a prompt, we remove the button text from the end.
         var txt = pre.innerText.substring(0, pre.innerText.length - it.target.innerText.length).trim();
     }

     navigator.clipboard.writeText(txt).then(function() {
         // Success - set the text to indicate it,
         // then set it back after 2 seconds.
         var span = pre.querySelector("button span");
         if (span) {
             var oldText = span.innerText;
             span.innerText = "COPIED!";
             setTimeout(function() {
                 span.innerText = oldText;
             }, 2000);
         }
     }, function() {
     });
 }

  (function () {
      // Add copy buttons to all the codeblocks.
      var codeblocks = document.querySelectorAll('div > pre');

      var button = document.createElement('button');
      var span = document.createElement('span');
      span.innerText = "COPY";
      button.appendChild(span);

      for (var i of codeblocks) {
          var newButton = button.cloneNode(true);
          newButton.addEventListener('click', copy_to_clipboard);
          i.appendChild(newButton);
      }
  })();
</script>

  </body>
</html>