unx07.htm

来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,697 行 · 第 1/5 页

HTM
2,697
字号

<P>Back up to the beginning of the word and press ~ four more times until the word is correct.

<BR></P>

<P>One more slight change and the file is fixed! Move to the last line of the file, to the extra apostrophe in the word &quot;weren't',&quot; and use the x key to delete the offending character. The screen should now look like this:

<BR></P>

<PRE>I found myself stealing a peek at my own watch and overhead

General Catbird's

aide give him the latest.

&quot;He's not even here,&quot; went the conversation.

&quot;Banzai.&quot;

&quot;Where the hell is he?&quot;

&quot;At the hospital in El Paso.&quot;

&quot;What? Why weren't we informed? What's wrong with him?&quot;

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~</PRE>

<P>That looks great! It's time to save it for posterity. Use :wq, a shortcut that has vi write out the changes, and then immediately quit the program:

<BR></P>

<PRE>~

~

~

&quot;buckaroo&quot; 8 lines, 270 characters

%</PRE>

<P>Not only have you learned about the variety of deletion options in vi, but you have also learned a few simple shortcut commands: ~, which transposes case, and :wq, which writes out the changes and quits the program all in one step.

<BR></P>

<P>You should feel pleased; you're now a productive and knowledgeable vi user, and you can modify files, making easy or tough changes. Go back to your system and experiment further, modifying some of the other files. Be careful, though, not to make changes 

in any of your dot files (for example, .cshrc), lest you cause trouble that would be difficult to fix!

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Searching within a File</B>

<BR></FONT></A></CENTER></H4>

<P>With the addition of two more capabilities, you'll be ready to face down any vi expert, demonstrating your skill and knowledge of the editor. Much more importantly, you'll be able to really fly through files, moving immediately to the information you 
desire.

<BR></P>

<P>The two new capabilities are for finding specific words or phrases in a file and for moving to specific lines in a file. Similar to searching for patterns in more and page, /<I>pattern</I> searches forward in the file for a specified pattern, and 
?<I>pattern</I> searches backward for the specified pattern. To repeat the previous search, use the n command to tell vi to search again, in the same direction, for the same pattern.

<BR></P>

<P>You can easily move to any specific line in a file using the G, or go to line, command. If you press a number before you press G, the cursor will move to that line in the file. If you press G without a line number, the cursor will zip you to the very 
last line of the file by default.

<BR></P>

<P>Start vi again with the big.output file:

<BR></P>

<PRE>leungtc  ttyrV   Dec  1 18:27   (magenta)

tuyinhwa ttyrX   Dec  3 22:38   (expert)

hollenst ttyrZ   Dec  3 22:14   (dov)

brandt   ttyrb   Nov 28 23:03   (age)

holmes   ttyrj   Dec  3 21:59   (age)

yuxi     ttyrn   Dec  1 14:19   (pc)

frodo    ttyro   Dec  3 22:01   (mentor)

labeck   ttyrt   Dec  3 22:02   (dov)

chenlx2  ttyru   Dec  3 21:53   (mentor)

leungtc  ttys0   Nov 28 15:11   (gold)

chinese  ttys2   Dec  3 22:53   (excalibur)

cdemmert ttys5   Dec  3 23:00   (mentor)

yuenca   ttys6   Dec  3 23:00   (mentor)

janitor  ttys7   Dec  3 18:18   (age)

mathisbp ttys8   Dec  3 23:17   (dov)

janitor  ttys9   Dec  3 18:18   (age)

cs541    ttysC   Dec  2 15:16   (solaria)

yansong  ttysL   Dec  1 14:44   (math)

mdps     ttysO   Nov 30 19:39   (localhost)

md       ttysU   Dec  2 08:45   (muller)

jac      ttysa   Dec  3 18:18   (localhost)

eichsted ttysb   Dec  3 23:21   (pc1)

sweett   ttysc   Dec  3 22:40   (dov)

&quot;big.output&quot; 40 lines, 1659 characters</PRE>

<P>Remember that you used :q! to quit earlier, so your changes were not retained.

<BR></P>

<P>To move to the very last line of the file, press G once:

<BR></P>

<PRE>cdemmert ttysk   Dec  3 22:37   (xsun)

jrlee    ttysn   Dec  3 22:53   (mac1)

fitzgejs ttyso   Dec  3 23:18   (dov)

doerrhb  ttysp   Dec  3 23:20   (dov)

cdemmert ttysq   Dec  3 23:00   (xsun)

frazierw ttysr   Dec  3 23:01   (dov)

buckeye  ttyss   Dec  3 23:20   (mac2)

mtaylor  ttyst   Dec  3 23:22   (dov)

look     ttysu   Dec  3 23:12   (age)

janitor  ttysw   Dec  3 18:29   (age)

ajones   ttysx   Dec  3 23:23   (rassilon)

~

~

~

~

~

~

~

~

~

~

~

~</PRE>

<P>To move to the third line of the file, press 3 followed by G:

<BR></P>

<PRE>leungtc  ttyrV   Dec  1 18:27   (magenta)

tuyinhwa ttyrX   Dec  3 22:38   (expert)

hollenst ttyrZ   Dec  3 22:14   (dov)

brandt   ttyrb   Nov 28 23:03   (age)

holmes   ttyrj   Dec  3 21:59   (age)

yuxi     ttyrn   Dec  1 14:19   (pc)

frodo    ttyro   Dec  3 22:01   (mentor)

labeck   ttyrt   Dec  3 22:02   (dov)

chenlx2  ttyru   Dec  3 21:53   (mentor)

leungtc  ttys0   Nov 28 15:11   (gold)

chinese  ttys2   Dec  3 22:53   (excalibur)

cdemmert ttys5   Dec  3 23:00   (mentor)

yuenca   ttys6   Dec  3 23:00   (mentor)

janitor  ttys7   Dec  3 18:18   (age)

mathisbp ttys8   Dec  3 23:17   (dov)

janitor  ttys9   Dec  3 18:18   (age)

cs541    ttysC   Dec  2 15:16   (solaria)

yansong  ttysL   Dec  1 14:44   (math)

mdps     ttysO   Nov 30 19:39   (localhost)

md       ttysU   Dec  2 08:45   (muller)

jac      ttysa   Dec  3 18:18   (localhost)

eichsted ttysb   Dec  3 23:21   (pc1)

sweett   ttysc   Dec  3 22:40   (dov)</PRE>

<P>Notice that the cursor is on the third line of the file.

<BR></P>

<P>Now it's time to search. From your previous travels in this file, you know that the very last line is for the account ajones, but instead of using G to move there directly, you can search for the specified pattern by using the / search command.

<BR></P>

<P>Pressing / immediately moves the cursor to the bottom of the screen:

<BR></P>

<PRE>md       ttysU   Dec  2 08:45   (mueller)

jac      ttysa   Dec  3 18:18   (localhost)

eichsted ttysb   Dec  3 23:21   (pc1)

sweett   ttysc   Dec  3 22:40   (dov)

/_</PRE>

<P>Now you can type in the pattern <I>ajones</I>:

<BR></P>

<PRE>md       ttysU   Dec  2 08:45   (mueller)

jac      ttysa   Dec  3 18:18   (localhost)

eichsted ttysb   Dec  3 23:21   (pc1)

sweett   ttysc   Dec  3 22:40   (dov)

/ajones_</PRE>

<P>When you press Return, vi spins through the file and moves you to the first line it finds that contains the specified pattern:

<BR></P>

<PRE>cdemmert ttysk   Dec  3 22:37   (xsun)

jrlee    ttysn   Dec  3 22:53   (mac1)

fitzgejs ttyso   Dec  3 23:18   (dov)

doerrhb  ttysp   Dec  3 23:20   (dov)

cdemmert ttysq   Dec  3 23:00   (xsun)

frazierw ttysr   Dec  3 23:01   (dov)

buckeye  ttyss   Dec  3 23:20   (mac2)

mtaylor  ttyst   Dec  3 23:22   (dov)

look     ttysu   Dec  3 23:12   (age)

janitor  ttysw   Dec  3 18:29   (age)

ajones   ttysx   Dec  3 23:23   (rassilon)

~

~

~

~

~

~

~

~

~

~

~

~</PRE>

<P>If you press n to search for this pattern again, a slash appears at the very bottom line to show that vi understood your request. But the cursor stays exactly where it is, which indicates that this is the only occurrence of the pattern in this file.

