⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 551-553.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Linux, Fourth Edition:Using the emacs Editor</TITLE>

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

 -->




<!--ISBN=0789717468//-->

<!--TITLE=Special Edition Using Linux, Fourth Edition//-->

<!--AUTHOR=Jack Tackett//-->

<!--AUTHOR=Jr.//-->

<!--AUTHOR=Steve Burnett//-->

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

<!--IMPRINT=Que//-->

<!--CHAPTER=28//-->

<!--PAGES=551-553//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="549-551.html">Previous</A></TD>

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

<TD><A HREF="553-554.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

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

<P>Finding a word, a phrase, or a number in a file can be difficult if you have to read through each line yourself. Like most editors and word processors, <TT>emacs</TT> has a command that allows you to search for a string of characters and, if you want, replace those characters with others. You can search forward or backward from your current position in the buffer. You also can continue searching. <TT>emacs</TT> starts searching from the beginning of the buffer file when it reaches the end, and vice versa. Table 28.2 summarizes the commands for searching. In each case, <TT>emacs</TT> searches for the string you specify, in the direction you specify, and positions the cursor at the beginning of the string.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 28.2</B> The Search and Replace Commands

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="30%" ALIGN="LEFT">Command

<TH WIDTH="70%" ALIGN="LEFT">Action

<TR>

<TD COLSPAN="2"><HR>

<TR>

<TD>&lt;Ctrl-s&gt;

<TD>Search forward from current position

<TR>

<TD>&lt;Ctrl-r&gt;

<TD>Reverse search from current position

<TR>

<TD>&lt;Ctrl-x&gt;&lt;s&gt;

<TD>Repeat search forward

<TR>

<TD>&lt;Ctrl-x&gt;&lt;r&gt;

<TD>Repeat search in reverse

<TR>

<TD VALIGN="TOP">&lt;Esc&gt;&lt;r&gt;

<TD>Replace all instances of first typed string in mini-buffer with second typed string, ending each string with &lt;Esc&gt;

<TR>

<TD VALIGN="TOP">&lt;Esc&gt;&lt;Ctrl-r query&gt;

<TD>Before performing the replacement, answer in the mini-buffer with one of the following:<BR>&lt;Ctrl-g&gt;: Cancel operation<BR>&lt;!&gt;: Replace the rest<BR>&lt;?&gt;: Get a list of options<BR>&lt;.&gt;: Replace and exit to where command was initiated<BR>&lt;,&gt;: Replace the rest without asking<BR>&lt;y&gt; or Spacebar: Replace and continue with replace operation<BR>&lt;n&gt;: Don&#146;t replace but continue with operation

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P><FONT SIZE="+1"><B>Searching</B></FONT></P>

<P>When you type the search command, it appears in the mini-buffer. To search forward for the string <TT>sales &gt; 100K</TT> in a file, use this command:</P>

<!-- CODE SNIP //-->

<PRE>

&lt;Ctrl-s&gt;<B>sales &gt; 100K</B>

</PRE>

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

<P>This command starts an incremental search through the buffer. Notice that as you type the characters for the search string, <TT>emacs</TT> positions the cursor on the sequence of characters. If <TT>emacs</TT> can&#146;t find the text, it displays the <TT>search failed</TT> message. If the string is in the buffer, <TT>emacs</TT> positions the cursor under the first s in the word <TT>sales</TT>. When you find the first occurrence of the string, you must press &lt;Esc&gt; to stop the search; otherwise, <TT>emacs</TT> continues looking for a match as you enter other text. <TT>emacs</TT> refers to these types of searches as <I>incremental searches</I> ; <TT>emacs</TT> searches as you enter the search string.</P>

<P><TT>emacs</TT> can also perform non-incremental searches if you preface the search string with the &lt;Esc&gt; key and press the &lt;Return&gt; key at the end, as shown here:</P>

<!-- CODE SNIP //-->

<PRE>

&lt;Ctrl-s&gt;&lt;Esc&gt;<B>sales &gt; 100K</B>

</PRE>

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

<P>If you&#146;re searching a large file and realize that you&#146;ve entered the wrong search string, <TT>emacs</TT> searches the entire file. To stop the search, press &lt;Ctrl-g&gt;.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>TROUBLESHOOTING:&nbsp;&nbsp;</B><BR><B>I typed a string I know exists in the file, but</B> <TT>emacs</TT> <B>can&#146;t find it.</B> The most common cause for this error is that you typed the string incorrectly. <TT>emacs</TT>&#151;and computers in general&#151;don&#146;t do a good job of thinking; emacs has a terrible time figuring out what you really mean. If you&#146;re looking for the string &#147;vegi-burger&#148; but you type &#147;vigi-burger,&#148; <TT>emacs</TT> can&#146;t find what you want (unless you happened to misspell &#147;vegi-burger&#148; in the buffer and it matches the search string). Check the search string carefully before you press &lt;Return&gt;.<HR></FONT>

</BLOCKQUOTE>

<P><FONT SIZE="+1"><B>Replacing</B></FONT></P>

<P>Although searching for text can help you locate a particular word or section of text, many times you want to replace the found text. An example is if you find a spelling error and want to correct the entire buffer rather than one mistake at a time. For example, to replace every occurrence of the word &#147;misstake&#148; with &#147;mistake,&#148; press &lt;Esc&gt;&lt;r&gt;. The mini-buffer prompts for the string to search for; enter <TT><B>misstake</B>. emacs</TT> then prompts for the replacement string; enter <TT><B>mistake</B></TT>. <TT>emacs</TT> proceeds through the file, looking for the string &#147;misstake&#148; and replacing it with &#147;mistake.&#148; <TT>emacs</TT> also tries to match capitalization as best as possible. Thus, if &#147;misstake&#148; appears as &#147;Misstake,&#148; <TT>emacs</TT> replaces it with &#147;Mistake.&#148;</P>

<P>Maybe you don&#146;t want to replace every occurrence of a search string with the replacement string, in which case you can instruct <TT>emacs</TT> to query before replacing the string. To have <TT>emacs</TT> query and replace, press &lt;Esc&gt;&lt;Ctrl-r&gt;.</P>

<P>For example, if you want to selectively replace the name of your operating system, Linux, with its ancestor UNIX, press &lt;Esc&gt;&lt;Ctrl-r&gt;. <TT>emacs</TT> responds in the mini-buffer with <TT>Query replace:</TT>. Now enter the search string <TT><B>Linux</B></TT>. The <TT>Query replace Linux with:</TT> prompt appears. Enter the replacement string <TT><B>UNIX</B>. emacs</TT> begins the search and states, <TT>Query replacing Linux with UNIX</TT>. If you want to terminate the search-and-replace operation, press &lt;Ctrl-g&gt;.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="549-551.html">Previous</A></TD>

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

<TD><A HREF="553-554.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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