📄 ch08_01.htm
字号:
<html><head><title>The vi Editor (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 8. The vi Editor</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch08_01.htm#sect1">Review of vi Operations</a><br /><a href="ch08_02.htm">Movement Commands</a><br /><a href="ch08_03.htm">Edit Commands</a><br /><a href="ch08_04.htm">Saving and Exiting</a><br /><a href="ch08_05.htm">Accessing Multiple Files</a><br /><a href="ch08_06.htm">Interacting with Unix</a><br /><a href="ch08_07.htm">Macros</a><br /><a href="ch08_08.htm">Miscellaneous Commands</a><br /><a href="ch08_09.htm">Alphabetical List of Keys</a><br /><a href="ch08_10.htm">Setting Up vi</a><br /></p></div><p><a name="unut-ch-8-ix-vi-editor" />This chapter presents the following topics:</p><ul><li><p>Review of <tt class="literal">vi</tt> operations</p></li><li><p>Movement commands</p></li><li><p>Edit commands</p></li><li><p>Saving and exiting</p></li><li><p>Accessing multiple files</p></li><li><p>Interacting with Unix</p></li><li><p>Macros</p></li><li><p>Miscellaneous commands</p></li><li><p>Alphabetical list of keys</p></li><li><p>Setting up <tt class="literal">vi</tt></p></li></ul><p><tt class="literal">vi</tt> is pronounced “vee eye.”</p><p>Besides the original Unix <tt class="literal">vi</tt>,there are a number of freely available <tt class="literal">vi</tt> clones.Both the original <tt class="literal">vi</tt> and the clones are covered in<em class="citetitle">Learning the vi Editor</em>, listed in the <a href="biblio.htm">Bibliography</a>.</p><div class="sect1"><a name="unut-ch-8-sect-1" /><h2 class="sect1"><a name="sect1" />8.1. Review of vi Operations</h2><p>This section provides a review of the following:</p><ul><li><p>Command-line syntax</p></li><li><p><tt class="literal">vi</tt> modes</p></li><li><p>Syntax of <tt class="literal">vi</tt> commands</p></li><li><p>Status-line commands</p></li></ul><a name="unut-ch-8-sect-1.1" /><div class="sect2"><h3 class="sect2">8.1.1. Command-Line Syntax</h3><p>The three most common ways of starting a <tt class="literal">vi</tt> session are:</p><blockquote><pre class="code">vi <em class="replaceable"><tt>file</tt></em>vi +<em class="replaceable"><tt>n file</tt></em>vi +/<em class="replaceable"><tt>pattern file</tt></em></pre></blockquote><p>You can open <em class="emphasis">file</em> for editing,optionally at line <em class="emphasis">n</em> or at thefirst line matching <em class="emphasis">pattern</em>.If no <em class="emphasis">file</em> is specified,<tt class="literal">vi</tt> opens with an empty buffer.See <a href="ch02_01.htm">Chapter 2</a>,for more information on command-line options for<tt class="literal">vi</tt>.</p><p>Note that <tt class="literal">vi</tt> and <tt class="literal">ex</tt> are actuallythe same program; thus it is worthwhile to review the material in<a href="ch09_01.htm">Chapter 9</a>,as well,in order to become familiar with the <tt class="literal">ex</tt> command set.</p></div><a name="unut-ch-8-sect-1.2" /><div class="sect2"><h3 class="sect2">8.1.2. Command Mode</h3><p><a name="IXT-8-123153" />Once the file is opened, you are in command mode.From command mode, you can:</p><ul><li><p>Invoke insert mode</p></li><li><p>Issue editing commands</p></li><li><p>Move the cursor to a different position in the file</p></li><li><p>Invoke <tt class="literal">ex</tt> commands</p></li><li><p>Invoke a Unix shell</p></li><li><p>Save or exit the current version of the file</p></li></ul></div><a name="unut-ch-8-sect-1.3" /><div class="sect2"><h3 class="sect2">8.1.3. Insert Mode</h3><p><a name="IXT-8-123154" />In insert mode, you can enter new text in the file.Press the Escape key to exit insert mode and return to command mode.The following commands invoke insert mode: </p><table border="1" cellpadding="3"><tr><td><tt class="literal">a</tt></td><td>Append after cursor.</td></tr><tr><td><tt class="literal">A</tt></td><td>Append at end of line.</td></tr><tr><td><tt class="literal">c</tt></td><td>Begin change operation.</td></tr><tr><td><tt class="literal">C</tt></td><td>Change to end of line.</td></tr><tr><td><tt class="literal">i</tt></td><td>Insert before cursor.</td></tr><tr><td><tt class="literal">I</tt></td><td>Insert at beginning of line.</td></tr><tr><td><tt class="literal">o</tt></td><td>Open a line below current line.</td></tr><tr><td><tt class="literal">O</tt></td><td>Open a line above current line.</td></tr><tr><td><tt class="literal">R</tt></td><td>Begin overwriting text.</td></tr><tr><td><tt class="literal">s</tt></td><td>Substitute a character.</td></tr><tr><td><tt class="literal">S</tt></td><td>Substitute entire line.</td></tr></table><p></div><a name="unut-ch-8-sect-1.4" /><div class="sect2"><h3 class="sect2">8.1.4. Syntax of vi Commands</h3><p><a name="unut-ch-8-ix-commands-vi-editor" />In <tt class="literal">vi</tt>, commands have the following general form:</p><p>[<em class="replaceable"><tt>n</em>] <em class="replaceable">operator</em> [<em class="replaceable">m</em>] <em class="replaceable">object</tt></em></p><p>The basic editing <em class="emphasis">operators</em> are:</p><table border="1" cellpadding="3"><tr><td><tt class="literal">c</tt></td><td>Begin a change.</td></tr><tr><td><tt class="literal">d</tt></td><td>Begin a deletion.</td></tr><tr><td><tt class="literal">y</tt></td><td>Begin a yank (or copy).</td></tr></table><p><p>If the current line is the object of the operation, the objectis the same as the operator: <tt class="literal">cc</tt>, <tt class="literal">dd</tt>, <tt class="literal">yy</tt>. Otherwise, the editing operators act on objects specified by cursor-movement commands orpattern-matching commands.<em class="emphasis">n</em> and <em class="emphasis">m</em> are the number of times the operation is performed, or thenumber of objects the operation is performed on. If both <em class="emphasis">n</em> and <em class="emphasis">m</em>are specified, the effect is <em class="emphasis">n</em> × <em class="emphasis">m</em>. </p><p>An object can represent any of the following text blocks:</p><table border="1" cellpadding="3"><tr><td><em class="emphasis">word</em></td><td><p>Includes characters up to a whitespace character(space or tab)or punctuation mark.A capitalized object is a variant form that recognizesonly whitespace.</p></td></tr><tr><td><em class="emphasis">sentence</em></td><td>Is up to <tt class="literal">.</tt>, <tt class="literal">!</tt>, or <tt class="literal">?</tt>, followed by two spaces.</td></tr><tr><td><em class="emphasis">paragraph</em></td><td><p>Is up to next blank line or paragraph macro defined bythe <tt class="literal">para=</tt> option.</p></td></tr><tr><td><em class="emphasis">section</em></td><td>Is up to next section heading defined by the <tt class="literal">sect=</tt> option.</td></tr></table><p><a name="unut-ch-8-sect-1.4.1" /><div class="sect3"><h3 class="sect3">8.1.4.1. Examples</h3><table border="1" cellpadding="3"><tr><td><tt class="literal">2cw</tt></td><td>Change the next two words.</td></tr><tr><td><tt class="literal">d}</tt></td><td>Delete up to next paragraph.</td></tr><tr><td><tt class="literal">d^</tt></td><td>Delete back to beginning of line.</td></tr><tr><td><tt class="literal">5yy</tt></td><td>Copy the next five lines.</td></tr><tr><td><tt class="literal">y]]</tt></td><td>Copy up to the next section.</td></tr></table><p></div></div><a name="unut-ch-8-sect-1.5" /><div class="sect2"><h3 class="sect2">8.1.5. Status-Line Commands</h3><p>Most commands are not echoed on the screen as you input them.However, the status line at the bottom of the screen is usedto echo input for these commands:</p><table border="1" cellpadding="3"><tr><td><tt class="literal">/</tt></td><td>Search forward for a pattern.</td></tr><tr><td><tt class="literal">?</tt></td><td>Search backward for a pattern.</td></tr><tr><td><tt class="literal">:</tt></td><td>Invoke an <tt class="literal">ex</tt> command.</td></tr><tr><td><tt class="literal">!</tt></td><td><p>Invoke a Unix command that takes as its inputan object in the buffer and replaces it with outputfrom the command.</p></td></tr></table><p><p>Commands that are input on the status line must be entered bypressing the Return key.In addition, error messages and output from the <tt class="literal">CTRL-G</tt> command aredisplayed on the status line.</p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">7.4. Summary of Commands by Name</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.2. Movement Commands</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2003</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"> </map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -