Files
hpr_website/www/eps/hpr1734/hpr1734_full_shownotes.html

282 lines
16 KiB
HTML
Raw 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 003 (HPR Show 1734)</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 003 (HPR Show 1734)</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="#moving-around">Moving Around</a><ul>
<li><a href="#simple-movement">Simple movement</a></li>
<li><a href="#word-related-movement">Word-related movement</a></li>
</ul></li>
<li><a href="#more-configuration-settings">More configuration settings</a><ul>
<li><a href="#adding-a-ruler">Adding a ruler</a></li>
<li><a href="#adding-a-status-line">Adding a status line</a></li>
<li><a href="#showing-the-mode">Showing the mode</a></li>
<li><a href="#adding-comments">Adding comments</a></li>
<li><a href="#screenshot">Screenshot</a></li>
</ul></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#links">Links</a></li>
</ul>
</nav>
</header>
<p>In this episode I want to look at how you move around the file you are editing in Vim. I also want to add some more elements to the configuration file we started building in the <a href="http://hackerpublicradio.org/eps.php?id=1724" title="Vim Hints 002">last episode</a>.</p>
<h2 id="moving-around">Moving Around</h2>
<p>One of the powerful features of Vim is the ease with which you can move around a file.</p>
<h3 id="simple-movement">Simple movement</h3>
<p>Some of the basic movements in <em>Normal</em> mode are:</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Key</th>
<th style="text-align: left;">Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><strong>l</strong> or <strong>cursor-right</strong></td>
<td style="text-align: left;">Move right</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>k</strong> or <strong>cursor-up</strong></td>
<td style="text-align: left;">Move up</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>j</strong> or <strong>cursor-down</strong></td>
<td style="text-align: left;">Move down</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>h</strong> or <strong>cursor-left</strong></td>
<td style="text-align: left;">Move left</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>$</strong> or <strong>End key</strong></td>
<td style="text-align: left;">Move to the end of the line</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>0</strong> or <strong>Home key</strong></td>
<td style="text-align: left;">Move to the start of the line</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>^</strong></td>
<td style="text-align: left;">Move to the first non-blank character of the line</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>-</strong></td>
<td style="text-align: left;">Move up to first non-blank character</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>+</strong></td>
<td style="text-align: left;">Move down to first non-blank character</td>
</tr>
</tbody>
</table>
<p><strong>Note</strong>: In the Vim documentation there is an alternative annotation for these keys (and many others):</p>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Vim Annotation</th>
<th style="text-align: center;">Key</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">&lt;Up&gt;</td>
<td style="text-align: center;">cursor-up</td>
</tr>
<tr class="even">
<td style="text-align: left;">&lt;Down&gt;</td>
<td style="text-align: center;">cursor-down</td>
</tr>
<tr class="odd">
<td style="text-align: left;">&lt;Left&gt;</td>
<td style="text-align: center;">cursor-left</td>
</tr>
<tr class="even">
<td style="text-align: left;">&lt;Right&gt;</td>
<td style="text-align: center;">cursor-right</td>
</tr>
<tr class="odd">
<td style="text-align: left;">&lt;Home&gt;</td>
<td style="text-align: center;">home</td>
</tr>
<tr class="even">
<td style="text-align: left;">&lt;End&gt;</td>
<td style="text-align: center;">end</td>
</tr>
</tbody>
</table>
<p>We will use this form of annotation in these and future notes. These will also be important when we look at customisation.</p>
<p>If a key is used in conjunction with the <strong>Shift</strong> or <strong>Control</strong> (<strong>CTRL</strong>) keys the annotation is shown as <strong>&lt;S-Right&gt;</strong> (shift + cursor-right) or <strong>&lt;C-Right&gt;</strong> (CTRL + cursor-right).</p>
<p>Some of these motion commands hardly seem different from what is available in other editors. Many presses of the right cursor key will move the cursor to the right a number of columns in most editors. However, Vim allows these keys to be preceded by a number. So, typing:</p>
<pre><code>10l</code></pre>
<p>will move the cursor 10 characters to the right, as will <strong>10&lt;Right&gt;</strong>.</p>
<p>The same goes for <strong>10h</strong> or <strong>10&lt;Left&gt;</strong>, <strong>10k</strong> or <strong>10&lt;Up&gt;</strong> and so forth.</p>
<p>The only movement commands in this group which do not take a count are <strong>0</strong> / <strong>&lt;Home&gt;</strong> and <strong>^</strong>.</p>
<h3 id="word-related-movement">Word-related movement</h3>
<p>The next movement commands (used in <em>Normal</em> mode) move the cursor in relation to words in the text. There two definitions of &quot;<em>word</em>&quot; in this context. We will use the Vim convention in these notes and refer to them as <em><code>word</code></em> and <em><code>WORD</code></em>.</p>
<p>These are the definitions from the Vim documentation:</p>
<p><em><code>word</code></em> : a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, end of line). An empty line is also considered to be a <em><code>word</code></em>.</p>
<p><em><code>WORD</code></em> : a sequence of non-blank characters, separated with white space. An empty line is also considered to be a <em><code>WORD</code></em>.</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">Key</th>
<th style="text-align: left;">Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><strong>w</strong> or <strong>&lt;S-Right&gt;</strong></td>
<td style="text-align: left;">Move forward to the start of a <em><code>word</code></em></td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>W</strong> or <strong>&lt;C-Right&gt;</strong></td>
<td style="text-align: left;">Move forward to the start of a <em><code>WORD</code></em></td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>e</strong></td>
<td style="text-align: left;">Move forward to the end of a <em><code>word</code></em></td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>E</strong></td>
<td style="text-align: left;">Move forward to the end of a <em><code>WORD</code></em></td>
</tr>
<tr class="odd">
<td style="text-align: center;"><strong>b</strong> or <strong>&lt;S-Left&gt;</strong></td>
<td style="text-align: left;">Move backward to the start of a <em><code>word</code></em></td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>B</strong> or <strong>&lt;C-Left&gt;</strong></td>
<td style="text-align: left;">Move backward to the start of a <em><code>WORD</code></em></td>
</tr>
</tbody>
</table>
<p>These movement commands may be preceded by a numeric count, as before, so <strong>5w</strong> or <strong>5&lt;S-Right&gt;</strong> will move the cursor forward by 5 words to the start of the 6th word from the current position.</p>
<p>The following list shows the effects of various word-related movements moving the cursor along the example log record. It contrasts the use of <em><code>word</code></em> versus <em><code>WORD</code></em> commands. The <strong>^</strong> characters represent the cursor positions after the various commands. All commands begin moving from the <strong>F</strong> of <em>FAT</em>. The last two move to the right 80 columns then backwards.</p>
<pre><code> FAT-fs (sdh): utf8 is not a recommended IO charset for FAT filesystems, filesystem will be case sensitive!
^
5w ^
5W ^
7e ^
7E ^
80l5b ^
80l5B ^</code></pre>
<p>If this is unclear then here are the effects of these commands in text form:</p>
<ul>
<li><strong>5w</strong> moves forward to the closing bracket</li>
<li><strong>5W</strong> moves forward to the first lower case '<em>a</em>'</li>
<li><strong>7e</strong> moves forward to the '<em>8</em>'</li>
<li><strong>7E</strong> moves forward to the last '<em>d</em>' of '<em>recommended</em>'</li>
<li><strong>80l5b</strong> moves forward 80 columns to the right to the second '<em>e</em>' of '<em>filesystem</em>' then backwards to the '<em>f</em>' of '<em>for</em>'</li>
<li><strong>80l5B</strong> moves forward 80 columns to the right then backwards to the '<em>c</em>' of '<em>charset</em>'</li>
</ul>
<p>There are many more movement commands which we will look at in forthcoming episodes.</p>
<hr />
<h2 id="more-configuration-settings">More configuration settings</h2>
<p>In the <a href="http://hackerpublicradio.org/eps.php?id=1724" title="Vim Hints 002">last episode</a> we looked at some of the basic elements of the configuration file. So far the file contains the following:</p>
<pre><code>set compatible
set backup
set undodir=~/.vim/undodir
set undofile</code></pre>
<p>We can now add some more settings.</p>
<h3 id="adding-a-ruler">Adding a ruler</h3>
<p>Vim will display a ruler at the bottom of the screen if this option is enabled. Add the following to the configuration file:</p>
<pre><code>set ruler</code></pre>
<p>This causes the line and column number of the cursor position to be shown at the bottom right of the screen, separated by a comma. When there is room, the relative position of the displayed text in the file is shown on the far right.</p>
<p>The relative position is <em>Top</em> when the first line of the file is visible, <em>Bot</em> when the last line is visible, <em>All</em> when both top and bottom lines are visible, and if none of the foregoing, <em>N%</em>, the relative position in the file.</p>
<p>The command can be abbreviated to <strong>se ru</strong>. I prefer to use the full form because it is easier to remember what it means!</p>
<p>The ruler can also be turned off with <strong>set noruler</strong> which you would prefix with a colon while in a Vim editing session to enter <em>command mode</em>:</p>
<pre><code>:set noruler</code></pre>
<p>It is possible to customise the contents of the ruler, but we will not be looking at this for the moment.</p>
<p>Note that some Linux distributions set this option for you. I run <em>Debian Testing</em> and a <strong>set ruler</strong> definition can be found in <strong>/usr/share/vim/vim74/debian.vim</strong>. It is a good idea to set it in your configuration file regardless, however, because you might need to transfer this file to another distribution in the future,</p>
<h3 id="adding-a-status-line">Adding a status line</h3>
<p>By default the Vim window uses the whole terminal window except for the last line, as we saw in <a href="http://hackerpublicradio.org/eps.php?id=1714" title="Vim Hints 001">episode 1</a>. The last line is used for displaying various messages, and the ruler, and for entering &quot;<strong>:</strong>&quot; commands.</p>
<p>It is possible to separate the status information from the command entry line with the following option:</p>
<pre><code>set laststatus=2</code></pre>
<p>This creates an inverse colour status line at the bottom of the screen followed by the command entry line. The status line contains the name of the file being edited, and the ruler (if enabled). The final line contains messages and is where commands are entered.</p>
<p>If the terminal you are using is small (like the 24 line by 80 column hardware terminals Vi was originally written for), stealing these lines from the Vim workspace may be a problem. In today's world it's unlikely to be so, and I always enable these.</p>
<p>This command can be abbreviated to <strong>se ls=2</strong>.</p>
<p>The status line can also be turned off with <strong>set laststatus=0</strong>.</p>
<h3 id="showing-the-mode">Showing the mode</h3>
<p>As we have seen, Vim is a modal editor with several modes, some of which we have yet to look at. By default, Vim does not indicate which mode it is in, but the following command in the configuration file will change this:</p>
<pre><code>set showmode</code></pre>
<p>As with <strong>set ruler</strong>, some Linux distributions set this for you, but I believe in setting this myself.</p>
<p>With <em>showmode</em> enabled a message such as <code>-- INSERT --</code> (<em>insert</em> mode) is shown on the last line of the Vim window.</p>
<p>This command can be abbreviated to <strong>se smd</strong>.</p>
<p>The mode display can also be turned off with <strong>set noshowmode</strong> which you would prefix with a colon while in a Vim editing session to enter <em>command mode</em>:</p>
<pre><code>:set noshowmode</code></pre>
<h3 id="adding-comments">Adding comments</h3>
<p>The comment character used by Vim in configuration files and elsewhere is the double quote character '<strong>&quot;</strong>'. See the summary below for an example.</p>
<h3 id="screenshot">Screenshot</h3>
<p>The following screenshot shows Vim in an <em>xterm</em> window (24x80) editing the notes for this episode (written in enhanced <em>Markdown</em>, to be processed with <em>pandoc</em>). The configuration file used is the same as that shown below in the summary.</p>
<p><img src="hpr1734_img001.png" alt="Vim screenshot" /><br />Picture: Vim with ruler and status line</p>
<h2 id="summary">Summary</h2>
<ul>
<li>Movement
<ul>
<li><strong>h</strong>, <strong>j</strong>, <strong>k</strong>, <strong>l</strong> or cursor keys</li>
<li><strong>$</strong> or <strong>&lt;End&gt;</strong></li>
<li><strong>0</strong> or <strong>&lt;Home&gt;</strong></li>
<li><strong>^</strong>, <strong>-</strong> and <strong>+</strong></li>
<li><strong>w</strong> or <strong>&lt;S-Right&gt;</strong>, <strong>W</strong> or <strong>&lt;C-Right&gt;</strong></li>
<li><strong>e</strong>, <strong>E</strong></li>
<li><strong>b</strong> or <strong>&lt;S-Left&gt;</strong>, <strong>B</strong> or <strong>&lt;C-Left&gt;</strong></li>
</ul></li>
<li>Configuration file - this time with comments</li>
</ul>
<pre><code>&quot; Ensure Vim runs as Vim
set nocompatible
&quot; Keep a backup file
set backup
&quot; Keep change history
set undodir=~/.vim/undodir
set undofile
&quot; Show the line,column and the % of buffer
set ruler
&quot; Always show a status line per window
set laststatus=2
&quot; Show Insert, Replace or Visual on the last line
set showmode</code></pre>
<h2 id="links">Links</h2>
<ol type="1">
<li>Vim Hints Episode 1 <a href="http://hackerpublicradio.org/eps.php?id=1714">http://hackerpublicradio.org/eps.php?id=1714</a></li>
<li>Vim Hints Episode 2 <a href="http://hackerpublicradio.org/eps.php?id=1724">http://hackerpublicradio.org/eps.php?id=1724</a></li>
</ol>
<!--
vim: syntax=markdown:ts=8:sw=4:ai:et:tw=78:fo=tcqn:fdm=marker
-->
</article>
</main>
</div>
</body>
</html>