?? rhl16.htm
字號:
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl16.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl17.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl17.htm">
<LINK REL="Previous" HREF="tppmsgs/msgs0.htm#16" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl15.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl16.htm</H2>
<P ALIGN=LEFT>
<A HREF="tppmsgs/msgs0.htm#16" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl15.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl17.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl17.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E114" >What Are Editors and Why Do I Need One?</A>
<LI>
<A HREF="#E68E115" >The Editing Functions</A>
<UL>
<LI>
<A HREF="#E69E209" >Inserting and Deleting Text</A>
<LI>
<A HREF="#E69E210" >Reading and Writing Files</A>
<LI>
<A HREF="#E69E211" >Searching Text</A>
<LI>
<A HREF="#E69E212" >Copying and Moving Text</A>
<LI>
<A HREF="#E69E213" >Editing Buffers</A></UL>
<LI>
<A HREF="#E68E116" >The vi Editor</A>
<UL>
<LI>
<A HREF="#E69E214" >Starting vi</A>
<LI>
<A HREF="#E69E215" >vi modes</A>
<LI>
<A HREF="#E69E216" >Inserting Text</A>
<LI>
<A HREF="#E69E217" >Quitting vi</A>
<LI>
<A HREF="#E69E218" >Moving the Cursor</A>
<LI>
<A HREF="#E69E219" >Deleting Text</A>
<LI>
<A HREF="#E69E220" >Copying and Moving Text</A>
<LI>
<A HREF="#E69E221" >Searching and Replacing Text</A>
<LI>
<A HREF="#E69E222" >Setting Preferences</A>
<LI>
<A HREF="#E69E223" >A Summary of Commands</A></UL>
<LI>
<A HREF="#E68E117" >The emacs Editor</A>
<UL>
<LI>
<A HREF="#E69E224" >Starting emacs</A>
<LI>
<A HREF="#E69E225" >Control and Meta Keys</A>
<LI>
<A HREF="#E69E226" >Moving the Cursor</A>
<LI>
<A HREF="#E69E227" >Quitting emacs</A>
<LI>
<A HREF="#E69E228" >Deleting Text</A>
<LI>
<A HREF="#E69E229" >Working with Multiple Files</A>
<LI>
<A HREF="#E69E230" >Copying and Moving Text</A>
<LI>
<A HREF="#E69E231" >Searching and Replacing Text</A>
<LI>
<A HREF="#E69E232" >Using Modes with Buffers</A>
<LI>
<A HREF="#E69E233" >Online Help in emacs</A>
<LI>
<A HREF="#E69E234" >A Summary of Commands</A></UL>
<LI>
<A HREF="#E68E118" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E16"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>16</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E16"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Text Editors</B></FONT></CENTER></H2>
<BR>
<P>It's time to look at editors. This chapter will show you
<BR>
<UL>
<LI>What editors are and why you need one
<BR>
<BR>
<LI>The basic editing functions
<BR>
<BR>
<LI>The vi editor in more detail
<BR>
<BR>
<LI>The emacs editor in more detail
<BR>
<BR>
</UL>
<BR>
<A NAME="E68E114"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Are Editors and Why Do I Need One?</B></FONT></CENTER></H3>
<BR>
<P>A text editor is one of the most essential tools provided with the Linux (or virtually any) operating system. With an editor, you can create and modify text files that have a wide variety of applications:
<BR>
<UL>
<LI>User files such as .login and .cshrc
<BR>
<BR>
<LI>System files
<BR>
<BR>
<LI>Shell programs
<BR>
<BR>
<LI>Documents
<BR>
<BR>
<LI>Mail messages
<BR>
<BR>
</UL>
<P>These are but a few of the many different types of text files that you will use when working with Linux. Basically, editors enable you to insert, delete, move, and search text ranging from individual characters to thousands of lines.
<BR>
<P>Two of the most popular editors for the Linux system are emacs and vi. These editors are both full-screen text editors: Put simply, they use every row and column of your terminal screen to display the textual contents of a file. Both of these editors
feature a rich set of commands. The essential commands for manipulating text can be learned reasonably quickly; the more sophisticated commands may take a little longer to master. However, you will likely appreciate this investment as you see how much time
these powerful tools can save you.
<BR>
<P>Choosing one editor over another can be a matter of taste. Both emacs and vi are efficient and can handle virtually any size of file. The emacs editor is better suited to complex editing tasks and comes with an online help facility, but, for simple
editing jobs, either editor is equally good. It really just comes down to whichever one you feel more comfortable using.
<BR>
<BR>
<A NAME="E68E115"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The Editing Functions</B></FONT></CENTER></H3>
<BR>
<P>Although there are a variety of text editors for Linux that have different interfaces, they all basically do the same things. Any useful text editor should support the following features at a minimum.
<BR>
<BR>
<A NAME="E69E209"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Inserting and Deleting Text</B></FONT></CENTER></H4>
<BR>
<P>The most intrinsic function of a text editor is to enable you to enter and erase characters as you see fit. This also implies that you have complete control over the movement of the cursor and its placement in the text.
<BR>
<BR>
<A NAME="E69E210"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Reading and Writing Files</B></FONT></CENTER></H4>
<BR>
<P>Because you will want to save the text files that you create for future use and reuse, an editor can write your text to an external file. Whenever you need to make changes to your file, an editor can read the file from disk. A nice feature is that text
editors are designed to accommodate ASCII formatted files, so an editor (such as emacs) can read any file written by another editor (such as vi), and vice versa.
<BR>
<BR>
<A NAME="E69E211"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Searching Text</B></FONT></CENTER></H4>
<BR>
<P>Personally scanning line after line of a large file for instances of a particular word is either a great way to improve your powers of concentration or an exercise in self-torture. That is why text editors provide sophisticated search capabilities.
These include the use of regular expressions as well as fixed strings. Remember that regular expressions include metacharacters (such as ., ?, and *) that replace and expand unknown text patterns.
<BR>
<P>Editors also support search-and-replace functions that enable you to change multiple instances of a string pattern with a single command.
<BR>
<BR>
<A NAME="E69E212"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Copying and Moving Text</B></FONT></CENTER></H4>
<BR>
<P>Because there is no guarantee that the way text is initially typed into a file is the way it should forever remain, editors provide you with the means to copy, cut, and move (or paste) blocks of text. These blocks can range in size from several pages to
a single character. The distinction between copying and cutting text is that cutting deletes the selected block of text after it has been copied to a buffer, whereas copying does not.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Imagine having to retype Dickens's A Tale of Two Cities after realizing that you have somehow placed "It was the best of times, it was the worst of times" at the end of the file and not the start!</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E213"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Editing Buffers</B></FONT></CENTER></H4>
<BR>
<P>What is a buffer, you ask? Buffers are places in the memory of the editing program where text can reside as you make changes to a file. For example, the first time you edit a file, the text you have entered actually exists in a buffer that is written to
an external file when you do a save. Buffers can also be used at other times in editing, particularly when it is necessary to temporarily move a block of text to memory as you make changes (in other words, cutting and pasting). Many editors enable you to
manage multiple buffers simultaneously.
<BR>
<P>These editors have many commands that will not be fully detailed in this chapter. Before engaging in any long and arduous editing task, consult the man page for the editor you are using. There may be an easier way of doing whatever it is that you want
to do. As you gain experience with an editor, you will discover convenient shortcuts and functions to perform your most tedious editing chores.
<BR>
<BR>
<A NAME="E68E116"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The vi Editor</B></FONT></CENTER></H3>
<BR>
<P>The vi editor is installed with virtually every UNIX system in existence. Because of this, vi is considered by many to be the default text editor of the UNIX system (upon which Linux is based). vi has two modes of operation and terse commands, both of
which make it a somewhat more difficult editor to learn than emacs. However, it is a useful editor to learn if emacs has not been installed on your Linux system.
<BR>
<BR>
<A NAME="E69E214"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Starting vi</B></FONT></CENTER></H4>
<BR>
<P>You invoke vi from the command line by typing
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi</FONT></PRE>
<P>The screen will clear and a column of tildes (~) will appear in the leftmost column. You are now editing an empty, unnamed file. Whatever text you place in this file will exist in a buffer until you write the contents of the buffer to some named file.
The tilde is vi's way of telling you that the line where the tilde appears is empty of text.
<BR>
<P>vi can also be started with a file or a list of files to edit:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi filename1 filename2 filename3 ...</FONT></PRE>
<P>Typically, you will probably edit only one file per vi session. If you are editing a list of files, vi will edit each one in the sequence that they appear on the command line.
<BR>
<P>Alternatively, vi can be invoked from the command line as
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi +n filename</FONT></PRE>
<P>where n represents the line number where vi will place its cursor in filename. This is useful for programmers debugging large source code files who need to quickly jump to a known line containing an error.
<BR>
<P>Another example is useful in illustrating the vi editor. If you still have a vi session on your screen, exit it by pushing Esc, and then typing :q!. To start a new vi session, enter
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">vi asong</FONT></PRE>
<P>at the command line.
<BR>
<BR>
<A NAME="E69E215"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>vi modes</B></FONT></CENTER></H4>
<BR>
<P>At the bottom of the screen in the left corner, you will see
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">"asong" [NEW FILE] 1 line, 1 char</FONT></PRE>
<P>The messages displayed on this status line tell you what vi is doing or has just done. In this case, vi is telling you that it has opened an empty buffer whose contents will be saved (whenever you do a save) to the file asong.
<BR>
<P>At this moment, you are in the command mode of vi. This is the major conceptual leap required in working with this editor. When editing text, you must remember if you are in command mode or text mode. In command mode, any character sequences that you
enter are interpreted as vi commands. In text mode, every character typed is placed in the buffer and displayed as text on-screen.
<BR>
<P>Four commands are echoed at the bottom of the screen on the status line:
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -