📄 ch16.htm
字号:
</FONT></PRE>
<P>The quotation mark (<TT>"</TT>) tells the yank command to overwrite the contents
of the named buffer <TT>a</TT>. If you had typed a capital <TT>A</TT> instead of
a lowercase <TT>a</TT>, the three lines would have been appended to the end of the
<TT>a</TT> buffer. This overwrite-versus-append concept works the same for all the
named buffers.</P>
<P>If you move the cursor to the end of the file using the <TT>:$</TT> command, you
can then paste the contents of the unnamed buffer to the end of the file. This is
done with the <TT>p</TT> command, which pastes the contents of a buffer to the right
of the cursor (<TT>P</TT> pastes to the left of the cursor). The paste command can
also specify a named buffer in the same way as the yank command, like so:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">"ap
</FONT></PRE>
<P>Yanks can also be performed on words using the command <TT>yw</TT>. This command
also can use named buffers and accepts numeric modifiers.
<CENTER>
<H4><A NAME="Heading21<FONT COLOR="#000077">Searching and Replacing Text</FONT></H4>
</CENTER>
<P>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.</P>
<P>You can use your file <TT>asong</TT> to illustrate searches. To search forward
through <TT>asong</TT> for the word "bay," you would type<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">/bay
</FONT></PRE>
<P>and press Enter. Notice that this is a status-line command. The command <TT>/bay</TT>
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 <TT>/</TT> 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 <TT>/</TT> key, vi
tries to find an instance of the previous string pattern. When it reaches the end
of the file, vi loops back and continues its search at the start of the file.</P>
<P>You can also search backward for strings in vi by using the <TT>?</TT> command.
It works in exactly the same manner as the <TT>/</TT> command, but in the opposite
direction. Try it out by typing<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">?I
</FONT></PRE>
<P>in <TT>asong</TT>, instructing vi to search back for instances of "I."
You can repeat this search by pressing <TT>?</TT>, as you might have suspected. You
can continue a search by pressing <TT>n</TT>, which always continues a search in
the same direction as the preceding search. <TT>N</TT>, however, uses the same search
string but in the opposite direction.</P>
<P>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 (<TT>/</TT> or <TT>?</TT>), but square brackets are added to instruct vi to
perform a regular expression expansion of the enclosed characters. For example, search
forward through <TT>asong</TT> from the first line for all strings containing the
substring "er." Type this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">/er
</FONT></PRE>
<P>vi's first matching string arrives at "Where." If you press <TT>n</TT>,
vi moves the cursor to "where", and so on. You can also specify collections
of characters or ranges of characters to match. Try typing the following command:<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">/[a-z]y
</FONT></PRE>
<P>This command used in <TT>asong</TT> finds 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 is treated as an enumerated range of
ASCII values. Thus, you could also include a range of numbers (for example, <TT>0-9</TT>).
Now try the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">/[Mm]y
</FONT></PRE>
<P>This locates the strings "My" and "my."</P>
<P>In vi, searches without regular expressions 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 might not know exactly
how a pattern appears in a file.</P>
<P>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 <TT>ex</TT> command that starts
with a colon. To search the entire <TT>asong</TT> file for the string "Down"
and replace it with the string "Up," type this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">:%s/Down/Up/g
</FONT></PRE>
<P>The <TT>s</TT> indicates that this is a search operation, the <TT>%</TT> means
that the entire file is to be searched, <TT>Down</TT> is the pattern to be found,
<TT>Up</TT> is the new pattern, and the <TT>g</TT> tells vi that the search should
continue until no more pattern matches are found. Without the <TT>g</TT>, 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.
<CENTER>
<H4><A NAME="Heading22<FONT COLOR="#000077">Setting Preferences</FONT></H4>
</CENTER>
<P>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 <TT>set</TT> command. You must precede
the <TT>set</TT> command with a colon and enter it by pressing Enter. For example,
to display line numbers in the editor, you would issue this command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">:set number
</FONT></PRE>
<P>The following list describes a few of the more common <TT>set</TT> commands:
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"><TT>all</TT> </TD>
<TD ALIGN="LEFT">Displays a list of all available <TT>set</TT> options and their current </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">statuses </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"><TT>errorbells</TT> </TD>
<TD ALIGN="LEFT">Sounds the terminal bell when an error occurs </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"><TT>ignorecase</TT> </TD>
<TD ALIGN="LEFT">Makes searches case-insensitive </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"><TT>number</TT> </TD>
<TD ALIGN="LEFT">Displays line numbers in the leftmost column of the screen </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"> <P>
</TD>
<TD ALIGN="LEFT">(these are not written to the file) </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"><TT>showmode<BR>
</TT></TD>
<TD ALIGN="LEFT">Puts a display in the lower-right portion of the screen thatindicates whether you
are in input mode, change mode, replace mode, and so on<BR>
<BR>
<BR>
<BR>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"> </TD>
<TD ALIGN="LEFT"> </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="105" ALIGN="LEFT"> </TD>
<TD ALIGN="LEFT"> </TD>
</TR>
</TABLE>
<P><TT>set</TT> commands that do not take a value can be switched off via insertion
of <TT>no</TT> as a prefix to the <TT>set</TT> parameter. For example, the command<FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">:set nonumber
</FONT></PRE>
<P>switches line numbering off. The command<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">:set
</FONT></PRE>
<P>shows only the options you have changed.</P>
<P>The settings 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,
you can perform this initialization in another way. Use the vi initialization file
called <TT>.exrc</TT>. 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 <TT>.exrc</TT> file can also
be used to define vi macros.</P>
<P>An example <TT>.exrc</TT> file would look something like this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">set number
set errorbells
set showmode
</FONT></PRE>
<P>Note that the colon is not required before a <TT>set</TT> command in <TT>.exrc</TT>
files.
<CENTER>
<H4><A NAME="Heading23<FONT COLOR="#000077">A Summary of Commands</FONT></H4>
</CENTER>
<P>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.
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>i</TT> </TD>
<TD ALIGN="LEFT">Starts inserting text at the cursor </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>h</TT> </TD>
<TD ALIGN="LEFT">Moves the cursor one character to the left </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>j</TT> </TD>
<TD ALIGN="LEFT">Moves the cursor down one line </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>k</TT> </TD>
<TD ALIGN="LEFT">Moves the cursor up one line </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>l</TT> </TD>
<TD ALIGN="LEFT">Moves the cursor one character to the right </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">C-f </TD>
<TD ALIGN="LEFT">Scrolls forward one screen </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">C-b </TD>
<TD ALIGN="LEFT">Scrolls backward one screen </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">n<TT>dd</TT> </TD>
<TD ALIGN="LEFT">Deletes the next n lines </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">n<TT>yy</TT> </TD>
<TD ALIGN="LEFT">Yanks the next n lines into the unnamed buffer </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>p</TT> </TD>
<TD ALIGN="LEFT">Puts the contents of the unnamed buffer to the right of the cursor </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>u</TT> </TD>
<TD ALIGN="LEFT">Undoes the preceding change </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>:wq</TT> </TD>
<TD ALIGN="LEFT">Writes changes and exits vi </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>:q!</TT> </TD>
<TD ALIGN="LEFT">Exits vi without saving changes </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>:set all</TT> </TD>
<TD ALIGN="LEFT">Shows all <TT>set</TT> parameters and their values </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>/</TT>string<BR>
</TD>
<TD ALIGN="LEFT">Searches forward for string<BR>
</TD>
</TR>
</TABLE>
<CENTER>
<H3><A NAME="Heading24<FONT COLOR="#000077">The emacs Editor</FONT></H3>
</CENTER>
<P>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 various
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.
<CENTER>
<H4><A NAME="Heading25<FONT COLOR="#000077">Starting emacs</FONT></H4>
</CENTER>
<P>emacs is invoked from the command line by the following command:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">emacs
</FONT></PRE>
<P>To start emacs with a file to be edited, enter this:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">emacs filename
</FONT></PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -