unx07.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,697 行 · 第 1/5 页
HTM
2,697 行
<BR></P>
<P>Press the h key once to try to move left. The cursor stays in the same spot and vi beeps to remind you that you can't move left any farther on the line. Try the k key to try to move up; the same thing will happen.
<BR></P>
<P>Now try pressing the j key to move down a character:
<BR></P>
<PRE> total 29
drwx——— 2 taylor 512 Nov 21 10:39 Archives/
drwx——— 3 taylor 512 Dec 3 02:03 InfoWorld/
drwx——— 2 taylor 1024 Dec 3 01:43 Mail/</PRE>
<P>Now the cursor is on the d directory indicator on the second line of the file.
<BR></P>
<P>Press the k key to move back up to the original starting spot.
<BR></P>
<P>Using the four cursor control keys (h, j, k, and l), move around in the file for a little bit until you are comfortable with what's happening on the screen. Now try using the Backspace and Enter keys to see how they help you move around.
<BR></P>
<P>Move to the middle of a line:
<BR></P>
<PRE>total 29
drwx——— 2 taylor 512 Nov 21 10:39 Archives/
drwx——— 3 taylor 512 Dec 3 02:03 InfoWorld/
drwx——— 2 taylor 1024 Dec 3 01:43 Mail/</PRE>
<P>Here you're middle digit in the file size of the second file in the listing. Here are two new cursor motion keys: the 0 (zero) key moves the cursor to the beginning of the line, and $ moves it to the end of the line. First, press 0:
<BR></P>
<PRE>total 29
drwx——— 2 taylor 512 Nov 21 10:39 Archives/
drwx——— 3 taylor 512 Dec 3 02:03 InfoWorld/
drwx——— 2 taylor 1024 Dec 3 01:43 Mail/</PRE>
<P>Now press $ to move to the end of the line:
<BR></P>
<PRE>total 29
drwx——— 2 taylor 512 Nov 21 10:39 Archives/
drwx——— 3 taylor 512 Dec 3 02:03 InfoWorld/
drwx——— 2 taylor 1024 Dec 3 01:43 Mail/</PRE>
<P>If you have arrow keys on your keyboard, try using them to see if they work the same way the h, j, k, and l keys work. If the arrow keys don't move you around, they might have shifted you into insert mode. If you type characters and they're added to the
file, you need to press the Esc key to return to command mode. Wrap this up by leaving this edit session. Because vi now knows that you have modified the file, it will try to ensure that you don't quit without saving the changes:
<BR></P>
<PRE>~
~
:q
No write since last change (:quit! overrides)</PRE>
<P>Use :q! (shorthand for :quit) to quit without saving the changes.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> In general, if you try to use a colon command in vi and the program complains that it might do something bad, try the command again, followed by an exclamation point. This is like saying, "Do it
anyway!"
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Stay in this file for the next section if you'd like, or use :q to quit.
<BR></P>
<P>Moving about a file using these six simple key commands is, on a small scale, much like the entire process of using the vi editor when working with files. Stick with these simple commands until you're comfortable moving around, and you will be well on
your way to becoming proficient with vi.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Moving by Words and Pages</B>
<BR></FONT></A></CENTER></H4>
<P>The description of the EMACS editor mentioned that because it's always in insert mode, all commands must include the Ctrl key. Well, it turns out that vi has its share of Ctrl+key commands, commands that require you to hold down the Ctrl key and press
another key. In this section, you will learn about Ctrl+F, Ctrl+B, Ctrl+U, and Ctrl+D. These move you forward or backward a screen, and up or down half a screen of text, respectively.
<BR></P>
<P>Here are a few more commands: Ctrl+w moves you forward word by word, Ctrl+b moves you backward word by word, and the uppercase versions of these two commands have very similar, but not identical, functions.
<BR></P>
<P>To see how this works, you need to create a file that is larger than your screen. An easy way to do this is to save the output of a common command to a file over and over until the file is long enough. The system I use has lots of users, so I needed to
use the who command just once. You might have to append the output of who to the big.output file a couple of times before the file is longer than 24 lines. (You can check by using wc, of course.)
<BR></P>
<PRE>% who > big.output; wc -l big.output
40
% vi big.output
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 (pc115)
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)
"big.output" 40 lines, 1659 characters</PRE>
<P>Because I have only a 25-line display and the output is 40 lines long (you can see that on the status line at the bottom), there is more information in this file than the screen can display at once.
<BR></P>
<P>To see the next screenful, hold down the Ctrl key, press the F key, and then let both go. In future, when I indicate Ctrl+F that's what I mean you should do.
<BR></P>
<PRE>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)
cq ttysi Dec 3 23:13 (expert)
cdemmert ttysk Dec 3 22:37 (xsun22)
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 (xsun22)
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 (rass)
~
~
~
~</PRE>
<P>Notice that the entire screen was rewritten, and that the familiar tilde characters have popped up again to indicate the end of the file.
<BR></P>
<P>By default, vi has a two-line overlap when you move from screen to screen. If you look carefully, you can see that both eichsted and sweett show up on both screens of data. This doesn't mean that they appear twice in the file; rather, vi is trying to
help you not get lost while you wander through the file.
<BR></P>
<P>Now, what do you think will happen if you press Ctrl+U?
<BR></P>
<PRE>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)
cq ttysi Dec 3 23:13 (expert)
cdemmert ttysk Dec 3 22:37 (xsun22)
jrlee ttysn Dec 3 22:53 (mac1)
fitzgejs ttyso Dec 3 23:18 (dov)</PRE>
<P>The command has moved you up half a screen. Notice where eichsted and sweett are now. Instead of the text being replaced at once, as when you used Ctrl+F, the text was scrolled downward a line at a time, each new line being added as the program went
along. The Ctrl+U command might work either way—one line or an entire screen at a time—for you.
<BR></P>
<P>Now it's time to try moving around in this file word by word. Find the w key on your keyboard and press it once to see what happens.
<BR></P>
<PRE>leungtc ttys0 Nov 28 15:11 (gold)
chinese ttys2 Dec 3 22:53 (excalibur)
cdemmert ttys5 Dec 3 23:00 (mentor)</PRE>
<P>Now press w six times more, noting that the cursor stops three times in the field to indicate what time the user logged into the system (15:11 in this listing). Now your cursor should be sitting on the parenthesized field:
<BR></P>
<PRE>leungtc ttys0 Nov 28 15:11 (gold)
chinese ttys2 Dec 3 22:53 (excalibur)
cdemmert ttys5 Dec 3 23:00 (mentor)</PRE>
<P>It's time to move backward. Press b a few times; your cursor moves backward to the beginning of each word.
<BR></P>
<P>What happens if you try to move backward but you're already on the first word of the line, or you try to move forward but you're already on the last word?
<BR></P>
<P>Using the various keys you've learned, move back to the beginning of the line beginning with leungtc, which you used in the last exercise:
<BR></P>
<PRE>leungtc ttys0 Nov 28 15:11 (gold)
chinese ttys2 Dec 3 22:53 (excalibur)
cdemmert ttys5 Dec 3 23:00 (mentor)</PRE>
<P>This time press the uppercase letter W, rather than the lowercase w, to move through this line. Can you see the difference? Notice what happens when you hit the time field and the parenthesized words. Instead of pressing w seven times to move to the
left parenthesis before gold, you can press W just five times.
<BR></P>
<P>Try moving backward using the B command. Notice that the B command differs from the b command the same way the W command differs from the w command.
<BR></P>
<P>Moving about forward and backward word by word, being able to move half screens or full screens at a time, and being able to zero in on specific spots with the h, j, k, and l cursor-motion keys give you quite a range of motion. Practice using these
commands in various combinations to move your cursor to specific characters in your sample file.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=3><B>Inserting Text into the File with </B><B>i</B><B>, </B><B>a</B><B>, </B><B>o</B><B>, and </B><B>O</B>
<BR></FONT></A></CENTER></H4>
<P>Being able to move around in a file is useful. The real function of an editor, however, is to enable you to easily add and remove—in editor parlance, insert and delete—information. The vi editor has a special insert mode, which you must use in
order to add to the contents of the file. There are four different ways to shift into insert mode, and you will learn about all of them in this section.
<BR></P>
<P>The first way to switch to insert mode is to enter the letter i, which, mnemonically enough, inserts text into the file. The other commands that accomplish more or less the same thing are a, which appends text to the file; o, which opens up a line below
the current line; and O, which opens up a line above the current line.
<BR></P>
<P>This time you want to start with a clean file, so quit from the big.output editing session and start vi again, this time specifying a nonexistent file called buckaroo:
<BR></P>
<PRE>% vi buckaroo
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"buckaroo" [New file]</PRE>
<P>Notice that vi reminds you that this file doesn't exist; the bottom of the screen says New file instead of indicating the number of lines and characters.
<BR></P>
<P>Now it's time to try using insert mode. Press k once:
<BR></P>
<PRE>~
~
~
~</PRE>
<P>The system beeps at you because you haven't moved into insert mode yet, and k still has its command meaning of moving down a line (and of course, there isn't another line yet).
<BR></P>
<P>Press the i key to move into insert mode, and then press the k key again:
<BR></P>
<PRE><B>k_</B>
~
~
~</PRE>
<P>There you go! You've added a character to the file.
<BR></P>
<P>Press the Backspace key, which will move the cursor over the letter k:
<BR></P>
<PRE>k
~
~
~</PRE>
<P>Now see what happens when you press Esc to leave insert mode and return to the vi command mode:
<BR></P>
<PRE>~
~
~
~</PRE>
<P>Notice that the k vanished when you pressed Esc. That's because vi only saves text you've entered to the left of or above the cursor, not the letter the cursor is resting on.
<BR></P>
<P>Now move back into insert mode by pressing i and enter a few sentences from a favorite book of mine:
<BR></P>
<PRE>"He's not even here," went the conservation.
"Banzai."
"Where is he?"
"At a hotpsial in El paso."
"What? Why werent' we informed? What's wrong with him?"_
~
~</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Movie buffs will perhaps recognize that this text comes from the book Buckaroo Banzai. The film The Adventures of Buckaroo Banzai Across the Eighth Dimension is based on this very fun book.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>I've deliberately left some typing errors in the text here. Fixing them will demonstrate some important features of the vi editor. If you fixed them as you went along, that's okay, and if you added errors of your own, that's okay, too!
<BR></P>
<P>Press Esc to leave insert mode. Press Esc a second time to ensure that it worked; remember that vi beeps to remind you that you're already in command mode.
<BR></P>
<P>Now use the cursor motion keys (h, j, k, and l) to move the cursor to any point on the first 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?"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?