aboutsummaryrefslogtreecommitdiff
path: root/build/blog/2015-09-21-zsh-51-and-bracketed-paste.html
blob: 52a7b006ac9e9958809425df5c8c86def6911ad5 (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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="pandoc" name="generator"/>
<meta content="Zhiming Wang" name="author"/>
<meta content="2015-09-21T14:40:36-07:00" name="date"/>
<title>Zsh 5.1 and bracketed paste</title>
<link href="/img/apple-touch-icon-152.png" rel="apple-touch-icon-precomposed"/>
<meta content="#FFFFFF" name="msapplication-TileColor"/>
<meta content="/img/favicon-144.png" name="msapplication-TileImage"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<link href="/css/normalize.min.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/theme.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/highlight.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="archival-notice">This blog has been archived.<br/>Visit my home page at <a href="https://zhimingwang.org">zhimingwang.org</a>.</div>
<nav class="nav">
<a class="nav-icon" href="/" title="Home"><!--blog icon--></a>
<a class="nav-title" href="/"><!--blog title--></a>
<a class="nav-author" href="https://github.com/zmwangx" target="_blank"><!--blog author--></a>
</nav>
<article class="content">
<header class="article-header">
<h1 class="article-title">Zsh 5.1 and bracketed paste</h1>
<div class="article-metadata">
<time class="article-timestamp" datetime="2015-09-21T14:40:36-07:00">September 21, 2015</time>
</div>
</header>
<p><strong>TL;DR.</strong> Jump to <a href="#code">code</a>.</p>
<hr/>
<p>In short, Zsh 5.1 introduced bracketed paste mode<a class="footnoteRef" href="#fn1" id="fnref1"><sup>1</sup></a> and turned it on by default (as it seems to me<a class="footnoteRef" href="#fn2" id="fnref2"><sup>2</sup></a>). It is nice in certain ways — I appreciate the change, yet I was bitten nevertheless. In at least two ways:</p>
<ol style="list-style-type: decimal">
<li><p>Most annoyingly, <code>url-quote-magic</code> doesn't work anymore when pasting URLs, so for example if I paste</p>
<pre><code>https://www.google.com/search?q=zsh</code></pre>
<p>without typing in a single or double quote first, the <code>?</code> and <code>=</code> won't be backslash-quoted by default, which causes an error when passed unnoticed (out of habit).</p></li>
<li><p>The Emacs shell<a class="footnoteRef" href="#fn3" id="fnref3"><sup>3</sup></a> is littered with <code>^[[?2004h</code> and <code>^[[?2004l</code> around every prompt.</p></li>
</ol>
<p>The solution? Zsh now also ships with <a href="https://github.com/zsh-users/zsh/blob/master/Functions/Zle/bracketed-paste-magic"><code>bracketed-paste-magic</code></a> that resolves exactly breakage #1 (and a bit more); to quote comments from the linked source file:</p>
<blockquote>
<p>Starting with zsh-5.1, ZLE began to recognize the "bracketed paste" capability of terminal emulators, that is, the sequences <code>$'\e[200~'</code> to start a paste and <code>$'\e[201~'</code> to indicate the end of the pasted text. Pastes are handled by the bracketed-paste widget and insert literally into the editor buffer rather than being interpreted as keystrokes.</p>
<p>This disables some common usages where the self-insert widget has been replaced in order to accomplish some extra processing. An example is the contributed url-quote-magic widget. The bracketed-paste-magic widget replaces bracketed-paste with a wrapper that re-enables these self-insert actions, and other actions as selected by the zstyles described below.</p>
</blockquote>
<p>And to resolve breakage #2, just disable bracketed paste altogether for dumb terms.</p>
<p id="code">
Putting it together:
</p>
<div class="sourceCode"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span class="co"># turn off ZLE bracketed paste in dumb term</span>
<span class="co"># otherwise turn on ZLE bracketed-paste-magic</span>
<span class="kw">if [[</span> <span class="ot">$TERM</span> <span class="ot">==</span> dumb<span class="kw"> ]]</span>; <span class="kw">then</span>
    <span class="kw">unset</span> zle_bracketed_paste
<span class="kw">else</span>
    <span class="kw">autoload</span> -Uz bracketed-paste-magic
    <span class="kw">zle</span> -N bracketed-paste bracketed-paste-magic
<span class="kw">fi</span></code></pre></div>
<hr/>
<p><span id="update"><strong>09/22/2015 update.</strong></span> I only read <code>NEWS</code> and not <code>README</code>, so I missed out on a very clear announcement of the <a href="https://github.com/zsh-users/zsh/blob/68405f31a043bdd5bf338eb06688ed3e1f740937/README#L38-L45">bracketed paste incompatibitilies (between 5.0.8 and 5.1)</a>:</p>
<blockquote>
<p>The default behaviour when text is pasted into an X Windows terminal has changed significantly (unless you are using a very old terminal emulator that doesn't support this mode). Now, the new "bracketed paste mode" treats all the pasted text as literal characters. This means, in particular, that a newline is simply inserted as a visible newline; you need to hit Return on the keyboard to execute the pasted text in one go. See the description of <code>zle_bracketed_paste</code> in the <code>zshparams</code> manual for more. "<code>unset zle_bracketed_paste</code>" restores the previous behaviour.</p>
</blockquote>
<div class="footnotes">
<hr/>
<ol>
<li id="fn1"><p>Bracketed paste mode is a safeguard against inadvertent interpretation of pasted text, e.g., newline being treated at <code>accept-line</code> in Zsh. You may read more about it <a href="https://cirw.in/blog/bracketed-paste">in this blog post</a>, which is somewhat outdated yet still informational.<a class="footnotes-backlink" href="#fnref1">↩︎</a></p></li>
<li id="fn2"><p>Indeed it is. See <a href="#update">update</a> with more accurate info from official source.<a class="footnotes-backlink" href="#fnref2">↩︎</a></p></li>
<li id="fn3"><p>I seldom use this dumb (literally) thing, but when I do I expect it to work ungarbled, naturally.<a class="footnotes-backlink" href="#fnref3">↩︎</a></p></li>
</ol>
</div>
</article>
<hr class="content-separator"/>
<footer class="footer">
<span class="rfooter">
<a class="rss-icon" href="/rss.xml" target="_blank" title="RSS feed"><!--RSS feed icon--></a><a class="atom-icon" href="/atom.xml" target="_blank" title="Atom feed"><!--Atom feed icon--></a><a class="cc-icon" href="https://creativecommons.org/licenses/by/4.0/" target="_blank" title="Released under the Creative Commons Attribution 4.0 International license."><!--CC icon--></a>
<a href="https://github.com/zmwangx" target="_blank">Zhiming Wang</a>
</span>
</footer>
</body>
</html>