Permalink
Browse files

Final pass over Navigation chapter before release.

  • Loading branch information...
1 parent 84d7fdb commit 1f3b741cd56d04862f68fe3acc89ca079b75a81c @runpaint committed May 2, 2009
View
1 TODO
@@ -1,6 +1,7 @@
* Note that arguments to :set should have spaces backslash escaped.
* Note on bias toward POSIX systems...
* Note <tt>vim</tt> is synonymous with <tt>gvim</tt> unless otherwise stated.
+ * Move Editing: Searching and Replacing to Searching
CONVENTIONS
* :w[rite] style
* Meaning of ! suffix
View
@@ -0,0 +1 @@
+<h2>Navigation</h2>
@@ -0,0 +1,39 @@
+<h3 id="basic-navigation">Basic Navigation</h3>
+
+<h4>Problem</h4>
+
+<p>You want to move around inside a file.</p>
+
+<h4>Solution</h4>
+
+<p>The traditional approach is to use the arrow keys to move up, down, left,
+and right. Vim supports that style of navigation but also offers a more
+efficient alternative:</p>
+
+<table>
+ <tr><th>Key</th> <th>Movement</th></tr>
+ <tr><td><kbd>h</kbd></td> <td>Left</td></tr>
+ <tr><td><kbd>l</kbd></td> <td>Right</td></tr>
+ <tr><td><kbd>k</kbd></td> <td>Up a line</td></tr>
+ <tr><td><kbd>j</kbd></td> <td>Down a line</td></tr>
+ <tr><td><kbd>0</kbd></td> <td>Start of line</td></tr>
+ <tr><td><kbd>^</kbd></td> <td>First character of line</td><tr>
+ <tr><td><kbd>$</kbd></td> <td>End of line</td></tr>
+</table>
+
+<h4>Discussion</h4>
+
+<p>It is tempting to rely on old habits for something as basic as moving
+around in a file. Once you're used to The Vim Way, however, you'll find
+yourself much more efficient. One reason for this is that these keys are all
+located on the main keyboard, so you don't need to stretch to the arrow keys,
+hence breaking your flow.</p>
+
+<p>Another benefit is that you can prefix these shortcuts with <i>counts</i> (as
+you can with many Vim commands) which specify how many times they should be
+executed. For instance, <kbd>2k</kbd> moves up two lines. </p>
+
+<p>Once you've become used to these keys, take a look at <i>motions</i> and
+<i>text objects</i> in <a href="#selecting-text-with-motions">Selecting Text
+ with Motions</a> to make the humble combination of <kbd>h</kbd>,
+<kbd>l</kbd>, <kbd>k</kbd>, and <kbd>j</kbd> more powerful still.</p>
@@ -1,4 +1,4 @@
-<h3>Navigating Text Files</h3>
+<h3 id="navigating-text-files">Navigating Text Files</h3>
<h4>Problem</h4>
@@ -7,61 +7,61 @@
<h4>Solution</h4>
-<p>In normal mode you can use the following shorcuts:</p>
+<p>In <i>Normal</i> mode you can use the following shortcuts:</p>
<table>
<tr>
<th>Key</th>
<th>Move To</th>
</tr>
<tr>
- <td><tt>{</tt></td>
+ <td><kbd>{</kbd></td>
<td>Beginning of current paragraph</td>
</tr>
<tr>
- <td><tt>}</tt></td>
+ <td><kbd>}</kbd></td>
<td>End of current paragraph</td>
</tr>
<tr>
- <td><tt>(</tt></td>
+ <td><kbd>(</kbd></td>
<td>Beginning of current sentence</td>
</tr>
<tr>
- <td><tt>)</tt></td>
+ <td><kbd>)</kbd></td>
<td>End of current sentence</td>
</tr>
<tr>
- <td><tt>w</tt></td>
+ <td><kbd>w</kbd></td>
<td>Beginning of next word</td>
</tr>
<tr>
- <td><tt>b</tt></td>
+ <td><kbd>b</kbd></td>
<td>Beginning of the previous word</td>
</tr>
<tr>
- <td><tt>e</tt></td>
+ <td><kbd>e</kbd></td>
<td>End of the word</td>
</tr>
</table>
<h4>Discussion</h4>
<p>The shortcuts above all rely on the fact that plain text is often very
-structured. A document consists of paragraphs seperated by newlines.
+structured. A document consists of paragraphs separated by newlines.
Paragraphs contain one or more sentences which begin with capital letters and
-end with periods. Sentences are collections of words which are seperated by
+end with periods. Sentences are collections of words which are separated by
spaces.</p>
<p>After using one of these shortcuts you may want to return to where you were
previously. This is particularly useful if you're writing one thing and are
then reminded that you now have to change another: you use a shortcut to jump
to the location of the change, then want to resume where you left of. You can
-use the <tt>g,</tt> (mnemonic: <i>g</i>o back to where I paused (commas can be
-used to represent pauses...)) command. Each time you execute it you'll be
+use the <kbd>g,</kbd> (mnemonic: <i>g</i>o back to where I paused (commas can be
+used to represent pauses&hellip;)) command. Each time you execute it you'll be
taken back another step. To move in the other direction (towards more recent
-changes), use <tt>g;</tt>.</p>
+changes), use <kbd>g;</kbd>.</p>
<p>All of these shortcuts can be combined. So, to move to the end of the previous
-word you type <tt>be</tt>. You can also prefix them with numbers to indicate
-how many times they should be executed. <tt>3b</tt> moves to the beginning of
+word you type <kbd>be</kbd>. You can also prefix them with numbers to indicate
+how many times they should be executed: <kbd>3b</kbd> moves to the beginning of
the 3rd previous word.</p>
@@ -1,80 +1,78 @@
-<h3>Navigating Source Code</h3>
+<h3 id="navigating-source-code">Navigating Source Code</h3>
<h4>Problem</h4>
-<p>You're editing the source code for a computer program, and want to navigate it
-efficiently.</p>
+<p>You're editing the source code for a computer program, and want to navigate
+it efficiently.</p>
<h4>Solution</h4>
-<p><span class="todo">Add intro paragraph here; the bare table isn't very
-welcoming</span></p>
+<p>The commands below allow movement around source code in the context of
+typical constructs. For instance, <kbd>%</kbd> finds the end of the
+<code>if</code>/<code>elsif</code>/<code>else</code> block, or a comment, your
+cursor is over, and moves you to the end of it.</p>
<table>
<tr>
<th>Key</th>
<th>Move To</th>
</tr>
<tr>
- <td><tt>%</tt></td>
+ <td><kbd>%</kbd></td>
<td>End of construct <span class="fn">A construct is a bracket pair, an
- <code>if</code>/<code>elsif</code>/<code>else</code> block, or a comment. For example, if you hit <tt>%</tt> on
+ <code>if</code>/<code>elsif</code>/<code>else</code> block, or a comment.
+ For example, if you hit <kbd>%</kbd> on
an opening bracket you'd jump to the corresponding closing bracket. See
<tt>:help %</tt> for more details.</span></td>
</tr>
<tr>
- <td><tt>[[</tt></td>
+ <td><kbd>[[</kbd></td>
<td>Backwards to the beginning of the current function.</td>
</tr>
<tr>
- <td><tt>][</tt></td>
+ <td><kbd>][</kbd></td>
<td>Forwards to the beginning of the current function.</td>
</tr>
<tr>
- <td><tt>}[</tt></td>
+ <td><kbd>}[</kbd></td>
<td>Beginning of the current block.</td>
</tr>
<tr>
- <td><tt>}]</tt></td>
+ <td><kbd>}]</kbd></td>
<td>End of the current block.</td>
</tr>
<tr>
- <td><tt>}[</tt></td>
+ <td><kbd>}[</kbd></td>
<td>Beginning of the current comment block.</td>
</tr>
<tr>
- <td><tt>}]</tt></td>
+ <td><kbd>}]</kbd></td>
<td>End of the current comment block.</td>
</tr>
<tr>
- <td><tt>gd</tt></td>
+ <td><kbd>gd</tt></td>
<td>First usage of the current variable name <span class="fn">Ocurrences
in comments are ignored.</span>. (Mnemonic: <i>g</i>o to
<i>d</i>efinition).</td>
</tr>
<tr>
- <td><tt>gD</tt></td>
- <td>Go to the first <em>global</em> <span class="fn"><tt>gd</tt> looks for
+ <td><kbd>gD</kbd></td>
+ <td>Go to the first <em>global</em> <span class="fn"><kbd>gd</kbd> looks for
the definition closest to your current position, thus respecting the
- lexical scoping rules of many languages. <tt>gD</tt> starts searching
+ lexical scoping rules of many languages. <kbd>gD</kbd> starts searching
from the first line of the file, so prefers variables with a global
scope.</span> usage of the current variable name.</td>
</tr>
</table>
-<p><span class="todo">Sidebar: What is a Function</span></p>
-
<h4>Discussion</h4>
<p>The shortcuts<span class="fn">See recipe~\ref{sec:nav-text}</span> available
for text files are supported for source code, too, so review them if you
haven't already.</p>
<p>There's a lot to remember here. Despite my best efforts, I suspect that the
-descriptions above are still confusing. To understand these shortucts you
+descriptions above are still confusing. To understand these shortcuts you
really need to try them yourself. Open some source code written in your
-favourite language with Vim, make sure that syntax highlighting is on <span
- class="todo">link to relevant recipe</span>, and bounce back and forth
-between those braces.</p>
-
-<p><span class="todo">link to other programming-related recipes</span></p>
+favourite language with Vim, make sure that syntax highlighting is on
+(<tt>:syntax on</tt>), and bounce back and forth between those braces.</p>
@@ -0,0 +1,32 @@
+<h3 id="navigating-the-viewport">Navigating the Viewport</h3>
+
+<h4>Problem</h4>
+
+<p>You want to scroll through a document in <i>screenfulls</i>; not line by
+line.</p>
+
+<h4>Solution</h4>
+
+<table>
+ <tr><th>Command</th> <th>Scroll To</th></tr>
+ <tr><td><kbd>H</kbd></td> <td>Top of the screen. (Mnemonic:
+ <i>H</i>ome).</td></tr>
+ <tr><td><kbd>L</kbd></td> <td>Bottom of the screen. (Mnemonic:
+ <i>L</i>ower).</td></tr>
+ <tr><td><kbd>M</kbd></td> <td>Middle of the screen. (Mnemonic:
+ <i>M</i>iddle).</td></tr>
+ <tr><td><kbd>gg</kbd></td> <td>Top of file.</td></tr>
+ <tr><td><kbd>G</kbd></td> <td>Bottom of file.</td></tr>
+</table>
+
+<h4>Discussion</h4>
+
+<p>If you're file is longer than the height of your window, you'll need a way of
+scrolling the portion that appears on the screen.</p>
+
+<p>The area of the screen displaying a file is called the <i>viewport</i>. As we
+scroll down the document using <kbd>j</kbd>, the viewport updates to hide the line at
+the top, and show a new one at the bottom. Scrolling through a long document
+this way is inefficient and unhealthy for our fingers. Instead we can scroll
+one screenfull at a time, which severely reduces the amount of key presses
+involved.</p>
@@ -1,12 +1,18 @@
-<h3>Navigating Buffers</h3>
+<h3 id="navigating-buffers">Navigating Buffers</h3>
<h4>Problem</h4>
-<p>You have multiple files open and want to navigate between them.
-<span class="todo">Write recipe for using buffers?</span></p>
+<p>You have multiple files open and want to navigate between them.</p>
<h4>Solution</h4>
+<p>Buffers allow you to have a collection of files open with only one
+displayed at a time. They are particularly useful for processing a set of
+files sequentially, whereby you operate on one file, then switch to the buffer
+containing the next. If you'd rather have the open files listed along the top
+of the screen, consider using <a href="#navigating-tabs"><i>tabs</i></a>
+instead.</p>
+
<table>
<tr>
<th>Command</th>
@@ -57,7 +63,4 @@
<p>To quickly navigate between buffers its common to map <tt>:bnext</tt> and/or
<tt>:bprev</tt> to a key. For example <tt>map &lt;F6&gt; :bn&lt;CR&gt;</tt> lets you hit
-<kbd>F6</kbd> to cycle through the open buffers.</p>
-
-<p><span class="todo">If Vim opens a filelist one in each buffer, as I believe,
-mention that here. Use of :next, etc.</span></p>
+<kbd>&lt;F6&gt;</kbd> to cycle through the open buffers.</p>
@@ -0,0 +1,81 @@
+<h3 id="navigating-tabs">Navigating Tabs</h3>
+
+<h4>Problem</h4>
+
+<p>You want to group your windows into multiple, logical groups.</p>
+
+<p>When you have a lot of open windows/buffers it can be tricky to navigate
+between them. Often it makes more sense to group them into logical
+<i>tabs</i>, so you can switch between them easier, and operate on them as a
+group.</p>
+
+<p>For example, if you were using Vim to edit a website, you may have your CSS
+files open in one tab, the HTML files in another, and a HTML reference guide
+in the third. </p>
+
+<h4>Solution</h4>
+
+<p>Use <i>tabs</i>.</p>
+
+<table>
+ <tr>
+ <th>Command</th>
+ <th>Action</th>
+ </tr>
+ <tr>
+ <td><tt>:tabedit <var>[file]</var></tt></td>
+ <td>Open a new tab. If the optional file is supplied, that file is opened
+ in the new tab.</td>
+ </tr>
+ <tr>
+ <td><tt>:tabclose</tt></td>
+ <td>Close the current tab.</td>
+ </tr>
+ <tr>
+ <td><tt>:tabnext <var>n</var></tt></td>
+ <td rowspan="2">Go to next tab, or the <var>n</var><sup>th</sup></td>
+ </tr>
+ <tr>
+ <td><tt><var>n</var>gt</tt></td>
+ </tr>
+ <tr>
+ <td><tt>:tabs</tt></td>
+ <td>Show a list of the open tabs.</td>
+ </tr>
+ <tr>
+ <td><tt>:tabprevious <var>n</var></tt></td>
+ <td rowspan="2">Go to previous tab, or the <var>n<sup>th</sup></var></td>
+ </tr>
+ <tr>
+ <td><tt><var>n</var>gT</tt></td>
+ </tr>
+ <tr>
+ <td><tt>:tabdo <var>cmd</var></tt></td>
+ <td>Executes <var>cmd</var> in each open tab, aborting on the first
+ error.</td>
+ </tr>
+</table>
+
+<h4>Discussion</h4>
+
+<p>You can use Vim's tabs like those in Firefox&reg; and Opera&reg;, by
+opening one file in each tab, then switching between them. Vim enables you to
+extend this concept, however, by allowing multiple files to be opened in the
+same tab.</p>
+
+<p>When you open a tab, a <i>tabline</i> appears along the top of the screen,
+which lists the open tabs. In GVim you can switch to another tab, close
+existing tabs, and open new tabs, by clicking the tabline with the mouse.</p>
+
+<p>You can either cycle through open tabs using <kbd>gt</kbd>, or go directly
+to a specific tab by prefixing <kbd>gt</kbd> with its number. Tabs are
+numbered starting with 1, so to switch to the 3<sup>rd</sup> tab on the
+tabline, say, you'd use <kbd>3gt</kbd>. If you have a lot of tabs, their
+numbers may not be obvious. In this case, use <tt>:tabs</tt> to find them.</p>
+
+<p>The power of tabs comes from executing commands on the windows they contain
+as a logical group. Continuing the above example, this would let you perform a
+<a href="#searching-and-replacing">search and replace</a> all HTML files. For
+example, if you were in the HTML tab, you could say <tt>:windo
+ s/&lt;foo&gt;/&lt;bar&gt;/g</tt>, and all of your HTML files would have
+their &lt;foo&gt;s replaced with &lt;bar&gt;s.</p>
Oops, something went wrong.

0 comments on commit 1f3b741

Please sign in to comment.