📄 551-553.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><Ctrl-s>
<TD>Search forward from current position
<TR>
<TD><Ctrl-r>
<TD>Reverse search from current position
<TR>
<TD><Ctrl-x><s>
<TD>Repeat search forward
<TR>
<TD><Ctrl-x><r>
<TD>Repeat search in reverse
<TR>
<TD VALIGN="TOP"><Esc><r>
<TD>Replace all instances of first typed string in mini-buffer with second typed string, ending each string with <Esc>
<TR>
<TD VALIGN="TOP"><Esc><Ctrl-r query>
<TD>Before performing the replacement, answer in the mini-buffer with one of the following:<BR><Ctrl-g>: Cancel operation<BR><!>: Replace the rest<BR><?>: Get a list of options<BR><.>: Replace and exit to where command was initiated<BR><,>: Replace the rest without asking<BR><y> or Spacebar: Replace and continue with replace operation<BR><n>: Don’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 > 100K</TT> in a file, use this command:</P>
<!-- CODE SNIP //-->
<PRE>
<Ctrl-s><B>sales > 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’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 <Esc> 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 <Esc> key and press the <Return> key at the end, as shown here:</P>
<!-- CODE SNIP //-->
<PRE>
<Ctrl-s><Esc><B>sales > 100K</B>
</PRE>
<!-- END CODE SNIP //-->
<P>If you’re searching a large file and realize that you’ve entered the wrong search string, <TT>emacs</TT> searches the entire file. To stop the search, press <Ctrl-g>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>TROUBLESHOOTING: </B><BR><B>I typed a string I know exists in the file, but</B> <TT>emacs</TT> <B>can’t find it.</B> The most common cause for this error is that you typed the string incorrectly. <TT>emacs</TT>—and computers in general—don’t do a good job of thinking; emacs has a terrible time figuring out what you really mean. If you’re looking for the string “vegi-burger” but you type “vigi-burger,” <TT>emacs</TT> can’t find what you want (unless you happened to misspell “vegi-burger” in the buffer and it matches the search string). Check the search string carefully before you press <Return>.<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 “misstake” with “mistake,” press <Esc><r>. 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 “misstake” and replacing it with “mistake.” <TT>emacs</TT> also tries to match capitalization as best as possible. Thus, if “misstake” appears as “Misstake,” <TT>emacs</TT> replaces it with “Mistake.”</P>
<P>Maybe you don’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 <Esc><Ctrl-r>.</P>
<P>For example, if you want to selectively replace the name of your operating system, Linux, with its ancestor UNIX, press <Esc><Ctrl-r>. <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 <Ctrl-g>.</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 + -