Files
hpr_website/www/eps/hpr2348/hpr2348_full_shownotes.html

337 lines
23 KiB
HTML
Raw Permalink Normal View History

2025-10-28 18:39:57 +01:00
<!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>Vim Hints 005 (HPR Show 2348)</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">Vim Hints 005 (HPR Show 2348)</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="#vim-hints-is-back">Vim Hints is back!</a></li>
<li><a href="#quick-recap">Quick recap</a></li>
<li><a href="#copying-and-pasting">Copying and pasting</a><ul>
<li><a href="#copying">Copying</a></li>
<li><a href="#pasting">Pasting</a></li>
<li><a href="#registers">Registers</a></li>
<li><a href="#examples-of-cutting-copying-and-pasting">Examples of cutting, copying and pasting</a></li>
</ul></li>
<li><a href="#text-objects-again">Text objects again</a><ul>
<li><a href="#defining-an-inner-object">Defining an <u>inner</u> object</a></li>
<li><a href="#defining-an-object">Defining <u>an</u> object</a></li>
<li><a href="#examples">Examples</a><ul>
<li><a href="#a-few-other-objects">A few other objects</a></li>
</ul></li>
</ul></li>
<li><a href="#more-changes">More changes</a><ul>
<li><a href="#joining-lines">Joining lines</a></li>
<li><a href="#example">Example</a></li>
</ul></li>
<li><a href="#configuration-file">Configuration file</a><ul>
<li><a href="#syntax-highlighting">Syntax highlighting</a></li>
<li><a href="#indenting">Indenting</a></li>
<li><a href="#automatic-wrapping">Automatic wrapping</a></li>
<li><a href="#tabs-or-spaces">Tabs or spaces</a></li>
<li><a href="#turning-off-the-search-highlight">Turning off the search highlight</a></li>
</ul></li>
<li><a href="#summary">Summary</a><ul>
<li><a href="#configuration-file-1">Configuration file</a></li>
</ul></li>
<li><a href="#links">Links</a></li>
</ul>
</nav>
</header>
<h2 id="vim-hints-is-back">Vim Hints is back!</h2>
<p>Oops! Where did half of 2015, all of 2016 and the first half of 2017 go?</p>
<p>Well, life got in the way, plus motivation dwindled somewhat. This series is very demanding - the <b>sed</b> series was a walk in the park compared to tackling the continental-scale landscape of Vim!</p>
<p>Still, the original goal was to try and introduce the really useful features of Vim and to make it manageable for everyday use. The hope was, and still is, that the series could get people started on their own journeys through its marvels.</p>
<p>Also, with the currently circulating <a href="https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/" title="How to exit Vim">StackOverflow article</a> on “<em>How to exit the Vim editor?</em>”, its worth pointing out that we dealt with that subject in episode 1, and this issue is revealed as the ridiculous meme that it really is!</p>
<h2 id="quick-recap">Quick recap</h2>
<p>To recap, the last episode of this planned series was in March 2015. Heres a list of links to all of the episodes so far:</p>
<ul>
<li><a href="http://hackerpublicradio.org/eps/hpr1714" title="Vim Hints 001">Vim Hints 001</a> (show 1714 2015-02-26)</li>
<li><a href="http://hackerpublicradio.org/eps/hpr1724" title="Vim Hints 002">Vim Hints 002</a> (show 1724 2015-03-12)</li>
<li><a href="http://hackerpublicradio.org/eps/hpr1734" title="Vim Hints 003">Vim Hints 003</a> (show 1734 2015-03-26)</li>
<li><a href="http://hackerpublicradio.org/eps/hpr1776" title="Vim Hints 004">Vim Hints 004</a> (show 1776 2015-05-25)</li>
</ul>
<p>Lets briefly describe what was covered in these episodes to set the context.</p>
<p>So far we have looked at very basic editing in <a href="http://hackerpublicradio.org/eps/hpr1714" title="Vim Hints 001">episode 1</a>, where we mentioned modes <em>Normal</em>, <em>Insert</em> and <em>Command</em> modes.</p>
<p>In <a href="http://hackerpublicradio.org/eps/hpr1724" title="Vim Hints 002">episode 2</a> we looked at Vims backup mechanism, undoing and redoing changes, and file recovery in the event of a problem. We started using the <code>.vimrc</code> configuration file.</p>
<p>We began looking at movement commands in <em>Normal</em> mode in <a href="http://hackerpublicradio.org/eps/hpr1734" title="Vim Hints 003">episode 3</a>, and beefed up the configuration file somewhat.</p>
<p>More movement commands were covered in <a href="http://hackerpublicradio.org/eps/hpr1776" title="Vim Hints 004">episode 4</a> as well as searching. We began looking at commands that make changes, adding, inserting, deleting and changing text in various ways. The concept of doing these things with various movements was covered. Again, a number of useful options for the configuration file were introduced.</p>
<h2 id="copying-and-pasting">Copying and pasting</h2>
<p>So far we have inserted, changed and deleted text, all in <em>Normal</em> mode. Now we want to look at how to copy existing text and how to paste text. See the Vim Help (type <b>:help change.txt</b>) or the online documentation <a href="http://vimdoc.sourceforge.net/htmldoc/change.html" title="Change">here</a> for the full details.</p>
<h3 id="copying">Copying</h3>
<p>The <b>yy</b> command in <em>Normal</em> mode <em>yanks</em> or copies lines. Just like <b>dd</b> (which we saw in episode 4), if preceded by a count it will yank more than the default one line.</p>
<p>Note that the <b>Y</b> command is a synonym for <b>yy</b>. It <u>doesnt</u> do the equivalent of what <b>C</b> and <b>D</b> do, yank from the current position to the end of the line.</p>
<p>The <b>y</b> command <em>yanks</em> or copies characters. Like <b>c</b> (change) and <b>d</b> (delete) (seen in episode 4) it needs a movement command to follow. The table below shows some examples of the operator+movement combinations:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Command</th>
<th style="text-align: left;">Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><strong>yw</strong></td>
<td style="text-align: left;">Yank from the cursor to before the start of the next word</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>ye</strong></td>
<td style="text-align: left;">Yank from the cursor to the end of the next word</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>y$</strong></td>
<td style="text-align: left;">Yank from the cursor to the end of the line (same as <strong>yy</strong>)</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>y0</strong></td>
<td style="text-align: left;">Yank from before the cursor to the beginning of the line</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>y)</strong></td>
<td style="text-align: left;">Yank from the cursor to the end of the sentence</td>
</tr>
</tbody>
</table>
<h3 id="pasting">Pasting</h3>
<p>Having copied text (or having deleted or changed it) its then possible to paste it (or <em>put</em> it as the Vim documentation defines it). The various delete commands save the last text that was deleted, and the change commands save the last text as it was before it was changed.</p>
<p>The <b>p</b> command in <em>Normal</em> mode puts (<em>pastes</em>) text after the cursor. The <b>P</b> command puts (<em>pastes</em>) text before the cursor. In both cases the cursor is left on the last character of the pasted text. Both commands can be preceded by a count, resulting in the text being pasted multiple times.</p>
<p>A number of Vim commands can be preceded by <b>g</b> which makes changes to their effects. We will visit these as we introduce new features.</p>
<p>In the case of these <em>paste</em> commands the effects are:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Command</th>
<th style="text-align: left;">Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><b>gp</b></td>
<td style="text-align: left;">Just like <b>p</b> but leave the cursor after the pasted text</td>
</tr>
<tr class="even">
<td style="text-align: center;"><b>gP</b></td>
<td style="text-align: left;">Just like <b>P</b> but leave the cursor after the pasted text</td>
</tr>
</tbody>
</table>
<h3 id="registers">Registers</h3>
<p>Deleted, changed and copied text is stored in a <b>register</b>. Its a default register if not specified, but a number of other registers exist in Vim and can be chosen as the source and destination of commands.</p>
<blockquote>
<p><em>This is a large topic, and this information is just a forward reference to the subject which well look at in more detail in a forthcoming show in the series.</em></p>
</blockquote>
<h3 id="examples-of-cutting-copying-and-pasting">Examples of cutting, copying and pasting</h3>
<ul>
<li>A simple use of cut and paste is the sequence <b>xp</b>. This swaps the character under the cursor with the one after it. Its really useful if, like me, your fingers keep typing <em>teh</em> instead of <em>the</em>, for example.</li>
<li>The sequence <b>dwwP</b> is useful for swapping words. Remember that <b>dw</b> deletes the current word (assuming the cursor is on the first character), then the next <b>w</b> moves forward one word and <b>P</b> pastes the deleted word in front of it. This is not the most robust and reliable way of doing this, but hopefully it makes the point.</li>
<li>The sequence <b>ywP</b> yanks the current word (again assuming the cursor is on the first character) and pastes it in front of the cursor, thereby duplicating the word.</li>
</ul>
<h2 id="text-objects-again">Text objects again</h2>
<p>We saw in episode 4 that Vim has the concept of <em>text objects</em>, and we looked at sentences and paragraphs, and at movements and actions relating to them. There are more than these, and in this episode well look at them in the context of commands. Well just touch the surface of this subject for now, and will come back for a deeper look in a later episode. See the Vim Help (type <b>:help motion.txt</b>) or the online documentation <a href="http://vimdoc.sourceforge.net/htmldoc/motion.html" title="Motion">here</a> for the full details.</p>
<h3 id="defining-an-inner-object">Defining an <u>inner</u> object</h3>
<p>We have seen commands like <b>dw</b> and <b>yw</b> which have an effect relating to a word. The command <b>yw</b> means “yank from the cursor position to before the beginning of the next word”. However, <b>yiw</b>, means “<em>yank inner word</em>” and has the effect of yanking from the <b>start</b> of the current word to the <b>end</b> of that word. That is, it doesnt matter where in the word the cursor is positioned.</p>
<p>Similarly <b>diw</b> deletes the entire word the cursor is positioned on. The “<em>inner word</em>” means that it does not include any non-word character after the word. In fact, the same applies to leading non-word characters too.</p>
<p>There are many objects that can be used with this “inner” text selection mechanism, including sentences and paragraphs. We will not look at all of them in this episode, but will revisit the subject again later.</p>
<h3 id="defining-an-object">Defining <u>an</u> object</h3>
<p>This terminology is a little confusing, but it exists because the effect is achieved by using an <b>a</b> (“<em>an object</em>”) rather than the <b>i</b> for “<em>inner object</em>”. (I like to think of the <em>a</em> as signifying <em>all</em> as a way to remember it.)</p>
<p>Here <b>yaw</b> includes all trailing white space (if there is any) and leading white space if there was no trailing space. Again the effect works regardless of where the cursor is positioned in the word.</p>
<p>There are many objects that can be used with this type of text selection mechanism, including sentences and paragraphs. We will not look at all of them in this episode, but will revisit the subject again later.</p>
<h3 id="examples">Examples</h3>
<p>The following example shows two rows of numbers which represent the column number in the line of text which follows. We will use these lines to show the result of actions at certain cursor positions:</p>
<pre><code> 1 2 3 4 5 6
123456789012345678901234567890123456789012345678901234567890
Hacker Public Radio is dedicated to sharing knowledge.</code></pre>
<ol type="1">
<li>Cursor at column 10 (on the <em>b</em> of <em>Public</em>). Typing <b>diw</b> here results in the deletion of “<em>Public</em>” leaving the leading and trailing spaces.</li>
</ol>
<pre><code>Hacker Radio is dedicated to sharing knowledge.</code></pre>
<ol start="2" type="1">
<li>Cursor at column 10. Typing <b>daw</b> here results in the deletion of “<em>Public</em><b>including</b> the trailing space.</li>
</ol>
<pre><code>Hacker Radio is dedicated to sharing knowledge.</code></pre>
<ol start="3" type="1">
<li>Cursor at column 48 (on the <em>w</em> of <em>knowledge</em>). Typing <b>diw</b> here results in the deletion of <em>knowledge</em> but leaves the leading space and the terminating full stop.</li>
</ol>
<pre><code>Hacker Public Radio is dedicated to sharing .</code></pre>
<ol start="4" type="1">
<li>Cursor at column 48. Typing <b>daw</b> here results in the deletion of <em>knowledge</em> and the leading space, thereby terminating the sentence.</li>
</ol>
<pre><code>Hacker Public Radio is dedicated to sharing.</code></pre>
<h4 id="a-few-other-objects">A few other objects</h4>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Command</th>
<th style="text-align: left;">Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><b>yis</b></td>
<td style="text-align: left;">Yank inner sentence (start to ending punctuation)</td>
</tr>
<tr class="even">
<td style="text-align: center;"><b>yas</b></td>
<td style="text-align: left;">Yank <em>a</em> sentence (including trailing white spaces)</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><b>yip</b></td>
<td style="text-align: left;">Yank inner paragraph (start to before terminating blank line)</td>
</tr>
<tr class="even">
<td style="text-align: center;"><b>yap</b></td>
<td style="text-align: left;">Yank <em>a</em> paragraph (including trailing blank line)</td>
</tr>
</tbody>
</table>
<h2 id="more-changes">More changes</h2>
<h3 id="joining-lines">Joining lines</h3>
<p>There are times when two lines adjacent to one another might need to be joined together. This can be achieved with the <b>J</b> command (remember that <b>j</b> is a cursor movement command). The <b>J</b> command can be preceded by a count to join multiple lines.</p>
<p>The <b>J</b> command places a space between the joined lines. It is removing the <b>&lt;EOL&gt;</b> (end of line) characters between lines and replacing them with spaces. More spaces may be inserted here if certain options are enabled which add double spaces after the end of a sentence.</p>
<p>The <b>gJ</b> command (remember <b>g</b> is often used for variants of certain commands) joins lines like <b>J</b> does but without adding spaces.</p>
<h3 id="example">Example</h3>
<p>Given the following three lines, we will demonstrate the results of the two commands:</p>
<pre><code>Hacker
Public
Radio</code></pre>
<p>Positioning the cursor on the first line and typing <b>3J</b> results in:</p>
<pre><code>Hacker Public Radio</code></pre>
<p>Whereas the same with <b>3gJ</b> results in:</p>
<pre><code>HackerPublicRadio</code></pre>
<hr />
<h2 id="configuration-file">Configuration file</h2>
<p>The configuration file we have built so far (see <a href="http://hackerpublicradio.org/eps/hpr1776" title="Vim Hints 004">episode 4</a>) has grown moderately long, and it will get longer in this episode. I order to simplify matters this is now included as a separate file: <a href="hpr2348_example_vimrc_5">example_vimrc_5</a>.</p>
<p>Full information on the options available in Vim can be found in the Vim Help (type <b>:h options.txt</b>) or online <a href="http://vimdoc.sourceforge.net/htmldoc/options.html" title="Options">here</a>.</p>
<h3 id="syntax-highlighting">Syntax highlighting</h3>
<p>This turns on Vims syntax highlighting features. We havent really looked at these in detail, but its useful to have some colouring and highlighting wherever its available.</p>
<pre><code>syntax on</code></pre>
<h3 id="indenting">Indenting</h3>
<p>If you have indented a line while typing, and start a new line then Vim will automatically indent that line the same as the original. This is very useful when writing a program or when preparing text.</p>
<p>This feature is turned on with the command:</p>
<pre><code>set autoindent</code></pre>
<p>The abbreviation for the command is is <b>se ai</b> and the effect can be reversed with <b>set noautoindent</b> or <b>se noai</b>.</p>
<h3 id="automatic-wrapping">Automatic wrapping</h3>
<p>As you are typing in <em>Insert</em> mode Vim can wrap automatically to the next line (by automatically adding the necessary line break). It does this when the defined line width has been reached and the current word is completed. It does not split words.</p>
<p>Note that if you add to an existing line and make it exceed the text width, Vim will not wrap the line.</p>
<p>The maximum width of text which triggers wrapping can be defined with the command:</p>
<pre><code>set textwidth=NNN</code></pre>
<p>For example:</p>
<pre><code>set textwidth=78</code></pre>
<p>The abbreviation for the command is is <b>se tw=NNN</b> and the text width feature can be turned off with <b>set textwidth=0</b> or <b>se tw=0</b>. The text width feature is turned off by default.</p>
<h3 id="tabs-or-spaces">Tabs or spaces</h3>
<p>In <em>Insert</em> mode, pressing the <strong>TAB</strong> key inserts a <em>TAB</em> character and moves the cursor to the appropriate tab stop.</p>
<p>The subject of whether to use <em>TAB</em> characters or spaces to indent programs can generate much discussion. We will look at this matter in more depth later in the series, but for now I suggest we make Vim replace <em>TAB</em> characters with spaces by default and make indenting to be in increments of 4 columns.</p>
<p>This can be achieved with two configuration options: <b>expandtab</b> and <b>shiftwidth</b>.</p>
<p>The <b>expandtab</b> option forces all <em>TAB</em> characters to be replaced by the appropriate number of spaces in <em>Insert</em> mode. This is a Boolean option, to to turn it on you need:</p>
<pre><code>set expandtab</code></pre>
<p>To turn it off use:</p>
<pre><code>set noexpandtab</code></pre>
<p>The command can be abbreviated to <b>se et</b> or <b>se noet</b>.</p>
<p>Note that if the file you are editing already contains <em>TAB</em> characters this setting will not affect them. There is a command mode command <b>:retab</b> which can be used to replace all <em>TAB</em> characters but well look at that later.</p>
<p>The <b>shiftwidth</b> option controls the number of spaces to use for autoindenting. It takes an argument - the number of spaces:</p>
<pre><code>set shiftwidth=4</code></pre>
<p>This sets the autoindent step to 4 spaces.</p>
<p>The option can be abbreviated to <b>se sw=4</b>.</p>
<p>As already mentioned, this is not the whole story, but well leave this subject to be developed in an upcoming episode. (Hint: well be looking at <b>tabstop</b> and <b>softtabstop</b> later).</p>
<p>We will also look at the use of <b>CTRL+D</b> (<b>&lt;C-D&gt;</b>) and <b>CTRL+T</b> (<b>&lt;C-T&gt;</b>) to delete and add indents to the automatically created ones.</p>
<h3 id="turning-off-the-search-highlight">Turning off the search highlight</h3>
<p>It was mentioned in <a href="http://hackerpublicradio.org/eps/hpr1776" title="Vim Hints 004">episode 4</a> that when searching for text with <b>incsearch</b> and <b>hlsearch</b> on all of the matches would be highlighted. These strings remain highlighted until another search is executed or the <b>:nohl</b> command is issued.</p>
<p>One way to simplify the cancellation of the highlight is shown in this episodes example configuration file. It uses a feature we have not seen yet, the mapping of a key to a command. We will look at this in detail in a later episode. Suffice it to say that if you add the following to your <code>.vimrc</code> you will be able to turn off the highlighting by typing <em>CTRL-L</em>, which will also refresh (redraw) the window:</p>
<pre><code>nnoremap &lt;C-L&gt; :nohl&lt;CR&gt;&lt;C-L&gt;</code></pre>
<hr />
<h2 id="summary">Summary</h2>
<ul>
<li>Copying
<ul>
<li><b>yy</b> or <b>Y</b> to copy a line</li>
<li><b>y</b><em>motion</em> to copy text up to a movement target</li>
</ul></li>
<li>Pasting
<ul>
<li><b>p</b> puts (pastes) after the cursor</li>
<li><b>P</b> puts (pastes) before the cursor</li>
<li><b>gp</b> and <b>gP</b> like <b>p</b> and <b>P</b> but leave the cursor after the pasted text</li>
</ul></li>
<li>Text objects with <b>i</b> and <b>a</b>
<ul>
<li><b>i</b> means the <em>inner</em> object</li>
<li><b>a</b> means <em>all</em> of the object</li>
</ul></li>
<li>Joining lines
<ul>
<li><b>J</b> joins with spaces</li>
<li><b>gJ</b> joins without spaces</li>
</ul></li>
</ul>
<h3 id="configuration-file-1">Configuration file</h3>
<pre><code>&quot; Previous stuff omitted for now, see &#39;example_vimrc_5&#39;
&quot; Enable syntax highlighting
syntax on
&quot; Indent automatically
set autoindent
&quot; Wrap at 78 characters
set textwidth=78
&quot; In Insert mode use numbers of spaces instead of tabs
set expandtab
&quot; Define number of spaces to use for indenting
set shiftwidth=4
&quot; Highlight searches (use &lt;C-L&gt; to temporarily turn off
&quot; highlighting; see the mapping of &lt;C-L&gt; below)
set hlsearch
&quot; Map &lt;C-L&gt; (redraw screen) to also turn off search highlighting
&quot; until the next search
nnoremap &lt;C-L&gt; :nohl&lt;CR&gt;&lt;C-L&gt;</code></pre>
<h2 id="links">Links</h2>
<ol type="1">
<li><a href="https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/">Stack Overflow: Helping One Million Developers Exit Vim</a></li>
<li>Vim Help:
<ul>
<li><a href="http://vimdoc.sourceforge.net/htmldoc/change.html">Vim documentation: change</a></li>
<li><a href="http://vimdoc.sourceforge.net/htmldoc/motion.html">Vim documentation: motion</a></li>
<li><a href="http://vimdoc.sourceforge.net/htmldoc/options.html">Vim documentation: options</a></li>
</ul></li>
<li>Previous episode: “<a href="http://hackerpublicradio.org/eps/hpr1776">Vim Hints Episode 4</a></li>
<li>Resources:
<ul>
<li>Full notes in <a href="hpr2348_full_shownotes.epub">ePub form</a></li>
<li><a href="hpr2348_example_vimrc_5">Example .vimrc</a></li>
</ul></li>
</ol>
<!--
vim: syntax=markdown:ts=8:sw=4:ai:et:tw=78:fo=tcqn:fdm=marker
-->
</article>
</main>
</div>
</body>
</html>