304-307.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 155 行

HTML
155
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Text Editors: vi and emacs</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=16//-->

<!--PAGES=304-307//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="299-304.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="307-309.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Starting vi</FONT></H4>

<P>You invoke <TT>vi</TT> from the command line by typing</P>

<!-- CODE SNIP //-->

<PRE>

vi

</PRE>

<!-- END CODE SNIP //-->

<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 will exist in a buffer until you write the contents of the buffer to some named file. The tilde is <TT>vi</TT>&#146;s way of telling you that the line where the tilde appears is empty of text.</P>

<TT>vi</TT> can also be started with a file or a list of files to edit:

<!-- CODE SNIP //-->

<PRE>

vi <I>filename1 filename2 filename3</I> &#133;

</PRE>

<!-- END CODE SNIP //-->

<P>Typically, you will probably edit only one file per <TT>vi</TT> session. If you are editing a list of files, <TT>vi</TT> edits each one in the sequence that they appear on the command line.</P>

<P>Alternatively, <TT>vi</TT> can be invoked from the command line as</P>

<!-- CODE SNIP //-->

<PRE>

vi &#43;<I>n filename</I>

</PRE>

<!-- END CODE SNIP //-->

<P>where <I>n</I> represents the line number where <TT>vi</TT> will place its cursor in <I>filename</I>. This is useful for programmers debugging large source code files who need to quickly jump to a known line containing an error.</P>

<P>Another example is useful in illustrating the <TT>vi</TT> editor. Enter</P>

<!-- CODE SNIP //-->

<PRE>

vi asong

</PRE>

<!-- END CODE SNIP //-->

<P>at the command line and let&#146;s see what happens.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">vi modes</FONT></H4>

<P>At the bottom of the screen in the left corner, you will see

</P>

<!-- CODE SNIP //-->

<PRE>

&#147;asong&#148; 0 lines, 0 characters

</PRE>

<!-- END CODE SNIP //-->

<P>The messages that display on this status line tell you what <TT>vi</TT> is doing or has just done. In this case, <TT>vi</TT> is telling you that it has opened an empty buffer whose contents are saved (whenever you do a save) to the file <TT>asong</TT>.</P>

<P>At this moment, you are in the command mode of <TT>vi</TT>. This is the major conceptual leap required in working with this editor. When editing text, you must remember whether you are in command mode or text mode. In <I>command mode,</I> any character sequences that you enter are interpreted as <TT>vi</TT> commands. In <I>text mode,</I> every character you enter is placed in the buffer and displayed as text onscreen.</P>

<P>Four commands are echoed at the bottom of the screen on the status line:</P>

<CENTER>

<TABLE WIDTH="75%"><TR>

<TD WIDTH="25%">/

<TD WIDTH="75%">Searches forward

<TR>

<TD>?

<TD>Searches backward

<TR>

<TD VALIGN="TOP">:

<TD>An <TT>ex</TT> command (<TT>ex</TT> is a standalone line-based editor used within <TT>vi</TT>)

<TR>

<TD>!

<TD>Invokes a shell command

<TR>

</TABLE>

</CENTER>

<P>Each of these types of status-line commands must be entered by pressing Return. This is not true for other types of <TT>vi</TT> commands, such as the ones that do insertions.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>To find out whether you are in command mode, use the <TT>set showmode</TT> preference described in the section, &#147;Setting Preferences,&#148; later in this chapter.

<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Inserting Text</FONT></H4>

<P>So, knowing that you are in command mode, let&#146;s insert some text. Basically, there are two commands for entering text on the current line: the letters <TT>i</TT> and <TT>a</TT>. These letters in lowercase insert (<TT>i</TT>) text to the left of the cursor or append (<TT>a</TT>) text to the right of the cursor. As with many <TT>vi</TT> commands, the uppercase versions of these letters have similar effects with subtle differences: uppercase <TT>I</TT> and <TT>A</TT> insert and append at the beginning and end of the current line, respectively, regardless of the cursor position.</P>

<P>After you type either of these letters, you are placed in input mode. Any text entered after this point is displayed onscreen.</P>

<P>Type an <TT>i</TT> and enter the following:</P>

<!-- CODE SNIP //-->

<PRE>

Down I walk&lt;Enter&gt;

by the bay,&lt;Enter&gt;

Where I can&lt;Enter&gt;

hear the water.&lt;Enter&gt;

Down we walk&lt;Enter&gt;

by the bay,&lt;Enter&gt;

My hand held&lt;Enter&gt;

by my daughter.&lt;Enter&gt;

</PRE>

<!-- END CODE SNIP //-->

<P>To exit from input mode, press Esc. Notice that the letter <TT>i</TT> does not display before you enter the text, meaning that the <TT>i</TT> was correctly interpreted as a command. Also, it is important to note that it is not necessary to press Return after pressing <TT>i</TT> for input mode.</P>

<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Quitting vi</FONT></H4>

<P>Now that you have some text for your file, let&#146;s quit the editor to see the results. The commands used for saving the file and exiting <TT>vi</TT> are slightly different from the <TT>i</TT> and <TT>a</TT> commands used in editing text; you must precede the command with a colon (<TT>:</TT>).</P>

<P>In this case, you want to do a save and exit, which are actually combined in one command. Enter<TT>:</TT> and a colon appears at the bottom left of your screen. <TT>vi</TT> has recognized that you are about to enter an <TT>ex</TT> command, and it will echo the remaining characters of the command after the colon. Type <TT>wq</TT> and press Return. <TT>vi</TT> quickly informs you that it has written the file to disk and tells you how many lines it contains. <TT>vi</TT> exits, and you find yourself back at the shell prompt. Another way to save and exit is to type <TT>ZZ</TT>. The difference between this method and using <TT>wq</TT> is that <TT>ZZ</TT> writes the file to disk only if it has been modified since the last save.</P>

<P>If no changes have been made to the file you opened, you quit <TT>vi</TT> by simply typing <TT>:q.</TT> This does not work if the file has been modified. If you are sure that you don&#146;t want to save what you have done, enter <TT>:q!</TT>. This command forces <TT>vi</TT> to quit, regardless of any edits.</P>

<P>To make sure that <TT>vi</TT> saved the file <TT>asong</TT> correctly, use the <TT>cat</TT> command to quickly view the file&#146;s contents:</P>

<!-- CODE //-->

<PRE>

% 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.

%

</PRE>

<!-- END CODE //-->

<P>Everything is exactly as you typed it in the file, so no surprises here.

</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="299-304.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="307-309.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?