<BR></P>

<P>You notice that the account janitor has all sorts of sessions running. To search backward for occurrences of their account, use the ? command:

<BR></P>

<PRE>~

~

?janitor_</PRE>

<P>The first search moves the cursor up one line, which leaves the screen looking almost the same:

<BR></P>

<PRE>cdemmert ttysk   Dec  3 22:37   (xsun)

jrlee    ttysn   Dec  3 22:53   (mac1)

fitzgejs ttyso   Dec  3 23:18   (dov)

doerrhb  ttysp   Dec  3 23:20   (dov)

cdemmert ttysq   Dec  3 23:00   (xsun)

frazierw ttysr   Dec  3 23:01   (dov)

buckeye  ttyss   Dec  3 23:20   (mac2)

mtaylor  ttyst   Dec  3 23:22   (dov)

look     ttysu   Dec  3 23:12   (age)

janitor  ttysw   Dec  3 18:29   (age)

ajones   ttysx   Dec  3 23:23   (rassilon)

~

~

~

~

~

~

~

~

~

~

~

~

?janitor</PRE>

<P>Here's where n, or next search, can come in handy. If you press n this time and there is another occurrence of the pattern in the file, vi moves you directly to the match:

<BR></P>

<PRE>yuxi     ttyrn   Dec  1 14:19   (pc)

frodo    ttyro   Dec  3 22:01   (mentor)

labeck   ttyrt   Dec  3 22:02   (dov)

chenlx2  ttyru   Dec  3 21:53   (mentor)

leungtc  ttys0   Nov 28 15:11   (gold)

chinese  ttys2   Dec  3 22:53   (excalibur)

cdemmert ttys5   Dec  3 23:00   (mentor)

yuenca   ttys6   Dec  3 23:00   (mentor)

janitor  ttys7   Dec  3 18:18   (age)

mathisbp ttys8   Dec  3 23:17   (dov)

janitor  ttys9   Dec  3 18:18   (age)

cs541    ttysC   Dec  2 15:16   (solaria)

yansong  ttysL   Dec  1 14:44   (math)

mdps     ttysO   Nov 30 19:39   (localhost)

md       ttysU   Dec  2 08:45   (muller)

jac      ttysa   Dec  3 18:18   (localhost)

eichsted ttysb   Dec  3 23:21   (pc1)

sweett   ttysc   Dec  3 22:40   (dov)

wellman  ttysd   Dec  3 23:01   (dov)

tuttleno ttyse   Dec  3 23:03   (indyvax)

wu       ttysf   Dec  3 23:10   (term01)

daurismj ttysg   Dec  3 23:10   (dov)

cs414    ttysh   Dec  3 23:12   (xds)</PRE>

<P>When you're done, quit vi by using :q.

<BR></P>

<P>There are not dozens, but hundreds of commands in vi. Rather than overwhelm you with all of them, even in a table, I have opted instead to work with the most basic and important commands.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>How to Start </B><B>vi</B><B> Correctly</B>

<BR></FONT></A></CENTER></H4>

<P>The vi command wouldn't be part of UNIX if it didn't have some startup options available, but there really are only two worth mentioning. The -R flag sets up vi as a read-only file to ensure that you don't accidentally modify a file. The second option 
doesn't start with a hyphen, but with a plus sign: any command following the plus sign is used as an initial command to the program. This is more useful than it may sound. The command vi +$ sample, for example, starts the editor at the bottom of the file 
sample, and vi +17 sample starts the editor on the 17th line of sample.

<BR></P>

<P>First, this is the read-only format:

<BR></P>

<PRE>% vi -R buckaroo

I found myself stealing a peek at my own watch and overhead

General Catbird's 

aide give him the latest.

&quot;He's not even here,&quot; went the conversation.

&quot;Banzai.&quot;

&quot;Where the hell is he?&quot;

&quot;At the hospital in El Paso.&quot;

&quot;What? Why weren't we informed? What's wrong with him?&quot;

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

&quot;buckaroo&quot; [Read only] 8 lines, 270 characters</PRE>

<P>Notice the addition of the [Read only] message on the status line. You can edit the file, but if you try to save the edits

⌨️ 快捷键说明

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