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

📄 315-317.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<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=315-317//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="317-318.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading26"></A><FONT COLOR="#000077">Working with Multiple Files</FONT></H4>

<P><TT>emacs</TT> enables you to edit several files in one session, each contained within its own buffer. To copy an external file into a new buffer, use the Ctrl&#43;x Ctrl&#43;f command. After entering this command, you see the following prompt on the echo line:</P>

<!-- CODE SNIP //-->

<PRE>

Find file: ~/

</PRE>

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

<P><TT>emacs</TT> is smart when it looks for files. It supports <I>filename completion,</I> which means that you can simply type a few characters of a filename, and <TT>emacs</TT> attempts to match a file (or files) to whatever you have typed so far. To do this, type <TT>.log</TT> and press the Tab key. <TT>emacs</TT> expands this to <TT>~/.login</TT>, and you can see your <TT>.login</TT> file in a new buffer by pressing Return. If two or more files match the pattern supplied, pressing the Tab key cycles through them.</P>

<P>After you load a new file into <TT>emacs</TT>, you can switch between buffers by using the Ctrl&#43;x b command followed by the name of the buffer that you want. The buffer&#146;s name is that of the file that was loaded into it. The Ctrl&#43;x b command also uses filename completion, so you can use the Tab key to cycle through your edit buffers after supplying a few relevant characters.</P>

<P>When you finish editing a buffer, instead of saving the contents by using the Ctrl&#43;x Ctrl&#43;s command, you might decide that you do not really want to keep the edits that you made. You can kill the current buffer by entering the command Ctrl&#43;x k. <TT>emacs</TT> then prompts you for the name of the buffer to kill, but by simply pressing Return, you can kill the current buffer. Next, <TT>emacs</TT> asks for confirmation to which you can respond by typing <TT>yes</TT> (if you&#146;re sure) and pressing Return.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>Whenever you are working with just two buffers, you can simply press Return after entering the Ctrl&#43;x b command to switch to the other buffer.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">Copying and Moving Text</FONT></H4>

<P>In order to copy and move blocks of text in <TT>emacs</TT>, you must define the region of text by marking the beginning and end points of the text block. This is done by moving the cursor to where you want the block to begin and marking it using the Ctrl&#43;Space command (in this case, Space means literally pressing the spacebar). The end of the block is defined by wherever you place the cursor after that. To make a copy of the block, enter the command META&#43;w. The text within the block is copied to <TT>emacs</TT>&#146;s internal clipboard from which it can be pasted at another location using the Ctrl&#43;y command. Alternatively, you can cut the block into the clipboard using Ctrl&#43;w instead of META&#43;w. Cutting, of course, deletes the text from its current location.</P>

<P>Let&#146;s try out some of these techniques on your buffer <TT>asong2</TT>. Use the META&#43;&lt; command to jump to the beginning of the buffer. Enter a Ctrl&#43;Space to mark the start of the block and then use Ctrl&#43;n to move down a line. Cut the block to the clipboard using Ctrl&#43;w, move the cursor to the end of the buffer using META&#43;&gt;, and paste it using Ctrl&#43;y. The result is something like this:</P>

<!-- CODE SNIP //-->

<PRE>

It&#146;s really quite swell

And all you have to do is spell

emacs works, if you let it!

This is a file for edit

And you have to give emacs some credit

</PRE>

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

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

<P>You can search forward and backward through text using the Ctrl&#43;s and Ctrl&#43;r commands, respectively. These commands, like many in <TT>emacs</TT>, use <I>command completion</I>. This is the same concept as filename completion: you supply a few characters, and <TT>emacs</TT> tries to fill in the rest. In this case, however, <TT>emacs</TT> moves the cursor to each instance it finds of the string supplied.</P>

<P>As you enter more characters, <TT>emacs</TT> narrows its search further. When you have found a correct match, press Return or use any of the cursor-movement commands to halt the search.</P>

<P>As with <TT>vi</TT>, searching in either direction wraps around the beginning or end of the file, depending on in which direction you are searching. However, when <TT>emacs</TT> reaches the top or bottom of the file, it tells you that the search failed. You can keep searching by pressing Ctrl&#43;s or Ctrl&#43;r accordingly, and <TT>emacs</TT> will continue using the current string.</P>

<P>To illustrate how searching in <TT>emacs</TT> works, let&#146;s search backward through your file <TT>asong2</TT>. Enter Ctrl&#43;r and type an <TT>s</TT>. <TT>emacs</TT> moves the cursor to the &#147;s&#148; in &#147;works.&#148; Next type a <TT>w</TT>. <TT>emacs</TT> now tries to find a pattern that matches the string <TT>sw</TT>. The cursor ends up on the &#147;<TT>w</TT>&#148; in &#147;swell.&#148; You can edit the search string by using the Backspace, or Delete, key. Delete the <TT>w</TT> and type a <TT>p</TT>. What happens?</P>

<P>Searches-and-replaces are done by entering the query-replace command. This is qualified by the META-x command, which tells <TT>emacs</TT> that the text to follow is a full command and not a key combination. After you enter the query-replace command, you are prompted for the string to be found. Enter the string and press Return. <TT>emacs</TT> then prompts you for the replacement string. After you enter the replacement string, <TT>emacs</TT> searches for every instance of the first string and, if it finds one, asks if it should be replaced with the second string.</P>

<H4 ALIGN="LEFT"><A NAME="Heading29"></A><FONT COLOR="#000077">Using Modes with Buffers</FONT></H4>

<P><TT>emacs</TT> is versatile enough to handle many different types of editing chores. It enables you to associate modes to buffers so that you can have text formatting specific to your editing application. If you enter the command Ctrl&#43;x m, <TT>emacs</TT> enters mail mode that formats a buffer with To: and Subject: fields, as well as a space for the body of the mail message. <TT>emacs</TT> can even send the mail message for you (by entering Ctrl&#43;c Ctrl&#43;c) after you finish editing it.</P>

<TT>emacs</TT> also supports modes for many different programming languages, such as C. When a file with the extension .c (C source code) or .h (C header file) is loaded into <TT>emacs</TT>, the buffer is automatically set to C mode. This mode has knowledge of how C programs are formatted, and pressing the Tab key indents a line correctly based on its place in the program (a <TT>for</TT> loop within another <TT>for</TT> loop, as an example).

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="317-318.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 + -