Files
hpr_website/www/eps/hpr2505/hpr2505_full_shownotes.html

235 lines
16 KiB
HTML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Dave Morriss">
<title>The power of GNU Readline - part 3 (HPR Show 2505)</title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://hackerpublicradio.org/css/hpr.css">
</head>
<body id="home">
<div id="container" class="shadow">
<header>
<h1 class="title">The power of GNU Readline - part 3 (HPR Show 2505)</h1>
<h2 class="author">Dave Morriss</h2>
<hr/>
</header>
<main id="maincontent">
<article>
<header>
<h1>Table of Contents</h1>
<nav id="TOC">
<ul>
<li><a href="#terminology">Terminology</a></li>
<li><a href="#readline-arguments">Readline Arguments</a></li>
<li><a href="#reminder">Reminder</a></li>
<li><a href="#more-character-and-word-manipulations-and-others">More character and word manipulations (and others)</a><ul>
<li><a href="#commenting-out-a-line">Commenting out a line</a><ul>
<li><a href="#example">Example</a></li>
</ul></li>
<li><a href="#transpose-characters">Transpose characters</a><ul>
<li><a href="#example-1">Example</a></li>
</ul></li>
<li><a href="#transpose-words">Transpose words</a><ul>
<li><a href="#example-2">Example</a></li>
</ul></li>
<li><a href="#change-the-case-of-words">Change the case of words</a><ul>
<li><a href="#examples">Examples</a></li>
</ul></li>
<li><a href="#revert-the-line">Revert the line</a><ul>
<li><a href="#example-3">Example</a></li>
</ul></li>
</ul></li>
<li><a href="#links">Links</a></li>
</ul>
</nav>
</header>
<h2 id="terminology">Terminology</h2>
<p>The <a href="https://tiswww.cwru.edu/php/chet/readline/rluserman.html" title="GNU Readline Manual"><em>GNU Readline</em> manual</a> uses the terms <em>point</em>, <em>mark</em> and <em>region</em>, which need definition. If you follow some of the links to this manual in this series you will encounter these terms, and I think they can be confusing.</p>
<dl>
<dt><strong>point</strong></dt>
<dd>The current cursor position (we have simply referred to it as the <em>cursor position</em> so far in this series). Also called the <em>insertion point</em>.
</dd>
<dt><strong>mark</strong></dt>
<dd>A cursor position saved by the <em>set-mark</em> command (well look at this in a forthcoming episode)
</dd>
<dt><strong>region</strong></dt>
<dd>The text between <em>point</em> and <em>mark</em> (also for a future episode)
</dd>
</dl>
<p>In this series I will try not to use these terms without an explanation or reminder of what they mean. I will be looking at these and commands that affect them more in later episodes.</p>
<h2 id="readline-arguments">Readline Arguments</h2>
<p>Readline commands (which are what are being invoked by the key sequences we have seen so far) can take numeric arguments. Sometimes the argument acts as a repeat count, other times it is the sign of the argument that is significant.</p>
<p>The argument itself begins with the <code>Meta</code> key, pressed in conjunction with a digit. If a multi-digit number is required this followed by further digits without the <code>Meta</code> key. The first “digit” can be a minus sign (<code>-</code>) if the numeric argument is to be negative.</p>
<p>For example, to repeat the <code>C-d</code> <small>(<code>Control-d</code>)</small> command 10 times type: <code>M-1</code> <code>0</code> <code>C-d</code> <small>(<code>Meta-1</code> <code>0</code> <code>Control-d</code>)</small>. This will delete forward 10 characters.</p>
<p>A negative argument reverses the effect of the command, so <code>M--</code> <code>C-k</code> <small>(<code>Meta--</code> <code>Control-k</code>)</small>, instead of deleting characters forward to the end of the line deletes them backwards to the start of the line.</p>
<h2 id="reminder">Reminder</h2>
<p>Some of the sequences we are looking at in this series can be intercepted and interpreted by:</p>
<ol type="1">
<li>The terminal</li>
<li>The desktop environment</li>
</ol>
<p>For example, I have been testing the key sequences used in these episodes using the <em>Terminator</em> terminal emulator on <em>XFCE</em> and I have found <code>M-l</code> (see later) was interpreted by <em>Terminator</em> and could not be typed.</p>
<p>Remember that all meta key sequences can be entered as <code>Esc</code> <em>key</em> press the <code>Esc</code> key then press the second key (<code>l</code> in this particular instance).</p>
<h2 id="more-character-and-word-manipulations-and-others">More character and word manipulations (and others)</h2>
<p>Lets get into some more Readline key sequences.</p>
<h3 id="commenting-out-a-line">Commenting out a line</h3>
<p>This is something I often do. Im typing a complex command and I want to save what Im doing and check something, or answer the phone perhaps. I used to hit the <strong>Home</strong> key or <code>C-a</code> and prefix the line with a <code>#</code> comment character then hit <code>Return</code>. The line is in the history and can be recalled, continued and executed after the comment has been removed.</p>
<p>There are Readline sequences that can help:</p>
<dl>
<dt><strong><code>M-#</code></strong> <small>(<code>Meta-#</code>)</small></dt>
<dd>Add a comment symbol to the start of the line and issue a <code>Return</code>.
</dd>
<dt><strong><code>M-1M-#</code></strong> <small>(<code>Meta-1</code> <code>Meta-#</code>)</small></dt>
<dd>If the current line begins with a comment remove it and execute the line with a <code>Return</code>. (If it doesnt begin with <code>#</code> then add one in the same way as <code>M-#</code>)
</dd>
</dl>
<p>The second sequence is actually <code>M-#</code> with an argument and any argument will have the same effect. So <code>M-0M-#</code> would also remove the comment and enter the command. This sequence is actually a “toggle” which adds a comment if there isnt one and removes it if there is.</p>
<p>The comment character can be changed (which well discuss in a later episode when we look at the Readline configuration file), which can be of relevance if Readline is being used in an application where a different comment character is required.</p>
<h4 id="example">Example</h4>
<ul>
<li>Type the Bash command: <code>echo &quot;Hello&quot;</code> but dont press <code>Return</code>.</li>
<li>Type <code>M-#</code>. The command turns into <code>#echo &quot;Hello&quot;</code> and the line is accepted as if <code>Return</code> had been pressed.</li>
<li>Recall the line with the up arrow key.</li>
<li>Type <code>M-1</code> <code>M-#</code>; the comment character is removed and the line accepted.</li>
</ul>
<h3 id="transpose-characters">Transpose characters</h3>
<p>Im a bad typist. I <em>so</em> often type words like <em>directroy</em> with transposed letters. However, Readline offers a facility to correct such errors:</p>
<dl>
<dt><strong><code>C-t</code></strong> <small>(<code>Control-t</code>)</small></dt>
<dd>Transpose characters. Swap the character before the cursor (<em>point</em>) with that under the cursor, then move the cursor to the right.
</dd>
</dl>
<h4 id="example-1">Example</h4>
<p>After typing the word incorrectly position the cursor as shown:</p>
<pre><code>directroy
^</code></pre>
<p>Press <code>C-t</code> and the o and r are transposed and the cursor moves to the y.</p>
<pre><code>directory
^</code></pre>
<p>If the insertion point is at the end of the line, then this transposes the last two characters of the line.</p>
<pre><code>directoyr --&gt; C-t --&gt; directory --&gt; C-t --&gt; directoyr
^ ^ ^</code></pre>
<h3 id="transpose-words">Transpose words</h3>
<p>You can also transpose words, where a word is defined as we have discussed earlier in the series, a series of letters and digits.</p>
<dl>
<dt><strong><code>M-t</code></strong> <small>(<code>Meta-t</code>)</small></dt>
<dd>Transpose words. The cursor (<em>point</em>) can be anywhere in a word (or just before it). It and the word before it are swapped and the cursor is left after the pair of words. If there is no word before the word the cursor is on then nothing happens. If the cursor is at the end of the line the last word and the word before it are swapped repeatedly for every <code>M-t</code>.
</dd>
</dl>
<h4 id="example-2">Example</h4>
<p>You think that split infinitives are bad:</p>
<pre><code>echo &quot;to boldly go where...&quot;
^</code></pre>
<p>Press <code>M-t</code> and the result is:</p>
<pre><code>echo &quot;to go boldly where...&quot;
^</code></pre>
<p>Note that, even though the cursor is not on a word transposition still takes place. Press <code>M-t</code> again and the result is:</p>
<pre><code>echo &quot;to go where boldly...&quot;
^</code></pre>
<h3 id="change-the-case-of-words">Change the case of words</h3>
<p>Readline allows you to change the case of whole words, to upper case, to lower case or change the case of the first letter of a word to upper case (capitalise it).</p>
<dl>
<dt><strong><code>M-u</code></strong> <small>(<code>Meta-u</code>)</small></dt>
<dd>Uppercase the current (or following) word. With a negative argument, uppercase the previous word, but do not move the cursor.
</dd>
<dt><strong><code>M-l</code></strong> <small>(<code>Meta-l</code>)</small></dt>
<dd>Lowercase the current (or following) word. With a negative argument, lowercase the previous word, but do not move the cursor.
</dd>
<dt><strong><code>M-c</code></strong> <small>(<code>Meta-c</code>)</small></dt>
<dd>Capitalise the current (or following) word. With a negative argument, capitalise the previous word, but do not move the cursor.
</dd>
</dl>
<p>To change the case of a whole word the cursor must be before the word or on its first letter. If it is part-way through the word then the rest of the word from the cursor to the end of the word is changed.</p>
<p>For capitalisation the situation is similar. The capital is at the start of the following word, or it occurs where the cursor is positioned. The rest of the word is lowercased.</p>
<h4 id="examples">Examples</h4>
<h5 id="upper-and-lower-case">1. Upper and lower case</h5>
<p>Given the following command with the cursor positioned as shown (NB: type the line and press <code>M-b</code> three times to move three words backward):</p>
<pre><code>echo &quot;hacker public radio&quot;
^</code></pre>
<p>Press <code>M-u</code> and the result is:</p>
<pre><code>echo &quot;HACKER public radio&quot;
^</code></pre>
<p>The current word has been changed to upper case and the cursor moved after it. Press <code>M-u</code> again and the result is:</p>
<pre><code>echo &quot;HACKER PUBLIC radio&quot;
^</code></pre>
<p>The following word has been changed to upper case and the cursor moved after it. Press <code>M--M-l</code> <small>(<code>Meta--</code> <code>Meta-l</code>)</small> (<em>remember the simplest negative argument is achieved by pressing the Meta key in conjunction with a dash</em>) and the result is:</p>
<pre><code>echo &quot;HACKER public radio&quot;
^</code></pre>
<p>The previous word has been changed to lower case but the cursor has not been moved.<a href="#fn1" class="footnoteRef" id="fnref1"><sup>1</sup></a></p>
<p>Regarding how much of a word is changed:</p>
<pre><code>echo &quot;hacker public radio&quot;
^</code></pre>
<p>Pressing <code>M-u</code> here gives the following result:</p>
<pre><code>echo &quot;hacKER public radio&quot;
^</code></pre>
<h5 id="capitalisation">2. Capitalisation</h5>
<p>Using another command:</p>
<pre><code>echo &quot;the capital of scotland is edinburgh&quot;
^</code></pre>
<p>Press <code>M-b</code> six times then press <code>M-c</code>, <code>M-f</code> twice, and <code>M-c</code> the result is:</p>
<pre><code>echo &quot;The capital of Scotland is edinburgh&quot;
^</code></pre>
<p>Press <code>M-f</code> and <code>M-c</code> the result is:</p>
<pre><code>echo &quot;The capital of Scotland is Edinburgh&quot;
^</code></pre>
<p>What was going on here should be self-evident from the previous episodes in this series! ☺</p>
<h3 id="revert-the-line">Revert the line</h3>
<p>We saw the <em>undo</em> command in <a href="http://hackerpublicradio.org/eps/hpr2073" title="The power of GNU Readline - part 1">episode 1</a> of this series: <code>C-_</code> <small>(<code>Control-_</code>)</small> or <code>C-x</code> <code>C-u</code> <small>(<code>Control-x</code> <code>Control-u</code>)</small> but there is a short-cut that undoes all changes. The following description is copied from the <a href="https://tiswww.cwru.edu/php/chet/readline/rluserman.html#SEC21" title="GNU Readline Manual, section 1.4.8">GNU Readline manual, section 1.4.8</a>.</p>
<dl>
<dt><strong><code>M-r</code></strong> <small>(<code>Meta-r</code>)</small></dt>
<dd>Undo all changes made to this line. This is like executing the undo command enough times to get back to the beginning.
</dd>
</dl>
<h4 id="example-3">Example</h4>
<p>I dont use this sequence often. I experimenting with it for this episode I did not find the description particularly useful so I spent a bit longer looking into it.</p>
<p>First the <em>undo</em> command: this will revert individual steps, as mentioned. Given the following line containing a command:</p>
<pre><code>echo &quot;Star Wars&quot;
^</code></pre>
<p>Move back to the r in Star and press <code>C-t</code>:</p>
<pre><code>echo &quot;Stra Wars&quot;
^</code></pre>
<p>Move forwards to the W in Wars and press <code>C-t</code>:</p>
<pre><code>echo &quot;StraW ars&quot;
^</code></pre>
<p>Now press <code>C-_</code> to perform one <em>undo</em>:</p>
<pre><code>echo &quot;Stra Wars&quot;
^</code></pre>
<p>And again:</p>
<pre><code>echo &quot;Star Wars&quot;
^</code></pre>
<p>We are back to the original state except that the cursor is in a different place. Pressing <code>C-_</code> again results in a blank line.</p>
<p>Given the same starting point, pressing <code>M-r</code> results in the blank line all of the changes are undone including the typing of the command in the first place.</p>
<p>If you recall a command from the history, neither of these key sequences do anything because that is the original state of the line. The line can be deleted with <code>C-u</code> (which <em>kills</em> the entire line as we saw in <a href="http://hackerpublicradio.org/eps/hpr2453" title="The power of GNU Readline - part 2">episode 2</a>).</p>
<p>Also, if the recalled line is edited, individual edits can be reverted with <code>C-_</code> or all of them with <code>M-r</code>, but only back to the state of the line that was recalled.</p>
<h2 id="links">Links</h2>
<ul>
<li>Wikipedia article on <a href="https://en.wikipedia.org/wiki/GNU_Readline"><em>GNU Readline</em></a></li>
<li><a href="https://tiswww.cwru.edu/php/chet/readline/rluserman.html"><em>GNU Readline</em> manual</a></li>
<li>Previous episodes:
<ul>
<li><a href="http://hackerpublicradio.org/eps/hpr2073">The power of GNU Readline - part 1</a></li>
<li><a href="http://hackerpublicradio.org/eps/hpr2453">The power of GNU Readline - part 2</a></li>
</ul></li>
</ul>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>In the audio I wondered whether <code>M-3</code> followed by <code>M-u</code> would uppercase the next three words to the right, and I later found that it does, and it works with <code>M-l</code> and <code>M-c</code>.<br />
<br />
So pressing <code>M-3M-c</code> with the cursor positioned on the h of hacker in <code>echo &quot;hacker public radio&quot;</code> results in: <code>echo &quot;Hacker Public Radio&quot;</code><a href="#fnref1"></a></p></li>
</ol>
</section>
</article>
</main>
</div>
</body>
</html>