360 lines
26 KiB
HTML
360 lines
26 KiB
HTML
|
|
<!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 004 (HPR Show 1776)</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 004 (HPR Show 1776)</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="#more-movement-commands">More movement commands</a><ul>
|
||
|
|
<li><a href="#sentences-and-paragraphs">Sentences and paragraphs</a></li>
|
||
|
|
<li><a href="#moving-up-and-down">Moving up and down</a></li>
|
||
|
|
<li><a href="#searching">Searching</a></li>
|
||
|
|
<li><a href="#matching-pairs">Matching pairs</a></li>
|
||
|
|
</ul></li>
|
||
|
|
<li><a href="#commands-that-make-changes">Commands that make changes</a><ul>
|
||
|
|
<li><a href="#insert-commands">Insert commands</a><ul>
|
||
|
|
<li><a href="#appending-text">Appending text</a></li>
|
||
|
|
<li><a href="#inserting-text">Inserting text</a></li>
|
||
|
|
<li><a href="#beginning-a-new-line">Beginning a new line</a></li>
|
||
|
|
<li><a href="#examples-of-text-insertion">Examples of text insertion</a></li>
|
||
|
|
</ul></li>
|
||
|
|
<li><a href="#deletion-commands">Deletion commands</a></li>
|
||
|
|
<li><a href="#change-commands">Change commands</a></li>
|
||
|
|
</ul></li>
|
||
|
|
<li><a href="#changes-and-movement">Changes and movement</a><ul>
|
||
|
|
<li><a href="#deleting-with-movement">Deleting with movement</a></li>
|
||
|
|
<li><a href="#changing-with-movement">Changing with movement</a></li>
|
||
|
|
</ul></li>
|
||
|
|
<li><a href="#configuration-file">Configuration file</a><ul>
|
||
|
|
<li><a href="#the-story-so-far">The story so far</a></li>
|
||
|
|
<li><a href="#stop-beeping">Stop beeping!</a></li>
|
||
|
|
<li><a href="#showing-incomplete-commands">Showing incomplete commands</a></li>
|
||
|
|
<li><a href="#command-history">Command history</a></li>
|
||
|
|
<li><a href="#ignore-case-when-searching">Ignore case when searching</a></li>
|
||
|
|
<li><a href="#searching-incrementally">Searching incrementally</a></li>
|
||
|
|
<li><a href="#wrapping-the-search-around">Wrapping the search around</a></li>
|
||
|
|
<li><a href="#highlighting-the-search">Highlighting the search</a></li>
|
||
|
|
<li><a href="#enable-extra-features-in-insert-mode">Enable extra features in INSERT mode</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="more-movement-commands">More movement commands</h2>
|
||
|
|
<p>So far we have seen how to move by character, by word and by line. We saw how Vim has two concepts of <em><code>word</code></em> with some demonstrations of what that means.</p>
|
||
|
|
<p>Now we will look at more movement commands.</p>
|
||
|
|
<p>This information can be found in the Vim Help (type <strong>:help motion.txt</strong>) and is online <a href="http://vimdoc.sourceforge.net/htmldoc/motion.html" title="Motion">here</a>. I will be making reference to the Vim documentation in this episode, even though it is very detailed and covers many aspects which we have not yet covered. There is a help page about Vim Help (type <strong>:h help</strong>) which is online <a href="http://vimdoc.sourceforge.net/htmldoc/helphelp.html" title="Vim Help">here</a>.</p>
|
||
|
|
<h3 id="sentences-and-paragraphs">Sentences and paragraphs</h3>
|
||
|
|
<p>Sentences and paragraphs are referred to as <em>text objects</em> in Vim.</p>
|
||
|
|
<p>A sentence is defined as ending at a '.', '!' or '?' followed by either the end of a line, or by a space or tab.</p>
|
||
|
|
<p>In <em>Normal</em> mode the <strong>)</strong> command moves forward one sentence, and <strong>(</strong> moves backward. Both commands, like those we have seen before, take a count, so <strong>3)</strong> moves forward three sentences.</p>
|
||
|
|
<p>In the context of Vim a paragraph is a group of sentences which begins after each empty line.</p>
|
||
|
|
<p>In <em>Normal</em> mode the <strong>}</strong> command moves forward one paragraph, and <strong>{</strong> moves backward. Again both commands take a count, so <strong>2}</strong> moves forward two paragraphs.</p>
|
||
|
|
<h3 id="moving-up-and-down">Moving up and down</h3>
|
||
|
|
<p>There are many ways of moving up and down in a file. We have already seen two such commands: <strong>-</strong> and <strong>+</strong>. Both can be preceded by a number, so <strong>10-</strong> moves up ten lines and positions to the first non-blank character and <strong>10+</strong> moves downwards in an equivalent way.</p>
|
||
|
|
<p>The <strong>G</strong> command will move to a specific line in the file. Typing a <strong>G</strong> on its own in <em>Normal</em> mode will move to the end of the file. Typing <strong>1G</strong> will move to the first line of the file (there is also a <strong>gg</strong> command that does the same). Otherwise, any number before the <strong>G</strong> will move to that line, so <strong>42G</strong> moves to line 42.</p>
|
||
|
|
<p>The <strong>gg</strong> command mentioned above can also be used to move to a particular line, so <strong>42gg</strong> moves to line 42 in the same way as <strong>42G</strong>.</p>
|
||
|
|
<h3 id="searching">Searching</h3>
|
||
|
|
<p>Not surprisingly, with Vim you can search the file you are editing. Full information can be found in the Vim Help (type <strong>:h pattern.txt</strong>) or online <a href="http://vimdoc.sourceforge.net/htmldoc/pattern.html" title="Searching">here</a>.</p>
|
||
|
|
<p>Searching forward is initiated by typing the <strong>/</strong> key in <em>Normal</em> mode, and to search backward the <strong>?</strong> key is used.</p>
|
||
|
|
<p>When a search is initiated the <strong>/</strong> or <strong>?</strong> character appears in the command line at the bottom of the screen and the next characters you type are the search target. The typing of the target is ended by pressing the <strong><CR></strong> (or <strong><Enter></strong>) key and the search is initiated.</p>
|
||
|
|
<p>The search target can be something quite simple like a sequence of letters and numbers, but it is actually a <em>pattern</em> which can be a <em>regular expression</em>. This is quite a large subject so we will deal with it in more depth in a later episode of this series. For now we will restrict ourselves to the simpler aspects.</p>
|
||
|
|
<p>Typing the following sequence in <em>Normal</em> mode:</p>
|
||
|
|
<pre><code>/the<CR></code></pre>
|
||
|
|
<p>will result in a search for the characters <strong>the</strong> and the cursor will be positioned on the next occurrence forward from the current location.</p>
|
||
|
|
<p>Pressing the <strong><Esc></strong> key while typing the search target will abort the search.</p>
|
||
|
|
<p>Once the first occurrence has been found pressing <strong>n</strong> will move to the next occurrence. This results in forward movement if the search used <strong>/</strong> and backward movement when using <strong>?</strong>.</p>
|
||
|
|
<p>Pressing <strong>N</strong> causes the search to change direction.</p>
|
||
|
|
<p>Preceding a search by a number causes it to skip to the nth instance of the target. So typing <strong>3</strong> then:</p>
|
||
|
|
<pre><code>/but<CR></code></pre>
|
||
|
|
<p>will position to the third instance of <strong>but</strong> from the current cursor position.</p>
|
||
|
|
<p>There are a number of settings that affect the searching process, and some recommended ones are listed and explained below in the <em>Configuration file</em> section. In short they do the following:</p>
|
||
|
|
<ul>
|
||
|
|
<li>ignore the case of letters when searching <strong>except</strong> when the target contains a capital letter, when an exact match is searched for</li>
|
||
|
|
<li>start the search as the target is being typed</li>
|
||
|
|
<li>continue the search at the top (or bottom) of the file when the bottom (or top) is reached</li>
|
||
|
|
<li>highlight all the search matches</li>
|
||
|
|
</ul>
|
||
|
|
<h3 id="matching-pairs">Matching pairs</h3>
|
||
|
|
<p>Vim can move the cursor between matching pairs of characters such as '(' and ')', '{' and '}' and '[' and ']'. The command that does this in <em>Normal</em> mode is <strong>%</strong>.</p>
|
||
|
|
<p>If the cursor is placed on the opening character of the pair it will jump to the closing one. If on the closing character it will jump to the opening one. If it is positioned before the opening character of the pair it will jump to the closing one. If it is between the pair it will be positioned to the opening character.</p>
|
||
|
|
<p>The command will also move between the start and end of a C-style comment:</p>
|
||
|
|
<pre><code>/* C-style comment */</code></pre>
|
||
|
|
<p>It is possible to extend the pairs of characters that this command recognises, and there is a Vim plugin available which considerably enhances its functionality. I will leave this subject until later in this series when we look at Vim plugins.</p>
|
||
|
|
<h2 id="commands-that-make-changes">Commands that make changes</h2>
|
||
|
|
<p>When Vim is editing a file it makes a copy of its contents into a <em>buffer</em>. This is what is displayed and can be manipulated. As we already know, the changes can be discarded with <strong>:q!</strong> or saved to the file with <strong>:w</strong>. Changes can also be undone with the <strong>u</strong> command.</p>
|
||
|
|
<p>The commands in this section perform changes to the buffer.</p>
|
||
|
|
<h3 id="insert-commands">Insert commands</h3>
|
||
|
|
<p>These are commands that insert new text into the buffer. They can all be preceded by a <em>count</em>.</p>
|
||
|
|
<p>Full information can be found in the Vim Help (type <strong>:h insert.txt</strong>) or online <a href="http://vimdoc.sourceforge.net/htmldoc/insert.html" title="Insertion">here</a>.</p>
|
||
|
|
<h4 id="appending-text">Appending text</h4>
|
||
|
|
<p>The <strong>a</strong> command appends text after the cursor. Vim enters <em>Insert</em> mode and text will continue to be added until the <em>Escape</em> (<strong><Esc></strong>) key is pressed. If there was a <em>count</em> before the command the insertion is repeated that many times.</p>
|
||
|
|
<p>The <strong>A</strong> command also appends text but at the end of the line</p>
|
||
|
|
<h4 id="inserting-text">Inserting text</h4>
|
||
|
|
<p>The <strong>i</strong> command inserts text before the cursor. As before <em>Insert</em> mode is ended by pressing the <em>Escape</em> (<strong><Esc></strong>) key. If there was a <em>count</em> before the command the insertion is repeated that many times.</p>
|
||
|
|
<p>The <strong>I</strong> command inserts text at the start of the line <em>before the first non-blank</em>. The insertion is repeated if a <em>count</em> was present.</p>
|
||
|
|
<p>Vim has an alternative command <strong>gI</strong> which is like <strong>I</strong> but inserts the text in column 1.</p>
|
||
|
|
<h4 id="beginning-a-new-line">Beginning a new line</h4>
|
||
|
|
<p>The <strong>o</strong> command begins a new line below the cursor and allows text to be entered until <em>Escape</em> (<strong><Esc></strong>) is pressed. The <em>count</em> causes the new line and any text to be repeated.</p>
|
||
|
|
<p>The <strong>O</strong> command begins a new line above the cursor and allows text to be entered until <em>Escape</em> (<strong><Esc></strong>) is pressed. The <em>count</em> causes the new line and any text to be repeated.</p>
|
||
|
|
<h4 id="examples-of-text-insertion">Examples of text insertion</h4>
|
||
|
|
<ul>
|
||
|
|
<li>Typing <strong>80i-<Esc></strong> at the start of a blank line will create a line of 80 hyphens.</li>
|
||
|
|
<li>Typing <strong>eas<Esc></strong> while on a word will append an <strong>s</strong> to it</li>
|
||
|
|
<li>Typing <strong>10oHello World<Esc></strong> will cause 10 lines containing <code>Hello World</code> to be inserted</li>
|
||
|
|
</ul>
|
||
|
|
<h3 id="deletion-commands">Deletion commands</h3>
|
||
|
|
<p>Full information can be found in the Vim Help (type <strong>:h change.txt</strong>) or online <a href="http://vimdoc.sourceforge.net/htmldoc/change.html" title="Changing">here</a>.</p>
|
||
|
|
<p>The <strong>x</strong> command in <em>Normal</em> mode deletes the character under the cursor. With a <em>count</em> it deletes characters after the cursor. It will not delete beyond the end of the line.</p>
|
||
|
|
<p>The <strong>X</strong> command in <em>Normal</em> mode deletes the character before the cursor. With a <em>count</em> it will delete that number before the cursor. It will not delete before the start of the line.</p>
|
||
|
|
<p>The <strong>dd</strong> command deletes lines, one by default, or more if a <em>count</em> was given.</p>
|
||
|
|
<p>The <strong>D</strong> command deletes from the character under the cursor to the end of the line, and if a <em>count</em> was given, that number minus 1 more full lines.</p>
|
||
|
|
<p>There is a <strong>d</strong> command as well but we will look at that shortly.</p>
|
||
|
|
<h3 id="change-commands">Change commands</h3>
|
||
|
|
<p>The <strong>cc</strong> command deletes the number of lines specified by the <em>count</em> (default 1) and enters <em>Insert</em> mode to allow text to be inserted. The <strong><Esc></strong> key ends the insertion as before.</p>
|
||
|
|
<p>The <strong>C</strong> command deletes from the cursor position to the end of the line, and if a <em>count</em> was given, that number minus 1 more full lines, then enters <em>Insert</em> mode. The <strong><Esc></strong> key ends the insertion as before.</p>
|
||
|
|
<p>There is a <strong>c</strong> command as well but we will look at that shortly.</p>
|
||
|
|
<p>The <strong>s</strong> command deletes <em>count</em> characters and enters <em>Insert</em> mode, which is ended with the <strong><Esc></strong> as usual.</p>
|
||
|
|
<p>The <strong>S</strong> command is a synonym for the <strong>cc</strong> command described above.</p>
|
||
|
|
<h2 id="changes-and-movement">Changes and movement</h2>
|
||
|
|
<p>At last we can join together the movement commands and some of the commands that change things in Vim. This is where some of the real editing power of Vim resides.</p>
|
||
|
|
<h3 id="deleting-with-movement">Deleting with movement</h3>
|
||
|
|
<p>We skipped the <strong>d</strong> command in the above section because it only really comes into its own in conjunction with motions. This command when followed by a motion command deletes the thing encompassed by the motion.</p>
|
||
|
|
<p>So, for example, <strong>dw</strong> deletes to the beginning of the next word from the position of the cursor. 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>dw</strong></td>
|
||
|
|
<td style="text-align: left;">Delete from the cursor to the start of the next word</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="even">
|
||
|
|
<td style="text-align: center;"><strong>de</strong></td>
|
||
|
|
<td style="text-align: left;">Delete from the cursor to the end of the next word</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="odd">
|
||
|
|
<td style="text-align: center;"><strong>d$</strong></td>
|
||
|
|
<td style="text-align: left;">Delete from the cursor to the end of the line (same as <strong>D</strong>)</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="even">
|
||
|
|
<td style="text-align: center;"><strong>d0</strong></td>
|
||
|
|
<td style="text-align: left;">Delete from before the cursor to the beginning of the line</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="odd">
|
||
|
|
<td style="text-align: center;"><strong>d)</strong></td>
|
||
|
|
<td style="text-align: left;">Delete from the cursor to the end of the sentence</td>
|
||
|
|
</tr>
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
<h3 id="changing-with-movement">Changing with movement</h3>
|
||
|
|
<p>Similar to the <strong>d</strong> command we also skipped the <strong>c</strong> command in the above section.</p>
|
||
|
|
<p>So, for example, <strong>cw</strong> deletes to the beginning of the next word from the position of the cursor, then enters <em>Insert</em> mode for a replacement to be inserted. 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>cw</strong></td>
|
||
|
|
<td style="text-align: left;">Change from the cursor to the start of the next word</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="even">
|
||
|
|
<td style="text-align: center;"><strong>ce</strong></td>
|
||
|
|
<td style="text-align: left;">Change from the cursor to the end of the next word</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="odd">
|
||
|
|
<td style="text-align: center;"><strong>c$</strong></td>
|
||
|
|
<td style="text-align: left;">Change from the cursor to the end of the line (same as <strong>C</strong>)</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="even">
|
||
|
|
<td style="text-align: center;"><strong>c0</strong></td>
|
||
|
|
<td style="text-align: left;">Change from before the cursor to the beginning of the line</td>
|
||
|
|
</tr>
|
||
|
|
<tr class="odd">
|
||
|
|
<td style="text-align: center;"><strong>c)</strong></td>
|
||
|
|
<td style="text-align: left;">Change from the cursor to the end of the sentence</td>
|
||
|
|
</tr>
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
<p>There are many more ways of deleting and changing text with movement which we will look at in more detail in a future episode.</p>
|
||
|
|
<hr />
|
||
|
|
<h2 id="configuration-file">Configuration file</h2>
|
||
|
|
<h3 id="the-story-so-far">The story so far</h3>
|
||
|
|
<p>In the <a href="https://hackerpublicradio.org/eps/hpr1734" title="Vim Hints 003">last episode</a> we extended the configuration file with a ruler and status line. Now we can add some more settings that make Vim more convenient to use.</p>
|
||
|
|
<p>Full information on the options available in Vim can be found in the Vim Help (type <strong>:h options.txt</strong>) or online <a href="http://vimdoc.sourceforge.net/htmldoc/options.html" title="Options">here</a>.</p>
|
||
|
|
<h3 id="stop-beeping">Stop beeping!</h3>
|
||
|
|
<p>Vim has a tendency to beep to alert you to events and errors. This can be a tiny bit annoying, especially in a shared workplace. Instead of an aural alert you can request a visual one with the command:</p>
|
||
|
|
<pre><code>set visualbell</code></pre>
|
||
|
|
<p>The abbreviation is <strong>se vb</strong> and the inverse is <strong>set novisualbell</strong> or <strong>se novb</strong>.</p>
|
||
|
|
<h3 id="showing-incomplete-commands">Showing incomplete commands</h3>
|
||
|
|
<p>As we have seen, Vim commands can consist of sequences of numbers and command letters. For example <strong>23dd</strong> means delete 23 lines.</p>
|
||
|
|
<p>The command:</p>
|
||
|
|
<pre><code>set showcmd</code></pre>
|
||
|
|
<p>makes Vim show the command that is being typed. So with <strong>23dd</strong> the <strong>23d</strong> part will be visible waiting for the final <strong>d</strong>, after which the display will be cleared and the command actioned.</p>
|
||
|
|
<p>The display of the partial command is shown in the status line at the bottom of the screen.</p>
|
||
|
|
<p>The abbreviation is <strong>se sc</strong> and the effect can be reversed with <strong>set noshowcmd</strong> or <strong>se nosc</strong>.</p>
|
||
|
|
<h3 id="command-history">Command history</h3>
|
||
|
|
<p>By default Vim will remember the last 50 ':' commands (and the last 50 searches) in <em>history tables</em>. When you press the ':' key or begin a search with '/' or '?' the history table can be traversed with the up and down cursor keys. The size of all of the history tables can be extended with the command such as the following:</p>
|
||
|
|
<pre><code>set history=100</code></pre>
|
||
|
|
<p>The abbreviation for the above command is <strong>se hi=100</strong>.</p>
|
||
|
|
<h3 id="ignore-case-when-searching">Ignore case when searching</h3>
|
||
|
|
<p>Normally Vim searches for the exact case you provide in your search target. You can switch this off with the command:</p>
|
||
|
|
<pre><code>set ignorecase</code></pre>
|
||
|
|
<p>You might think that this is a little counter-intuitive; I certainly did when I first encountered it. However, in conjunction with the next command:</p>
|
||
|
|
<pre><code>set smartcase</code></pre>
|
||
|
|
<p>it seems more usable. When the <strong>smartcase</strong> option is enabled Vim will search for both lower and upper case forms when there are only lower case letters in the target, but will search for an exact match when the target is mixed case.</p>
|
||
|
|
<p>The abbreviation for <strong>set ignorecase</strong> is <strong>se ic</strong> and for <strong>set smartcase</strong> is <strong>se scs</strong>. The options can be reversed with <strong>set noignorecase</strong> (<strong>se noic</strong>) and <strong>set nosmartcase</strong> (<strong>se noscs</strong>).</p>
|
||
|
|
<h3 id="searching-incrementally">Searching incrementally</h3>
|
||
|
|
<p>While typing a search pattern, Vim can show where that part of the pattern which has been typed so far matches. This feature is enabled with the <strong>incsearch</strong> option. The matched string is highlighted, but if the pattern is invalid or not found, nothing is shown. In this mode the screen will be updated frequently so it should not be used over a slow link to a remote system!</p>
|
||
|
|
<pre><code>set incsearch</code></pre>
|
||
|
|
<p>The abbreviation is <strong>se is</strong> and the option is turned off with <strong>set noincsearch</strong> or <strong>se nois</strong>.</p>
|
||
|
|
<h3 id="wrapping-the-search-around">Wrapping the search around</h3>
|
||
|
|
<p>When searching Vim normally stops at the end (forward searches) or beginning (reverse searches) of the file. With the <strong>wrapscan</strong> option searches wrap around.</p>
|
||
|
|
<pre><code>set wrapscan</code></pre>
|
||
|
|
<p>The abbreviation is <strong>se ws</strong> and the option is turned off with <strong>set nowrapscan</strong> or <strong>se nows</strong>.</p>
|
||
|
|
<p>As the search wraps a message is displayed in the status line.</p>
|
||
|
|
<h3 id="highlighting-the-search">Highlighting the search</h3>
|
||
|
|
<p>Vim can be configured to highlight all occurrences of the search pattern with the command:</p>
|
||
|
|
<pre><code>set hlsearch</code></pre>
|
||
|
|
<p>The abbreviation is <strong>se hls</strong> and the option is turned off with <strong>set nohlsearch</strong> or <strong>se nohls</strong>.</p>
|
||
|
|
<p>The highlight stays in effect until cancelled, which can get a little tedious, so Vim allows the current pattern match to be turned off with the command <strong>:nohlsearch</strong> (abbreviated to <strong>:nohl</strong>).</p>
|
||
|
|
<h3 id="enable-extra-features-in-insert-mode">Enable extra features in INSERT mode</h3>
|
||
|
|
<p>Vim allows more functionality when in <em>Insert</em> mode than vi. It is possible to work in <em>Insert</em> mode most of the time such as in editors such as <em>Nano</em>. Enabling these features is done with the <strong>set backspace</strong> option. This is followed by a list of up to three items separated by commas:</p>
|
||
|
|
<ul>
|
||
|
|
<li><strong>indent</strong> - allows backspacing over auto indents (not covered yet in this series)</li>
|
||
|
|
<li><strong>eol</strong> - allows backspacing over line breaks (thus permitting inserted lines to be joined)</li>
|
||
|
|
<li><strong>start</strong> - allows backspacing over the start of the insert to previously existing text</li>
|
||
|
|
</ul>
|
||
|
|
<p>To get the full functionality of Vim it is probably wise to use all three items:</p>
|
||
|
|
<pre><code>set backspace=indent,eol,start</code></pre>
|
||
|
|
<p>The abbreviation is <strong>se bs=indent,eol,start</strong>.</p>
|
||
|
|
<hr />
|
||
|
|
<h2 id="summary">Summary</h2>
|
||
|
|
<ul>
|
||
|
|
<li>Movement
|
||
|
|
<ul>
|
||
|
|
<li><strong>)</strong>, <strong>(</strong> move forward and backward by sentences</li>
|
||
|
|
<li><strong>}</strong>, <strong>{</strong> move forward and backward by paragraphs</li>
|
||
|
|
<li><strong>G</strong>, <strong>gg</strong> move to a specific line or beginning or end of file</li>
|
||
|
|
<li><strong>%</strong> move between matching pairs of characters</li>
|
||
|
|
</ul></li>
|
||
|
|
<li>Searching
|
||
|
|
<ul>
|
||
|
|
<li><strong>/</strong> to search forward</li>
|
||
|
|
<li><strong>?</strong> to search backwards</li>
|
||
|
|
</ul></li>
|
||
|
|
<li>Changing
|
||
|
|
<ul>
|
||
|
|
<li><strong>a</strong> and <strong>A</strong> to append text</li>
|
||
|
|
<li><strong>i</strong> and <strong>I</strong> to insert text</li>
|
||
|
|
<li><strong>o</strong> and <strong>O</strong> to open a new line and insert text</li>
|
||
|
|
<li><strong>x</strong> and <strong>X</strong> to delete characters</li>
|
||
|
|
<li><strong>dd</strong> and <strong>D</strong> to delete lines</li>
|
||
|
|
<li><strong>d</strong><em>motion</em> to delete up to a movement target</li>
|
||
|
|
<li><strong>s</strong> and <strong>S</strong> to change characters</li>
|
||
|
|
<li><strong>cc</strong> and <strong>C</strong> to change lines</li>
|
||
|
|
<li><strong>c</strong><em>motion</em> to change up to a movement target</li>
|
||
|
|
</ul></li>
|
||
|
|
</ul>
|
||
|
|
<h3 id="configuration-file-1">Configuration file</h3>
|
||
|
|
<pre><code>" Ensure Vim runs as Vim
|
||
|
|
set nocompatible
|
||
|
|
|
||
|
|
" Keep a backup file
|
||
|
|
set backup
|
||
|
|
|
||
|
|
" Keep change history
|
||
|
|
set undodir=~/.vim/undodir
|
||
|
|
set undofile
|
||
|
|
|
||
|
|
" Show the line,column and the % of buffer
|
||
|
|
set ruler
|
||
|
|
|
||
|
|
" Always show a status line per window
|
||
|
|
set laststatus=2
|
||
|
|
|
||
|
|
" Show Insert, Replace or Visual on the last line
|
||
|
|
set showmode
|
||
|
|
|
||
|
|
" Stop beeping! (Flash the screen instead)
|
||
|
|
set visualbell
|
||
|
|
|
||
|
|
" Show incomplete commands
|
||
|
|
set showcmd
|
||
|
|
|
||
|
|
" Increase the command history
|
||
|
|
set history=100
|
||
|
|
|
||
|
|
" Turn off case in searches
|
||
|
|
set ignorecase
|
||
|
|
|
||
|
|
" Turn case-sensitive searches back on if there are capitals in the target
|
||
|
|
set smartcase
|
||
|
|
|
||
|
|
" Do incremental searching
|
||
|
|
set incsearch
|
||
|
|
|
||
|
|
" Set the search scan to wrap around the file
|
||
|
|
set wrapscan
|
||
|
|
|
||
|
|
" Highlight all matches when searching
|
||
|
|
set hlsearch
|
||
|
|
|
||
|
|
" Allow extra movement in INSERT mode
|
||
|
|
set backspace=indent,eol,start</code></pre>
|
||
|
|
<h2 id="links">Links</h2>
|
||
|
|
<ol type="1">
|
||
|
|
<li>Vim Help:
|
||
|
|
<ul>
|
||
|
|
<li>Using Help: <a href="http://vimdoc.sourceforge.net/htmldoc/helphelp.html">http://vimdoc.sourceforge.net/htmldoc/helphelp.html</a></li>
|
||
|
|
<li>Motion: <a href="http://vimdoc.sourceforge.net/htmldoc/motion.html">http://vimdoc.sourceforge.net/htmldoc/motion.html</a></li>
|
||
|
|
<li>Searching: <a href="http://vimdoc.sourceforge.net/htmldoc/pattern.html">http://vimdoc.sourceforge.net/htmldoc/pattern.html</a></li>
|
||
|
|
<li>Insertion: <a href="http://vimdoc.sourceforge.net/htmldoc/insert.html">http://vimdoc.sourceforge.net/htmldoc/insert.html</a></li>
|
||
|
|
<li>Changing: <a href="http://vimdoc.sourceforge.net/htmldoc/change.html">http://vimdoc.sourceforge.net/htmldoc/change.html</a></li>
|
||
|
|
<li>Options: <a href="http://vimdoc.sourceforge.net/htmldoc/options.html">http://vimdoc.sourceforge.net/htmldoc/options.html</a></li>
|
||
|
|
</ul></li>
|
||
|
|
<li>Graphical Cheat Sheet: <a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html">http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html</a></li>
|
||
|
|
<li>Vim Hints Episode 3 <a href="https://hackerpublicradio.org/eps/hpr1734">https://hackerpublicradio.org/eps/hpr1734</a></li>
|
||
|
|
</ol>
|
||
|
|
<!--
|
||
|
|
vim: syntax=markdown:ts=8:sw=4:ai:et:tw=78:fo=tcqn:fdm=marker
|
||
|
|
-->
|
||
|
|
</article>
|
||
|
|
</main>
|
||
|
|
</div>
|
||
|
|
</body>
|
||
|
|
</html>
|