unx07.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,697 行 · 第 1/5 页
HTM
2,697 行
<P>Quit vi and return to the UNIX prompt.</P>
<TR>
<TD>
<P>:q!</P>
<TD>
<P>Quit vi and return to the system, throwing away any changes made to the file.</P></TABLE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="warning.gif" WIDTH = 37 HEIGHT = 35><B>WARNING:</B> In this table, I've introduced a simple shorthand notation that's worth explaining. UNIX users often use a caret followed by a character instead of the awkward Ctrl+c notation. Therefore, ^f has
the same meaning as Ctrl+F. Expressing this operation as ^f does not change the way it's performed: you'd still press and hold down the Ctrl key and then press the lowercase F key. It's just a shorter notation.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>You've already learned quite a few commands, but you've barely scratched the surface of the powerful vi command!
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I9" NAME="I9">
<FONT SIZE=3><B>Deleting Text</B>
<BR></FONT></A></CENTER></H4>
<P>You now have many of the pieces you need to work efficiently with the vi editor, to zip to any point in the file, or to add text wherever you like. Now you need to learn how to delete characters, words, and lines.
<BR></P>
<P>The simplest form of the delete command is the x command, which functions as though you are writing an X over a letter you don't want on a printed page: it deletes the character under the cursor. Press x five times and you delete five characters.
Deleting a line of text this way can be quite tedious, so vi has some alternate commands. (Are you surprised?) One command that many vi users don't know about is the D, or delete through end of line, command. Wherever you are on a line, pressing D
immediately deletes everything after the cursor to the end of that line of text.
<BR></P>
<P>If there's an uppercase D command, you can just bet there's a lowercase d command too. The d command is the first of a set of more sophisticated vi commands, which are followed by a second command that indicates what you'd like to do with the command.
You already know that w and W move you forward a word in the file; they're known as addressing commands in vi. You can follow d with one of these addressing commands to specify what you would like to delete. For example, to delete a line, simply press dd.
<BR></P>
<P>Sometimes you might get a bit overzealous and delete more than you anticipated. That's not a problem—well, not too much of a problem—because vi remembers the state of the file prior to the most recent action taken. To undo a deletion (or
insertion, for that matter), use the u command. To undo a line of changes, use the U command. Be aware that once you've moved off the line in question, the U command is unable to restore it!
<BR></P>
<P>Start vi again with the big.output file you used earlier:
<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)
"big.output" 40 lines, 1659 characters</PRE>
<P>Press the x key a few times to delete a few characters from the beginning of the file:
<BR></P>
<PRE>gtc 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)</PRE>
<P>Now press u to undo the last deletion:
<BR></P>
<PRE>ngtc 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)</PRE>
<P>If you press u again, what do you think will happen?
<BR></P>
<PRE>gtc 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)</PRE>
<P>The undo command alternates between the last command having happened or not having happened. To explain it a bit better, the undo command is an action unto itself, so the second time you press u, you're undoing the undo command that you just requested.
Press the u key a few more times if you need to convince yourself that this is the case.
<BR></P>
<P>It's time to make some bigger changes to the file. Press dw twice to delete the current word and the next word in the file. It should look something like this after the first dw:
<BR></P>
<PRE>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)</PRE>
<P>Then it should look like this after the second dw:
<BR></P>
<PRE>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)</PRE>
<P>Press u. You see that you can only undo the most recent command. At this point, though, because you haven't moved from the line you're editing, the U, or undo a line of changes, command will restore the line to its original splendor:
<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)</PRE>
<P>Well, in the end, you really don't want to see some of these folk. Fortunately, you can delete lines with the dd command. What if I want to delete the entries for chinese and janitor, both of which are visible on this screen?
<BR></P>
<P>The first step is to use the cursor keys to move down to any place on the line for the chinese account, about halfway down the screen:
<BR></P>
<PRE>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)</PRE>
<P>If your cursor isn't somewhere in the middle of this line, move it so that you too are not at an edge.
<BR></P>
<P>Instead of removing this line completely, perhaps you'd rather just remove the date, time, and name of the system (in parentheses) instead. To accomplish this, you don't need to press dw a bunch of times, or even x a lot of times, but rather just D to
delete through the end of the line:
<BR></P>
<PRE>chenlx2 ttyru Dec 3 21:53 (mentor)
leungtc ttys0 Nov 28 15:11 (gold)
chinese ttys2 _
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)</PRE>
<P>Oh, that's not quite what you wanted to do. No problem, the undo command can fix it. Simply pressing the u key restores the text you deleted:
<BR></P>
<PRE>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)</PRE>
<P>The problem is that you used the wrong command to delete the two entries chinese and janitor from the file. Instead of using the D command, you should use dd. Pressing dd once has these results:
<BR></P>
<PRE>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)
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)</PRE>
<P>Notice that a new line of information has been pulled onto the screen at the bottom to replace the blank line that you removed. If you try using the u command now, what happens? You're almost done. A few presses of the Enter key and you're down to the
entry for the janitor account. Using dd removes that line too:
<BR></P>
<PRE>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)
cdemmert ttys5 Dec 3 23:00 (mentor)
yuenca ttys6 Dec 3 23:00 (mentor)
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)</PRE>
<P>Each line below the one deleted moves up a line to fill in the blank space, and a new line, for tuttleno, moves up from the following screen.
<BR></P>
<P>Now you want to return to the buckaroo file to remedy some of the horrendous typographic errors! It doesn't matter whether you save the changes you've just made to the file, so use :q! to quit, discarding these edit changes to the big.output file.
Entering vi buckaroo starts vi again:
<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 weren't' we informed? What's wrong with him?"
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"buckaroo" 8 lines, 271 characters</PRE>
<P>There are a few fixes you can make in short order. The first is to change "conservation" to "conversation" on the third line. To move there, press the Return key twice and then use W to zip forward until the cursor is at the first
letter of the word you're editing:
<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?"</PRE>
<P>Then use the dw command:
<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 .
"Banzai."
"Where the hell is he?"</PRE>
<P>Now enter insert mode by pressing i and type in the correct spelling of the word "conversation." Then press Esc:
<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 conversation.
"Banzai."
"Where the hell is he?"</PRE>
<P>That's one fix. Now move down a few lines to fix the atrocious misspelling of "hospital":
<BR></P>
<PRE>"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>Again, use dw to delete the word, then i to enter insert mode. Type "hospital" and press Esc, and all is well on the line:
<BR></P>
<PRE>"Banzai."
"Where the hell is he?"
"At the hospital in El paso."
"What? Why weren't' we informed? What's wrong with him?"
~</PRE>
<P>Well, almost all is well. The first letter of "Paso" needs to be capitalized. Move to it by pressing w:
<BR></P>
<PRE>"Banzai."
"Where the hell is he?"
"At the hospital in El paso."
"What? Why weren't' we informed? What's wrong with him?"
~</PRE>
<P>It's time for a secret vi expert command! Instead of pressing x to delete the letter, i to enter insert mode, P as the correct letter, and Esc to return to command mode, there's a much faster way to transpose case: the ~ command. Press the ~ character
once, and here's what happens:
<BR></P>
<PRE>"Banzai."
"Where the hell is he?"
"At the hospital in El Paso."
"What? Why weren't' we informed? What's wrong with him?"
~</PRE>
<P>Cool, isn't it? Back up to the beginning of the word again, using the h command, and press ~ a few times to see what happens. Notice that each time you press ~, the character's case switches (transposes) and the cursor moves to the next character. Press
~ four times and you should end up with this:
<BR></P>
<PRE>"Banzai."
"Where the hell is he?"
"At the hospital in El pASO."
"What? Why weren't' we informed? What's wrong with him?"
~</PRE>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?