unx07.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,697 行 · 第 1/5 页
HTM
2,697 行
~
~</PRE>
<P>It turns out that there's a line of dialogue missing between the line you're on and "Banzai." One way to enter the line would be to move to the beginning of the line "Banzai.", insert the new text, and press Enter before pressing Esc
to quit insert mode. But vi has a special command—o—to open a line immediately below the current line for inserting text. Press o on your keyboard and follow along:
<BR></P>
<PRE>"He's not even here," went the conservation.
_
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Now type the missing text:
<BR></P>
<PRE>"He's not even here," went the conservation.
<B>"Who?"_</B>
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>That's it. Press Esc to return to command mode.
<BR></P>
<P>The problem with the snippet of dialogue you're using is that there's no way to figure out who is talking. Adding a line above this dialogue helps identify the speakers. Again, use cursor motion keys to place the cursor on the top line:
<BR></P>
<PRE>"He's not _even here," went the conservation.
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Now you face a dilemma. You want to open up a line for new text, but you want the line to be above the current line, not below it. It happens that vi can do that, too. Instead of using the o command, use its big brother O instead. When you press O,
here's what you see:
<BR></P>
<PRE>_
"He's not even here," went the conservation.
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Type the new sentence and press Esc.
<BR></P>
<PRE><B>I found myself stealing a peek at my own watch and overhead</B>
<B>General Catbird's </B>
<B>aide give him the latest._</B>
"He's not even here," went the conservation.
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Now the dialogue makes a bit more sense. The conversation, overheard by the narrator, takes place between the general and his aide.
<BR></P>
<P>There are a few words missing in one of the lines, so the next task is to insert them. Use the cursor keys to move the cursor to the fifth line, just after the word "Where":
<BR></P>
<PRE>I found myself stealing a peek at my own watch and overhead
General Catbird's
aide give him the latest.
"He's not even here," went the conservation.
"Banzai."
"Where_is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>At this juncture, you need to add the words "the hell" to make the sentence a bit stronger (and correct). You can use i to insert the text, but then you end up with a trailing space. Instead, you can add text immediately after the current
cursor location by using the a key to append the information. When you press <I>a</I>, the cursor moves one character to the right:
<BR></P>
<PRE>I found myself stealing a peek at my own watch and overhead
General Catbird's
aide give him the latest.
"He's not even here," went the conservation.
"Banzai."
"Where is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Here's where vi can be difficult to use. You're in insert mode, but there's no way for you to know that. When you type the letters you want to add, the screen shows that they are appended. But what if you think you're in insert mode when you're actually
in command mode? One trick you could use to ensure you're in insert mode is to press the command key a second time. If the letter "a" shows up in the text, simply backspace over it; now you know that you're in append mode. When you're done
entering the new characters, and you're still in append mode, here's what your screen looks like:
<BR></P>
<PRE>I found myself stealing a peek at my own watch and overhead
General Catbird's
aide give him the latest.
"He's not even here," went the conservation.
"Banzai."
"Where the hell is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~
~</PRE>
<P>Notice that the cursor stayed on the "i" in "is" throughout this operation. Press Esc to return to command mode. Notice that the cursor finally hops off the "i" and moves left one character.
<BR></P>
<P>To differentiate between the i and a commands, remember that the insert command always adds the new information immediately before the cursor, whereas append adds the information immediately after the cursor.
<BR></P>
<P>With this in mind, try to fix the apostrophe problem in the word "werent'" on the last line. Move the cursor to the "n" in that word:
<BR></P>
<PRE>"Where the hell is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~</PRE>
<P>Now, to put the apostrophe immediately after the current character, do you want to use the insert command (i) or the append command? If you said "Append," give yourself a pat on the back! Press a to append the apostrophe:
<BR></P>
<PRE>"Where the hell is he?"
"At the hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"
~</PRE>
<P>Press the ' key once and press Esc.
<BR></P>
<P>Quit vi. Use :q, and the program reminds you that you haven't saved your changes to this new file:
<BR></P>
<PRE>~
~
No write since last change (:quit! overrides)</PRE>
<P>To write the changes, you need a new command, so I'll give you a preview of a set of colon commands you will learn later in this chapter. Type : (the colon character), which moves the cursor to the bottom of the screen.
<BR></P>
<PRE>~
~
:_</PRE>
<P>Now press w to write out the file, and then press the Enter key:
<BR></P>
<PRE>~
~
"buckaroo" 8 lines, 271 characters</PRE>
<P>It's okay to leave vi now. Use :q to quit and you're safely back at the command prompt. A quick cat confirms that the tildes were not included in the file itself:
<BR></P>
<PRE>%
% <B>cat buckaroo</B>
I found myself stealing a peek at my own watch and overhead
General Catbird's
aide give him the latest.
"He's not even here," went the conservation.
"Banzai."
"Where the hell is he?"
"At the hotpsial in El paso."
"What? Why weren't' we informed? What's wrong with him?"
%</PRE>
<P>As you can tell, the vi editor is quite powerful, and it has a plethora of commands. Just moving about and inserting text, you have learned 24 commands, as summarized in Table 7.1.
<BR></P>
<UL>
<LH><B>Table 7.1. Summary of </B><B>vi</B><B> motion and insertion commands.</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Command</I>
<BR></PRE>
<TD>
<PRE><I>Meaning</I>
<BR></PRE>
<TR>
<TD>
<P>0</P>
<TD>
<P>Move to beginning of line.</P>
<TR>
<TD>
<P>$</P>
<TD>
<P>Move to end of line.</P>
<TR>
<TD>
<P>a</P>
<TD>
<P>Append text—move into insert mode after the current character.</P>
<TR>
<TD>
<P>^b</P>
<TD>
<P>Back up one screen of text.</P>
<TR>
<TD>
<P>B</P>
<TD>
<P>Back up one space-delimited word.</P>
<TR>
<TD>
<P>b</P>
<TD>
<P>Back up one word.</P>
<TR>
<TD>
<P>Backspace</P>
<TD>
<P>Move left one character.</P>
<TR>
<TD>
<P>^d</P>
<TD>
<P>Move down half a page.</P>
<TR>
<TD>
<P>Esc</P>
<TD>
<P>Leave insert mode, return to command mode.</P>
<TR>
<TD>
<P>^f</P>
<TD>
<P>Move forward one screen of text.</P>
<TR>
<TD>
<P>h</P>
<TD>
<P>Move left one character.</P>
<TR>
<TD>
<P>i</P>
<TD>
<P>Insert text—move into insert mode before the current character.</P>
<TR>
<TD>
<P>j</P>
<TD>
<P>Move down one line.</P>
<TR>
<TD>
<P>k</P>
<TD>
<P>Move up one line.</P>
<TR>
<TD>
<P>l</P>
<TD>
<P>Move right one character.</P>
<TR>
<TD>
<P>O</P>
<TD>
<P>Open new line for insert above the current line.</P>
<TR>
<TD>
<P>o</P>
<TD>
<P>Open new line for insert below the current line.</P>
<TR>
<TD>
<P>Enter</P>
<TD>
<P>Move to beginning of next line.</P>
<TR>
<TD>
<P>^u</P>
<TD>
<P>Move up half a page.</P>
<TR>
<TD>
<P>W</P>
<TD>
<P>Move forward one space-delimited word.</P>
<TR>
<TD>
<P>w</P>
<TD>
<P>Move forward one word.</P>
<TR>
<TD>
<P>:w</P>
<TD>
<P>Write the edit buffer to the system.</P>
<TR>
<TD>
<P>:q</P>
<TD>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?