309-312.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 151 行

HTML
151
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Text Editors: vi and emacs</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=16//-->

<!--PAGES=309-312//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="307-309.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="312-315.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Searching and Replacing Text</FONT></H4>

<P>Text searches in <TT>vi</TT> can be performed in either direction: forward or backward. Searches are always started from the current cursor location and continue from the top or bottom of the file, depending on which direction you use. In other words, searches &#147;word wrap&#148; the file.</P>

<P>You can use your file <TT>asong</TT> to illustrate searches. To search forward through <TT>asong</TT> for the word &#147;bay,&#148; type</P>

<!-- CODE SNIP //-->

<PRE>

/bay

</PRE>

<!-- END CODE SNIP //-->

<P>and press Return. Notice that this is a status-line command. The command <TT>/bay</TT> is echoed on the status line and the cursor is moved to the first occurrence it finds in the forward direction of the string &#147;bay.&#148; Interested in finding another instance of &#147;bay&#148;? Enter a <TT>/</TT> character. This command continues the search for &#147;bay&#148; in the forward direction and places the cursor at the next instance of &#147;bay.&#148; Each time you enter the <TT>/</TT> key, <TT>vi</TT> tries to find an instance of the previous string pattern. When it reaches the end of the file, <TT>vi</TT> loops back and continues its search at the start of the file.</P>

<P>You can also search backward for strings in <TT>vi</TT> by using the <TT>?</TT> command. It works in exactly the same manner as the <TT>/</TT> command, but in the opposite direction. Try it out by entering</P>

<!-- CODE SNIP //-->

<PRE>

?I

</PRE>

<!-- END CODE SNIP //-->

<P>in <TT>asong</TT>, instructing <TT>vi</TT> to search back for instances of &#147;I.&#148; This search can be repeated by typing <TT>?</TT>, as you might have suspected. You can continue a search by pressing <TT>n</TT>, which always continues a search in the same direction as the previous search. However, typing <TT>N</TT> uses the same search string but in the opposite direction.</P>

<P>As mentioned earlier, searches can be made very powerful through the use of regular expressions. The search command is supplied in the same fashion as described before (<TT>/</TT> or <TT>?</TT>), but square brackets are added to instruct <TT>vi</TT> to do a regular expression expansion of the enclosed characters. For example, search forward through <TT>asong</TT> from the first line for all strings containing the substring &#147;er.&#148; Type</P>

<!-- CODE SNIP //-->

<PRE>

/[*]er[*]

</PRE>

<!-- END CODE SNIP //-->

<P><TT>vi</TT>&#146;s first matching string arrives at &#147;Where.&#148; If you type <TT>n</TT>, <TT>vi</TT> moves the cursor to &#147;watermelon,&#148; and so on. You can also specify collections of characters or ranges of characters to match. Try typing the following:</P>

<!-- CODE SNIP //-->

<PRE>

/[a-z]y

</PRE>

<!-- END CODE SNIP //-->

<P>This command used in <TT>asong</TT> finds the strings &#147;by&#148; and &#147;my,&#148; as well as any word with these strings inside them (such as &#147;bay&#148;). This works because the range of characters given are treated as an enumerated range of ASCII values. Thus, you could also include a range of numbers (for example, 0-9). Now try the following command:</P>

<!-- CODE SNIP //-->

<PRE>

/[Mm]y

</PRE>

<!-- END CODE SNIP //-->

<P>This locates the strings &#147;My&#148; and &#147;my.&#148;

</P>

<P>In <TT>vi</TT>, searches without regular expressions find only exact matches of the supplied pattern (including the case of the letters in the pattern). Clearly, regular expressions can be used to enhance many types of searches in which you may not know exactly how a pattern appears in a file.</P>

<P>One of the more common applications of a search is to replace instances of one word (or pattern) with another. This is done with an <TT>ex</TT> command that starts with a colon. To search the entire <TT>asong</TT> file for the string &#147;Down&#148; and replace it with the string &#147;Up,&#148; type</P>

<!-- CODE SNIP //-->

<PRE>

:%s/Down/Up/g

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>s</TT> indicates that this is a search operation, the <TT>%</TT> means that the entire file is to be searched, &#147;Down&#148; is the pattern to be found, &#147;Up&#148; is the new pattern, and the <TT>g</TT> tells <TT>vi</TT> that the search should continue until there are no more pattern matches. Without the <TT>g</TT>, <TT>vi</TT> would perform the replacement on only the first match it finds. This command also works with regular expressions appearing in the search pattern and the replacement pattern.</P>

<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Setting Preferences</FONT></H4>

<P><TT>vi</TT> is <I>configurable,</I> which means that you can set options to control your editing environment. These options are initialized with default values that you can modify in <TT>vi</TT> at any time. <TT>vi</TT> is configured using the <TT>set</TT> command. The <TT>set</TT> command must be preceded by a colon and entered by pressing Return. For example, to display line numbers in the editor, enter</P>

<!-- CODE SNIP //-->

<PRE>

:set number

</PRE>

<!-- END CODE SNIP //-->

<P>The following table describes a few of the more common <TT>set</TT> commands.</P>

<CENTER>

<TABLE WIDTH="80%"><TR>

<TD WIDTH="30%" VALIGN="TOP">all

<TD WIDTH="70%">Displays a list of all available <TT>set</TT> options and their current status

<TR>

<TD VALIGN="TOP">errorbells

<TD>Sounds the terminal bell when an error occurs

<TR>

<TD>ignorecase

<TD>Searches are case-insensitive

<TR>

<TD VALIGN="TOP">number

<TD>Displays line numbers in the leftmost column of the screen (these are not written to the file)

<TR>

<TD VALIGN="TOP">showmode

<TD>An indicator appears at the bottom right of the screen if you are in input mode, change mode, replace mode, and so on

<TR>

</TABLE>

</CENTER>

<P><TT>set</TT> commands that do not take a value can be switched off by inserting a &#147;no&#148; as a prefix to the set parameter. For example, the command</P>

<!-- CODE SNIP //-->

<PRE>

:set nonumber

</PRE>

<!-- END CODE SNIP //-->

<P>switches line numbering off. The command

</P>

<!-- CODE SNIP //-->

<PRE>

:set

</PRE>

<!-- END CODE SNIP //-->

<P>shows only the options that you have changed.

</P>

<P>The settings that you use in a <TT>vi</TT> session are (unfortunately) lost each time you exit <TT>vi</TT>. If you do not like the idea of resetting these options each time you use <TT>vi</TT>, there is an easier way to perform this initialization. Use the <TT>vi</TT> initialization file called <TT>.exrc</TT>. <TT>vi</TT> searches for this file in your home directory each time it is invoked. If it can&#146;t find this file, it uses the defaults set within the <TT>vi</TT> program. As you will see in the following example, the <TT>.exrc</TT> file can also be used to define <TT>vi</TT> macros.</P>

<P>A sample <TT>.exrc</TT> file looks something like this:</P>

<!-- CODE SNIP //-->

<PRE>

set number

set errorbells

set showmode

</PRE>

<!-- END CODE SNIP //-->

<P>Note that the colon is not required before a <TT>set</TT> command in an <TT>.exrc</TT> file.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="307-309.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="312-315.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?