📄 ch16.htm
字号:
<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>
-->
<center><H1>Text Editors</H1></center>
<UL>
<LI><A HREF="#Heading1">- 16 -</A>
<UL>
<LI><A HREF="#Heading2">Text Editors</A>
<UL>
<LI><A HREF="#Heading3">What Are Editors and Why Do I Need One?</A>
<LI><A HREF="#Heading4">The Editing Functions</A>
<UL>
<LI><A HREF="#Heading5">Inserting and Deleting Text</A>
<LI><A HREF="#Heading6">Reading and Writing Files</A>
<LI><A HREF="#Heading7">Searching Text</A>
<LI><A HREF="#Heading8">Copying and Moving Text</A>
</UL>
<LI><A HREF="#Heading9">NOTE</A>
<UL>
<LI><A HREF="#Heading10">Editing Buffers</A>
</UL>
<LI><A HREF="#Heading11">The vi Editor</A>
<UL>
<LI><A HREF="#Heading12">Starting vi</A>
<LI><A HREF="#Heading13">vi Modes</A>
</UL>
<LI><A HREF="#Heading14">TIP</A>
<UL>
<LI><A HREF="#Heading15">Inserting Text</A>
<LI><A HREF="#Heading16">Quitting vi</A>
<LI><A HREF="#Heading17">Moving the Cursor</A>
<LI><A HREF="#Heading18">Deleting Text</A>
</UL>
<LI><A HREF="#Heading19">TIP</A>
<UL>
<LI><A HREF="#Heading20">Copying and Moving Text</A>
<LI><A HREF="#Heading21">Searching and Replacing Text</A>
<LI><A HREF="#Heading22">Setting Preferences</A>
<LI><A HREF="#Heading23">A Summary of Commands</A>
</UL>
<LI><A HREF="#Heading24">The emacs Editor</A>
<UL>
<LI><A HREF="#Heading25">Starting emacs</A>
<LI><A HREF="#Heading26">Control and Meta Keys</A>
</UL>
<LI><A HREF="#Heading27">NOTE</A>
<UL>
<LI><A HREF="#Heading28">Moving the Cursor</A>
</UL>
<LI><A HREF="#Heading29">NOTE</A>
<UL>
<LI><A HREF="#Heading30">Quitting emacs</A>
<LI><A HREF="#Heading31">Deleting Text</A>
</UL>
<LI><A HREF="#Heading32">TIP</A>
<UL>
<LI><A HREF="#Heading33">Working with Multiple Files</A>
</UL>
<LI><A HREF="#Heading34">TIP</A>
<UL>
<LI><A HREF="#Heading35">Copying and Moving Text</A>
<LI><A HREF="#Heading36">Searching and Replacing Text</A>
</UL>
<LI><A HREF="#Heading37">NOTE</A>
<UL>
<LI><A HREF="#Heading38">Using Modes with Buffers</A>
<LI><A HREF="#Heading39">Online Help in emacs</A>
<LI><A HREF="#Heading40">A Summary of Commands for emacs</A>
</UL>
<LI><A HREF="#Heading41">The joe Editor</A>
<UL>
<LI><A HREF="#Heading42">A Summary of Commands for joe</A>
</UL>
<LI><A HREF="#Heading43">Listing 16.1. Commands for the joe editor.</A>
<UL>
<LI><A HREF="#Heading44">When Should I Use joe Rather Than vi or emacs?</A>
<LI><A HREF="#Heading45">Macro Recording and Playback</A>
</UL>
<LI><A HREF="#Heading46">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 16 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Text Editors</FONT></H2>
<P><I>by Peter MacKinnon and Kamran Husain</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>What Are Editors and Why Do I Need One?
<P>
<LI>The Editing Functions
<P>
<LI>The vi Editor
<P>
<LI>The emacs Editor
<P>
<LI>The joe Editor
</UL>
<P>It's time to look at editors. This chapter will show you
<UL>
<LI>What editors are and why you need one
<P>
<LI>The basic editing functions
<P>
<LI>The vi editor in more detail
<P>
<LI>The emacs editor in more detail
<P>
<LI>The joe editor in more detail
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">What Are Editors
and Why Do I Need One?</FONT></H3>
<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:
<UL>
<LI>User files such as <TT>.login</TT> and <TT>.cshrc</TT>
<P>
<LI>System files
<P>
<LI>Shell programs
<P>
<LI>Documents
<P>
<LI>Mail messages
</UL>
<P>These are but a few of the many types of text files 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.</P>
<P>Two of the most popular editors for the Linux system are emacs and vi. Both these
editors are 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 might take a little
longer to master. You will likely appreciate this investment, however, as you see
how much time these powerful tools can save you. A less known, though quite powerful,
editor is joe. It too attempts to use all the screen space it can, but it offers
an online help menu and an easy-to-use command set.</P>
<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. If you are coming from the DOS world or have
used WordStar, you will find joe very easy to use. It really just comes down to choosing
the one you feel more comfortable using.
<H3 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">The Editing Functions</FONT></H3>
<P>Although the various text editors for Linux have different interfaces, they all
basically do the same things. Any useful text editor should support the following
features at a minimum.
<H4 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">Inserting and Deleting
Text</FONT></H4>
<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.
<H4 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">Reading and Writing
Files</FONT></H4>
<P>Because you will want to save the text files you create for future use, 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 handy 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).
<H4 ALIGN="CENTER"><A NAME="Heading7<FONT COLOR="#000077">Searching Text</FONT></H4>
<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 <TT>.</TT>, <TT>?</TT>, and <TT>*</TT>)
that replace and expand unknown text patterns.</P>
<P>Editors also support search-and-replace functions that enable you to change multiple
instances of a string pattern with a single command.
<H4 ALIGN="CENTER"><A NAME="Heading8<FONT COLOR="#000077">Copying and Moving
Text</FONT></H4>
<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 a single
character to several pages. 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 remove the text block.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading9<FONT COLOR="#000077"><B>NOTE:</B> </FONT>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!
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading10<FONT COLOR="#000077">Editing Buffers</FONT></H4>
<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 perform 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, when you're cutting and pasting).
Many editors enable you to manage multiple buffers simultaneously.</P>
<P>These editors have many commands that are not 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 might be an easier way of doing whatever it is 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.
<H3 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">The vi Editor</FONT></H3>
<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
(on 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. It is, however,
a useful editor to learn if emacs has not been installed on your Linux system.
<H4 ALIGN="CENTER"><A NAME="Heading12<FONT COLOR="#000077">Starting vi</FONT></H4>
<P>You invoke vi from the command line by typing <TT>vi</TT>.</P>
<P>The screen clears and a column of tildes (<TT>~</TT>) appears in the leftmost
column. You are now editing an empty, unnamed file. Whatever text you place in this
file exists 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.</P>
<P>vi can also be started with a file or a list of files to edit, like this:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">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 edits the files in the order in which they appear on the command
line.</P>
<P>Alternatively, vi can be invoked from the command line as<FONT COLOR="#0066FF"></FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -