⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch16.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE><FONT COLOR="#0066FF">vi +n filename



</FONT></PRE>



<P>where n represents the line number where vi will place its cursor in filename.



This alternative is useful for programmers debugging large source-code files who



need to jump quickly to a known line containing an error.</P>



<P>Another example will be of use in illustrating the vi editor. If you still have



a vi session on your screen, exit it by pressing Esc and then typing <TT>:q!</TT>.



To start a new vi session, enter<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">vi asong



</FONT></PRE>



<P>at the command line.



<H4 ALIGN="CENTER"><A NAME="Heading13<FONT COLOR="#000077">vi Modes</FONT></H4>



<P>At the bottom of the screen in the left corner, you will see this:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">&quot;asong&quot; [NEW FILE] 1 lines, 1 characters



</FONT></PRE>



<P>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 <TT>asong</TT>.</P>



<P>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 whether



you are in command mode or text mode. In command mode, any character sequences you



enter are interpreted as vi commands. In text mode, every character typed is placed



in the buffer and displayed as text on-screen.</P>



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



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="36" ALIGN="LEFT"><TT>/</TT> </TD>



		<TD ALIGN="LEFT">Searches forward </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="36" ALIGN="LEFT"><TT>?</TT> </TD>



		<TD ALIGN="LEFT">Searches backward </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="36" ALIGN="LEFT"><TT>:</TT> </TD>



		<TD ALIGN="LEFT">Is an <TT>ex</TT> command (<TT>ex</TT> is a standalone line-based editor used within



			vi) </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="36" ALIGN="LEFT"><TT>!<BR>



			</TT></TD>



		<TD ALIGN="LEFT">Invokes a shell command<BR>



					</TD>



	</TR>



</TABLE>



You enter each of these types of status-line commands by pressing Enter. This is



not true for other types of vi commands, such as the ones that perform insertions.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading14<FONT COLOR="#000077"><B>TIP:</B> </FONT>To find out whether



	you are in command mode, use the <TT>set showmode</TT> preference described in the



	section titled &quot;Setting Preferences,&quot; later in this chapter.



<HR>







</DL>







<CENTER>



<H4><A NAME="Heading15<FONT COLOR="#000077">Inserting Text</FONT></H4>



</CENTER>



<P>So knowing that you are in command mode, let's insert some text. Basically, two



commands can be used for entering text on the current line: the letters <TT>i</TT>



and <TT>a</TT>. These lowercase letters 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 vi 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.</P>



<P>After you press either of these letters, you are placed in input mode. Any text



entered after this point is displayed on-screen.</P>



<P>Press <TT>i</TT> and then type the following text:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">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;



</FONT></PRE>



<P>To exit from input mode, press Esc. Notice that you did not see the letter <TT>i</TT>



displayed before you entered the text, meaning that the <TT>i</TT> was correctly



interpreted as a command. Also, it is important to note that it was not necessary



to press Enter after pressing <TT>i</TT> for input mode.



<CENTER>



<H4><A NAME="Heading16<FONT COLOR="#000077">Quitting vi</FONT></H4>



</CENTER>



<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 <TT>i</TT> and <TT>d</TT> commands used in editing text: you must precede the



command with a colon (<TT>:</TT>).</P>



<P>In this case, you want to perform a save and exit, which are actually combined



in one command. Press <TT>:</TT>. In the lower-left part of your screen, you will



notice that a colon has appeared. vi 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 Enter. vi quickly informs you that it has written



the file to disk and tells you how many lines the file contains. If the file is small



and you have a fast system, this message might appear and be erased so quickly that



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 <TT>ZZ</TT>. The difference between this method and using



<TT>wq</TT> is that <TT>ZZ</TT> writes the file only if it has been modified since



the last save.</P>



<P>You can quit vi by typing <TT>:q</TT> if no changes have been made to the file



you opened. This method doesn't work if the file has been modified. If you are sure



you don't want to save what you have done, enter <TT>:q!</TT>. This command forces



vi to quit, regardless of any edits.</P>



<P>To make sure that vi saved the file <TT>asong</TT> correctly, use the <TT>cat</TT>



command to quickly view the file's contents:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$ 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 there are no surprises here.



<CENTER>



<H4><A NAME="Heading17<FONT COLOR="#000077">Moving the Cursor</FONT></H4>



</CENTER>



<P>Moving the cursor around in vi essentially involves the following four keys: 



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="79" ALIGN="LEFT">h </TD>



		<TD ALIGN="LEFT">Moves the cursor one space to the left </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="79" ALIGN="LEFT">j </TD>



		<TD ALIGN="LEFT">Moves the cursor down one line </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="79" ALIGN="LEFT">k </TD>



		<TD ALIGN="LEFT">Moves the cursor up one line </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="79" ALIGN="LEFT">l<BR>



					</TD>



		<TD ALIGN="LEFT">Moves the cursor one space to the right<BR>



					</TD>



	</TR>



</TABLE>



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.</P>



<P>vi enables you to move through a file in bigger &quot;leaps&quot; as well. Following



are some commands for scrolling more than one line at a time: 



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT">Ctrl </TD>



		<TD ALIGN="LEFT">U Scrolls up a half screen </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT">Ctrl </TD>



		<TD ALIGN="LEFT">D Scrolls down a half screen </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT">Ctrl </TD>



		<TD ALIGN="LEFT">F Scrolls down one full screen </TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT">Ctrl<BR>



					</TD>



		<TD ALIGN="LEFT">B Scrolls up one full screen<BR>



					</TD>



	</TR>



</TABLE>



The size of these movements largely depends on the terminal settings.</P>



<P>It is also possible to move the cursor to a specific line in a file. If you want



to move to the 10th line, type <TT>10G</TT> or <TT>:10</TT> in command mode. <TT>G</TT>



by itself moves the cursor to the end of the file. The cursor does not move if the



number given is not applicable (for example, typing <TT>:10</TT> in an eight-line



file has no effect).</P>



<P>vi also enables 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, press <TT>w</TT>. Press <TT>b</TT> to move



the cursor to the beginning of the current or preceding word or punctuation mark.



<CENTER>



<H4><A NAME="Heading18<FONT COLOR="#000077">Deleting Text</FONT></H4>



</CENTER>



<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.</P>



<P>To delete a word, use the <TT>dw</TT> command. If you want to delete the word



to the right of the cursor, type <TT>dw</TT>. If you are in the middle of a word,



this command deletes from the cursor position to the end. You can also delete several



words at a time. For example, the command <TT>4dw</TT> deletes the next four words



on the current line.</P>



<P>You can delete lines individually or specify a range of lines to delete. To delete



the current line, type <TT>dd</TT>. The command <TT>4dd</TT> deletes four lines (the



current line and three below it). <TT>dG</TT> deletes all lines from the current



one to the end of the file.</P>



<P>On the current line, you can delete in either direction: <TT>d^</TT> deletes backward



to the beginning of the line, and <TT>d$</TT> (or <TT>D</TT>) deletes forward to



the end of the line.</P>



<P>To delete individual characters, <TT>x</TT> deletes the character underneath the



cursor, and <TT>X</TT> deletes the character to the left of the cursor. Both of these



commands accept a number modifier: for example, <TT>4x</TT> deletes the current character



and the four characters to the right.</P>



<P>Unwanted changes such as deletions can be immediately undone by the <TT>u</TT>



command. This &quot;rolls back&quot; the last edit made.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading19<FONT COLOR="#000077"><B>TIP: </B></FONT>Not sure what command



	you just typed? When in doubt, press Esc and then enter the command again.



<HR>







</DL>







<CENTER>



<H4><A NAME="Heading20<FONT COLOR="#000077">Copying and Moving Text</FONT></H4>



</CENTER>



<P>Moving sections of text around in a file basically requires three steps:







<DL>



	<DD><B>1. </B>Yank the text into a buffer.<BR>



	<B><BR>



	2.</B> Move the cursor to where you want to insert the text.<BR>



	<B><BR>



	3. </B>Place the text from the buffer at the new location.



</DL>







<P>Yanking text means to copy it into either a named or an 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.</P>



<P>To yank the current line into the unnamed buffer, the command is <TT>yy</TT> or



<TT>Y</TT>. These commands can be modified by a number indicating how many lines



beneath the cursor are to be yanked. For example, the command <TT>3yy</TT> in your



file <TT>asong</TT> (with the cursor on the top line) yanks the following text into



the temporary buffer:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Down I walk



by the bay,



Where I can



</FONT></PRE>



<P>This text could also be yanked into the named buffer <TT>a</TT> by the following



command:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">&quot;a3yy

⌨️ 快捷键说明

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