Linux 版 (精华区)
发信人: netiscpu (说不如做), 信区: Linux
标 题: [B] Red Hat Linux Unleashed (16)
发信站: 紫 丁 香 (Sat Jul 25 03:10:37 1998), 转信
Text Editors
_________________________________________________________________
o What Are Editors and Why Do I Need One?
o The Editing Functions
# Inserting and Deleting Text
# Reading and Writing Files
# Searching Text
# Copying and Moving Text
# Editing Buffers
o The vi Editor
# Starting vi
# vi modes
# Inserting Text
# Quitting vi
# Moving the Cursor
# Deleting Text
# Copying and Moving Text
# Searching and Replacing Text
# Setting Preferences
# A Summary of Commands
o The emacs Editor
# Starting emacs
# Control and Meta Keys
# Moving the Cursor
# Quitting emacs
# Deleting Text
# Working with Multiple Files
# Copying and Moving Text
# Searching and Replacing Text
# Using Modes with Buffers
# Online Help in emacs
# A Summary of Commands
o Summary
_________________________________________________________________
16
Text Editors
It's time to look at editors. This chapter will show you
* What editors are and why you need one
* The basic editing functions
* The vi editor in more detail
* The emacs editor in more detail
What Are Editors and Why Do I Need One?
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:
* User files such as .login and .cshrc
* System files
* Shell programs
* Documents
* Mail messages
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.
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.
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.
The Editing Functions
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.
Inserting and Deleting Text
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.
Reading and Writing Files
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.
Searching Text
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.
Editors also support search-and-replace functions that enable you to
change multiple instances of a string pattern with a single command.
Copying and Moving Text
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.
______________________________________________________________
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!
______________________________________________________________
Editing Buffers
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.
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.
The vi Editor
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.
Starting vi
You invoke vi from the command line by typing
vi
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.
vi can also be started with a file or a list of files to edit:
vi filename1 filename2 filename3 ...
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.
Alternatively, vi can be invoked from the command line as
vi +n filename
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.
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
vi asong
at the command line.
vi modes
At the bottom of the screen in the left corner, you will see
"asong" [NEW FILE] 1 line, 1 char
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.
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.
Four commands are echoed at the bottom of the screen on the status
line:
/ Searches forward.
? Searches backward.
: An ex command (ex is a standalone line-based editor used within vi).
! Invokes a shell command.
Each of these types of status-line commands must be entered by
pressing Return. This is not true for other types of vi commands, such
as the ones that do insertions.
______________________________________________________________
NOTE: To find out whether you are in command mode, use the set
showmode preference described in the section entitled "Setting
Preferences" later in this chapter.
______________________________________________________________
Inserting Text
So, knowing that you are in command mode, let's insert some text.
Basically, there are two commands for entering text on the current
line: the letters i and a. These letters in lowercase insert (i) text
to the left of the cursor or append text to the right of the cursor.
As with many vi commands, the uppercase versions of these letters have
similar effects with subtle differences: uppercase I and A insert and
append at the beginning and end of the current line, respectively.
After you type either of these letters, you will be placed in input
mode. Any text entered after this point will be displayed on-screen.
Type an i and then type the following:
Down I walk<Enter>
by the bay,<Enter>
Where I can<Enter>
hear the water.<Enter>
Down we walk<Enter>
by the bay,<Enter>
My hand held<Enter>
by my daughter.<Enter>
To exit from input mode, press Esc. Notice that you did not see the
letter i displayed before you entered the text, meaning that the i was
correctly interpreted as a command. Also, it is important to note that
it was not necessary to press Enter after pressing i for input mode.
Quitting vi
Now that you have some text for your file, let's quit the editor to
see the results. The commands used for saving the file and exiting vi
are slightly different from the i and d commands used in editing text:
you must precede the command with a colon (:).
In this case, you want to do a save and exit, which are actually
combined in one command. Type a :. At the bottom left of your screen,
you will notice that a colon has appeared. vi has recognized that you
are about to enter an ex command, and it will echo the remaining
characters of the command after the colon. Type wq and press Return.
vi quickly informs you that it has written the file to disk and tells
you how many lines it contains. If the file is small and you have a
fast system, this message may appear and be erased so quickly you
won't catch it. Don't worry—the file has been saved if you issued
the command properly. vi exits and you find yourself back at the shell
prompt. Another way to save and exit is to type ZZ. The difference
between this method and using wq is that ZZ will write the file only
if it has been modified since the last save.
You can quit vi by typing :q if no changes have been made to the file
you opened. This will not work if the file has been modified. If you
are sure that you don't want to save what you have done, enter :q!.
This command forces vi to quit, regardless of any edits.
To make sure that vi saved the file asong correctly, use the cat
command to quickly view the file's contents:
$ cat asong
Down I walk
by the bay,
Where I can
hear the water.
Down we walk
by the bay,
My hand held
by my daughter.
$
Everything is exactly as you typed it in the file, so no surprises
here.
Moving the Cursor
Moving the cursor around in vi essentially involves the following four
keys:
h Moves the cursor one space to the left.
j Moves the cursor down one line.
k Moves the cursor up one line.
l Moves the cursor one space to the right.
These keys can perform their operations only when vi is in command
mode. For convenience, most implementations of vi map these keys to
their directional counterparts on the keyboard arrow keys.
vi enables you to move through a file in bigger "leaps" as well.
Following are some commands for scrolling more than one line at a
time:
Ctrl-U Scrolls up a half-screen.
Ctrl-D Scrolls down a half-screen.
Ctrl-F Scrolls down one full screen.
Ctrl-B Scrolls up one full screen.
The size of these movements largely depends on the terminal settings.
It is also possible to move the cursor to a specific line in a file.
If you want to move to the tenth line, type 10G or :10 in command
mode. G by itself will move the cursor to the end of the file. The
cursor will not move if the number given is not applicable (for
example, typing :10 in a eight-line file will have no effect).
vi will also enable you to move the cursor a word at a time. A word is
defined as any sequence of non-whitespace characters. To move to the
beginning of the next word or punctuation mark on the current line,
type w. Type b to move the cursor to the beginning of the current or
previous word or punctuation mark.
Deleting Text
vi has commands for deleting characters, lines, and words. Deletion
means that the selected text is removed from the screen but is copied
into an unnamed text buffer from which it can be retrieved.
To delete a word, use the dw command. If you want to delete the word
to the right of the cursor, type dw. If you are in the middle of a
word, it will delete from the cursor position to the end. You can also
delete several words at a time. For example, the command 4dw will
delete the next four words on the current line.
Lines can be deleted individually or by specifying a range of lines to
delete. To delete the current line, type dd. The command 4dd deletes
four lines (the current line and three below it). dG will delete all
lines from the current one to the end of the file.
On the current line, you can delete in either direction: d^ will
delete backward to the beginning of the line; d$ (or D) will delete
forward to the end of the line.
To delete individual characters, x deletes the character underneath
the cursor, and X deletes the character to the left of the cursor.
Both of these commands will accept a number modifier: For example, 4x
deletes the current character and the four characters to the right.
Unwanted changes such as deletions can be immediately undone by the u
command. This "rolls back" the last edit made.
______________________________________________________________
NOTE: Not sure what command you just typed? When in doubt, press
Esc and then enter the command again.
______________________________________________________________
Copying and Moving Text
Moving sections of text around in a file basically requires three
steps:
1. Yank the text into a buffer.
2. Move the cursor to where you want to insert the text.
3. Place the text from the buffer at the new location.
Yanking text means to copy it into either a named or unnamed buffer.
The unnamed buffer is a temporary storage space in memory that is
continually overwritten by successive yanks. vi has 26 named buffers
that correspond to each letter of the alphabet.
To yank the current line into the unnamed buffer, the command is yy or
Y. These commands can be modified by a number indicating how many
lines beneath the cursor are to be yanked. For example, the command
3yy
in your file asong (with the cursor on the top line) yanks the
following text into the temporary buffer:
Down I walk
by the bay,
Where I can
This text could also be yanked into the named buffer a by the
following command:
"a3yy
The yank command to overwrite the contents of the named buffer a. If
you had typed a capital A instead of a lowercase a, the three lines
would have been appended to the end of the a buffer. This
overwrite-versus-append concept works the same for all of the named
buffers.
If you move the cursor to the end of the file using the :$ command,
you can then paste the contents of the unnamed buffer to the end of
the file. This is done using the p command, which pastes the contents
of a buffer to the right of the cursor (P pastes to the left of the
cursor). The paste command can also specify a named buffer in the same
way as the yank command:
"ap
Yanks can also be performed on words using the command yw. This
command can also use named buffers and accepts numeric modifiers.
Searching and Replacing Text
Text searches in vi can be performed in either direction: forward or
backward. Searches are always started from the current cursor location
and continue from the top or bottom of the file depending on which
direction you use. In other words, searches "wrap around" the file.
You can use your file asong to illustrate searches. To search forward
through asong for the word "bay," you would type
/bay
and press Return. Notice that this is a status-line command. The
command /bay is echoed on the status line and the cursor is moved to
the first occurrence it finds in the forward direction of the string
"bay." Interested in finding another instance of "bay"? Enter a /
character. This command continues the search for "bay" in the forward
direction and places the cursor at the next instance of "bay." Each
time you enter the / key, vi will try to find an instance of the
previous string pattern. When it reaches the end of the file, vi will
loop back and continue its search at the start of the file.
You can also search backward for strings in vi by using the ? command.
It works in exactly the same manner as the / command, but in the
opposite direction. Try it out by typing
?I
in asong, instructing vi to search back for instances of "I." This
search can be repeated by typing ?, as you may have suspected. You can
continue a search by pressing n, which always continues a search in
the same direction as the previous search. However, typing N will use
the same search string but in the opposite direction.
As I mentioned earlier, searches can be made very powerful through the
use of regular expressions. The search command is supplied in the same
fashion as described before (/ or ?), but square brackets are added to
instruct vi to do a regular expression expansion of the enclosed
characters. For example, search forward through asong from the first
line for all strings containing the substring "er". Type
/er
vi's first matching string arrives at "Where." If you type n, vi will
move the cursor to "where," and so on. You can also specify
collections of characters or ranges of characters to match. Try typing
the following:
/[a-z]y
This command used in asong will find the strings "by" and "my," as
well as any word with these strings inside them (such as "bay"). This
works because the range of characters given are treated as an
enumerated range of ASCII values. Thus, you could also include a range
of numbers (for example, 0-9). Now try the following command:
/[Mm]y
This will locate the strings "My" and "my."
In vi, searches without regular expressions will find only exact
matches of the supplied pattern (including the case of the letters in
the pattern). Clearly, regular expressions can be used to enhance many
types of searches in which you may not know exactly how a pattern
appears in a file.
One of the more common applications of a search is to replace
instances of one word (or pattern) with another. This is done with an
ex command that starts with a colon. To search the entire asong file
for the string "Down" and replace it with the string "Up," type
:%s/Down/Up/g
The s indicates that this is a search operation, the % means that the
entire file is to be searched, "Down" is the pattern to be found, "Up"
is the new pattern, and the g tells vi that the search should continue
until there are no more pattern matches. Without the g, vi would
perform the replacement on only the first match it finds. This command
also works with regular expressions appearing in the search pattern
and the replacement pattern.
Setting Preferences
vi is configurable, which means that you can set options to control
your editing environment. These options are initialized with default
values that you can modify in vi at any time. vi is configured using
the set command. The set command must be preceded by a colon and
entered by pressing Return. For example, to display line numbers in
the editor, you would issue
:set number
The following table describes a few of the more common set commands.
all Displays a list of all available set options and their current
status.
errorbells Sounds the terminal bell when an error occurs.
ignorecase Searches are case-insensitive.
number Displays line numbers in the leftmost column of the screen
(these are not written to the file).
showmode An indication appears at the bottom right of the screen if
you are in input mode, change mode, replace mode, and so on.
set commands that do not take a value can be switched off by inserting
a "no" as a prefix to the set parameter. For example, the command
:set nonumber
switches line numbering off. The command
:set
shows only the options that you have changed.
The settings that you use in a vi session are (unfortunately) lost
each time you exit vi. If you do not like the idea of resetting these
options each time you use vi, there is an easier way to perform this
initialization. Use the vi initialization file called .exrc. vi
searches for this file in your home directory each time it is invoked.
If it can't find this file, it uses the defaults set within the vi
program. As you will see in the following example, the .exrc file can
also be used to define vi macros.
A sample .exrc file would look something like this:
set number
set errorbells
set showmode
Note that the colon is not required before a set command in a .exrc
file.
A Summary of Commands
The following is a summary of the more essential commands described in
this chapter. You should consult the vi man page for more details on
the many other vi commands.
i Starts inserting text at the cursor.
h Moves the cursor one character to the left.
j Moves the cursor down one line.
k Moves the cursor up one line.
l Moves the cursor one character to the right.
C-f Scrolls forward one screen.
C-b Scrolls backward one screen.
ndd Deletes the next n lines.
nyy Yanks the next n lines into the unnamed buffer.
p Puts the contents of the unnamed buffer to the right of the cursor.
u Undoes the last change.
:wq Writes changes and exits vi.
:q! Exits vi without saving changes.
:set all Shows all set parameters and their values.
/string Searches forward for string.
The emacs Editor
emacs has become the editor of choice for many users because of its
online help facility and its extensive collection of editing commands.
For programmers, emacs is especially attractive because it can be
configured to format source code for a variety of languages such as C,
C++, and Lisp. emacs is somewhat easier to learn than vi, but it also
features a much larger set of commands.
Starting emacs
emacs is invoked from the command line by entering
emacs
To start emacs with a file to be edited, enter
emacs filename
If you start emacs with a file, the screen will display the contents
starting from the first line. Note the two lines at the bottom of the
screen. The first of these lines, known as the mode line, displays the
name of the file being edited and which part of the file that you are
looking at (for example, TOP, 20%, BOT). The last line on the screen
is the echo line, which emacs uses to display system messages and as a
prompt for more input.
Control and Meta Keys
You are quite free at this point to start entering text into the edit
buffer at the cursor location. However, you're probably wondering,
"How do I move the cursor around?" Before I fill you in on this little
detail, there are two keys that you should know about: the Control key
(which I will refer to as C) and the Meta key (denoted by M). The
Control key is used in most of the commands for emacs, but some use
the Meta key instead. Commands in emacs consist of combinations of the
Control or Meta key followed by some other character. It is necessary
to hold down the Control key when pressing the next character, whereas
the Meta key can be pressed and released before you enter the next
character. For the PC, the Meta key is usually the Alt key.
______________________________________________________________
NOTE: You may see the Control key abbreviated as C and the Meta key
denoted by M.
On the PC, you should use the Alt key for the Meta key.
______________________________________________________________
Moving the Cursor
Now that you know about the Control key, we can talk about the
cursor-movement commands. The basic ones that you need to remember
are:
C-f Moves the cursor forward one character.
C-b Moves the cursor back one character.
C-p Moves the cursor to the previous line.
C-n Moves the cursor to the next line.
C-a Moves the cursor to the beginning of the line.
C-e Moves the cursor to the end of the line.
______________________________________________________________
NOTE: When I refer to a command such as C-b, I mean press and hold
the Control key while you press the letter b. The same is true for
Meta commands such as M-v.
______________________________________________________________
Most implementations of emacs conveniently map the first four movement
commands to the arrow keys on the keyboard. Let's edit a new file
called asong2. (If you are in the middle of a previous file, exit the
editor by typing Ctrl-X, Ctrl-C.) Start up a new copy of emacs by
entering the following command from the shell:
emacs asong2<Enter>
Now enter the following text into the buffer:
This is a file for edit
And you have to give emacs some credit
It's really quite swell
And all you have to do is spell
emacs works, if you let it!
Now use the C-b command to move back through this horrendous piece of
poetry. Notice how the cursor jumps up to the end of each line after
the reaching the beginning of the previous line. This works the same
way in the opposite direction using the C-f command.
Another useful way of moving around is by scrolling through a file one
screen at a time. The command C-v moves the cursor forward one screen
at a time. The command M-v moves the cursor in the opposite direction.
Like vi, emacs treats a sequence of non-whitespace characters as a
word. You can move the cursor forward one word at a time with the M-f
command. The M-b command moves back one word.
Quitting emacs
At this time, you can stop editing to save the contents of the buffer
to your file asong2. To do this, issue the command sequence C-x C-s.
As you enter this command, notice how the command is displayed on the
echo line as you type it. To quit emacs and return to the shell, enter
the command C-x C-c. If you have made changes that haven't been saved
using C-x C-s, emacs will ask for confirmation before quitting.
Deleting Text
You can delete text in several ways. The Backspace (or Delete) key is
used to erase the character immediately preceding the cursor. The
command C-d deletes the character underneath the cursor, and C-k
deletes or "kills" all characters from the cursor to the end of the
line. Words can be deleted also: M-d deletes the word the cursor is
currently located over and M-Del (the Delete key) deletes the word
previous to the current word.
If you ever find that you have committed an edit that you didn't
really want, just type C-x u to undo the previous editing changes. You
can repeat the undo command as many times as you want, rolling over
all the changes you made. This is an advantage over vi, which can only
undo the last change.
______________________________________________________________
NOTE: Change your mind about a command? Type C-g to abort the
current command operation.
______________________________________________________________
Working with Multiple Files
emacs 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 C-x C-f command. After entering this command, you will see the
following prompt on the echo line:
Find file: ~/
emacs is smart when it looks for files. It supports filename
completion, which means that you can simply type a few characters of a
filename and emacs will attempt to match a file (or files) to what you
have typed so far. To do this, type in the letters ".log" and press
the Tab key. emacs expands this to ~/.login (or any other filename
that matches). If two or more files match the pattern supplied,
pressing the Tab key will cycle through them.
After you have loaded a new file into emacs, you can switch between
buffers by using the C-x b command followed by the name of the buffer
that you want. The buffer's name is that of the file that was loaded
into it. The C-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.
When you have finished editing a buffer, instead of saving the
contents using the C-x C-s command, you may decide that you do not
really want to keep the edits you have made. You can "kill" the
current buffer by entering the command C-x k. emacs will prompt you
for the name of the buffer to kill, but you can kill the current
buffer by simply pressing Return. emacs will ask for confirmation, to
which you can respond by typing yes (if you're sure) and press Return.
______________________________________________________________
NOTE: Whenever you are working with just two buffers, you can
simply press Return after entering the C-x b command to switch to
the other buffer.
______________________________________________________________
Copying and Moving Text
In order to copy and move blocks of text in emacs, 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 C-Space command (in this case, Space
means literally 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 M-w. The text within the block is copied to emacs's
internal clipboard, from which it can be pasted at another location
using the C-y command. Alternatively, you can cut the block into the
clipboard using C-w instead of M-w. Cutting, of course, deletes the
text from its current location.
Let's try out some of these techniques on your buffer asong2. Use the
M-< command to jump to the beginning of the buffer. Enter a C-Space to
mark the start of the block and then use C-n to move down a line. Cut
the block to the clipboard using C-w, move the cursor to the end of
the buffer using M->, and paste it using C-y. The result should look
like this:
It'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
Searching and Replacing Text
You can search forward and backward through text using the C-s and C-r
commands, respectively. These commands, like many in emacs, use
command completion. This is the same concept as filename completion:
you supply a few characters and emacs tries to fill in the rest. In
this case, however, emacs moves the cursor to each instance it finds
of the string supplied.
As you enter more characters, emacs 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.
As with vi, searching in either direction wraps around the beginning
or end of the file, depending on in which direction you are searching.
However, when emacs reaches the top or bottom of the file, it will
tell you that the search failed. You can keep searching by pressing
C-s or C-r accordingly and emacs will continue using the current
string.
To illustrate how searching in emacs works, let's search backward
through your file asong2. Enter C-r and type an s. emacs moves the
cursor to the "s" in "works". Now type a w. emacs now tries to find a
pattern that matches the string sw. The cursor ends up on the "w" in
"swell". You can edit the search string using the Backspace or Delete
key. Delete the w and type a p. What happens?
Search-and-replaces are done by entering the query-replace command.
This is qualified by the M-x command, which tells emacs that the text
to follow is a full command and not a key combination. After you have
entered the query-replace command, you will be prompted for the string
to be found. Enter the string and press Return. emacs will then prompt
you for the replacement string. Once you have entered the replacement
string, emacs will search for every instance of the first string and,
if it finds one, asks you if it should be replaced with the second
string.
______________________________________________________________
NOTE: emacs is actually composed of a set of explicit command names
that are bound to key combinations. The query-replace command is
bound to M-% in some implementations of emacs.
______________________________________________________________
Using Modes with Buffers
emacs 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 C-x m, emacs enters mail mode, which formats a
buffer with To: and Subject: fields as well as a space for the body of
the mail message. emacs can even send the mail message for you (by
entering C-c C-c) after you have finished editing it.
emacs 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 emacs, the buffer is automatically set to
C mode. This mode has knowledge of how C programs are formatted, and
pressing the Tab key will indent a line correctly based on its place
in the program (a for loop within another for loop, as an example).
Online Help in emacs
One of the best features of the emacs editor is that if you ever get
stuck, or are just plain overwhelmed by it all, help is just a few
keystrokes away—and lots of it! If you need a short emacs
tutorial, just enter C-h t. If you would like to find out what
function a particular key supports, type C-h k and then press the key.
The help option has many different topics. Use C-h i to load the
information documentation reader and read about all the types of help
available.
A Summary of Commands
emacs, like the vi editor, has such a rich command set that we can
cover only a portion of it in this chapter. The following table is a
summary of the strictly essential commands that you will need for
basic editing in emacs. The emacs man page should be consulted for a
more comprehensive description of the full emacs command set.
C-b Moves back one character.
C-d Deletes the current character.
C-f Moves forward one character.
C-g Cancels the current command.
C-h Enters emacs online help.
C-n Moves forward to the next line.
C-p Moves back to the previous line.
C-s Searches forward for a string.
C-v Scrolls forward one screen.
M-v Scrolls backward one screen.
C-x u Undoes the last edit.
C-x C-c Exits emacs.
C-x C-s Saves the buffer to a file.
Summary
There are many text editors available for the Linux system. Two of the
most popular are vi (which is actually an alias to the elvis editor)
and emacs. Both provide basic editing functions such as inserting and
deleting text, reading and writing of external files, text searching,
and copying and moving text. vi is a full-screen editor that has two
modes: command mode and text mode. emacs is an extendible and powerful
editor that is highly configurable to suit a variety of editing tasks
(such as programming, document writing, and changing user or system
files).
--
Enjoy Linux!
-----It's FREE!-----
※ 修改:.netiscpu 于 Jul 25 03:48:04 修改本文.[FROM: mtlab.hit.edu.cn]
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: mtlab.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:609.297毫秒