?? rhl16.htm
字號:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
/
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Searches forward.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
?
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Searches backward.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
:
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An ex command (ex is a standalone line-based editor used within vi).</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
!
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Invokes a shell command.</FONT>
</TABLE><P>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.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<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.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E216"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Inserting Text</B></FONT></CENTER></H4>
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>Type an i and then type the following:
<BR>
<PRE>
<FONT COLOR="#000080">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></FONT></PRE>
<P>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.
<BR>
<BR>
<A NAME="E69E217"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Quitting vi</B></FONT></CENTER></H4>
<BR>
<P>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
(:).
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>To make sure that vi saved the file asong correctly, use the cat command to quickly view the file's contents:
<BR>
<PRE>
<FONT COLOR="#000080">$ 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.
$</FONT></PRE>
<P>Everything is exactly as you typed it in the file, so no surprises here.
<BR>
<BR>
<A NAME="E69E218"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Moving the Cursor</B></FONT></CENTER></H4>
<BR>
<P>Moving the cursor around in vi essentially involves the following four keys:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
h
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Moves the cursor one space to the left.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
j
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Moves the cursor down one line.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
k
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Moves the cursor up one line.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
l
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Moves the cursor one space to the right.</FONT>
</TABLE><P>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.
<BR>
<P>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:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Ctrl-U
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Scrolls up a half-screen.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Ctrl-D
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Scrolls down a half-screen.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Ctrl-F
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Scrolls down one full screen.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Ctrl-B
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Scrolls up one full screen.</FONT>
</TABLE><P>The size of these movements largely depends on the terminal settings.
<BR>
<P>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).
<BR>
<P>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.
<BR>
<BR>
<A NAME="E69E219"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Deleting Text</B></FONT></CENTER></H4>
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>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.
<BR>
<P>Unwanted changes such as deletions can be immediately undone by the u command. This "rolls back" the last edit made.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Not sure what command you just typed? When in doubt, press Esc and then enter the command again.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E220"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Copying and Moving Text</B></FONT></CENTER></H4>
<BR>
<P>Moving sections of text around in a file basically requires three steps:
<BR>
<OL>
<LI>Yank the text into a buffer.
<BR>
<BR>
<LI>Move the cursor to where you want to insert the text.
<BR>
<BR>
<LI>Place the text from the buffer at the new location.
<BR>
<BR>
</OL>
<P>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.
<BR>
<P>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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">3yy</FONT></PRE>
<P>in your file asong (with the cursor on the top line) yanks the following text into the temporary buffer:
<BR>
<PRE>
<FONT COLOR="#000080">Down I walk
by the bay,
Where I can</FONT></PRE>
<P>This text could also be yanked into the named buffer a by the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">"a3yy</FONT></PRE>
<P>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.
<BR>
<P>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:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">"ap</FONT></PRE>
<P>Yanks can also be performed on words using the command yw. This command can also use named buffers and accepts numeric modifiers.
<BR>
<BR>
<A NAME="E69E221"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Searching and Replacing Text</B></FONT></CENTER></H4>
<BR>
<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.
<BR>
<P>You can use your file asong to illustrate searches. To search forward through asong for the word "bay," you would type
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/bay</FONT></PRE>
<P>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.
<BR>
<P>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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">?I</FONT></PRE>
<P>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.
<BR>
<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 (/ 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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/er</FONT></PRE>
<P>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:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -