?? unx07.htm
字號:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx07.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx08.htm">
<LINK REL="Previous" HREF="unx06.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx06.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx08.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">7 — Text Editing with vi, EMACS, and sed</A></LI>
<UL>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I2">By Dave Taylor</A></LI></UL></UL>
<LI>
<A HREF="#I3">The vi Editor</A></LI>
<UL>
<LI>
<A HREF="#I5">How to Start and Quit vi</A></LI>
<LI>
<A HREF="#I6">Simple Character Motion in vi</A></LI>
<LI>
<A HREF="#I7">Moving by Words and Pages</A></LI>
<LI>
<A HREF="#I8">Inserting Text into the File with i, a, o, and O</A></LI>
<LI>
<A HREF="#I9">Deleting Text</A></LI>
<LI>
<A HREF="#I10">Searching within a File</A></LI>
<LI>
<A HREF="#I11">How to Start vi Correctly</A></LI>
<LI>
<A HREF="#I12">The Key Colon Commands in vi</A></LI>
<LI>
<A HREF="#I13">Advanced vi Tricks, Tools, and Techniques</A></LI>
<LI>
<A HREF="#I14">The Change and Replace Commands</A></LI>
<LI>
<A HREF="#I15">Numeric Repeat Prefixes</A></LI>
<LI>
<A HREF="#I16">Numbering Lines in the File</A></LI>
<LI>
<A HREF="#I17">Search and Replace</A></LI>
<LI>
<A HREF="#I18">Key Mapping with the map Command</A></LI>
<LI>
<A HREF="#I19">Moving Sentences and Paragraphs</A></LI>
<LI>
<A HREF="#I20">Access UNIX with !</A></LI></UL>
<LI>
<A HREF="#I21">The EMACS Editor</A></LI>
<UL>
<LI>
<A HREF="#I22">Launching EMACS and Inserting Text</A></LI>
<LI>
<A HREF="#I23">How to Move Around in a File</A></LI>
<LI>
<A HREF="#I24">How to Delete Characters and Words</A></LI>
<LI>
<A HREF="#I25">Search and Replace in EMACS</A></LI>
<LI>
<A HREF="#I26">Using the EMACS Tutorial and Help System</A></LI>
<LI>
<A HREF="#I27">Working with Other Files</A></LI></UL>
<LI>
<A HREF="#I28">The sed Command</A></LI>
<UL>
<LI>
<A HREF="#I29">Changing Things En Route with sed</A></LI></UL></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><B>7 — Text Editing with </B><B>vi</B><B>, EMACS, and </B><B>sed</B>
<BR></FONT></A></CENTER></H1>
<H5 ALIGN="CENTER">
<CENTER><A ID="I2" NAME="I2">
<FONT SIZE=3><B>By Dave Taylor</B>
<BR></FONT></A></CENTER></H5>
<H3 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=4><A ID="I4" NAME="I4"></A><B>The </B><B>vi</B><B> Editor</B>
<BR></FONT></A></CENTER></H3>
<P>If you like primitive tools, you've already figured out that you can use a combination of << and cat to add lines to a file, and you can use sed and file redirection to modify the contents of a file. These tools are rough and awkward, and when
it's time to either create new files or modify existing ones, you need a screen-oriented editor. In UNIX, the screen editor of choice is called vi.
<BR></P>
<P>There are a number of editors that may be included with your UNIX system, including ed, ex, vi, and EMACS. The latter two use the entire screen, a big advantage, and both are powerful editors. This section focuses on vi, however, because it's easier
and, perhaps more importantly, it's guaranteed to always be part of UNIX. Most vendors omit EMACS, forcing you to find it yourself.
<BR></P>
<P>In this section, you will learn how to start and quit vi, simple character motion in vi, how to move by words and pages, how to insert text into the file, how to search within a file, how to have vi start out right, the key colon commands in vi.
<BR></P>
<P>In some ways, an editor is like another operating system living within UNIX. If you're used to Windows or Macintosh editors, you'll be unhappy to find that vi doesn't know anything about your mouse. Once you spend some time working with vi, however, it
will grow on you. By the end of this section, you will be able to create and modify files on your UNIX system to your heart's content.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=3><B>How to Start and Quit </B><B>vi</B>
<BR></FONT></A></CENTER></H4>
<P>Most UNIX commands do their work, display their results, and quit. Among the few exceptions are more and pg, where you work within the specific program environment until you have viewed the entire contents of the file being shown, or until you quit. The
vi editor is another program in this small category of environments, programs that you move in and use until you explicitly tell the program to quit.
<BR></P>
<P>Before you start vi for the first time, you must learn about two aspects of its behavior. The first is that vi is a modal editor. A mode is like an environment. Different modes in vi interpret the same key differently. For example, if you're in insert
mode, pressing the A key adds an a to the text, whereas in command mode, pressing the A key enters a, a single key abbreviation for the append command. If you ever get confused about what mode you're in, press the Esc key on your keyboard. Pressing Esc
always returns you to the command mode (and if you're already in command mode, it beeps to remind you of that fact).
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> In vi, the Enter key is a specific command (meaning move to the beginning of the next line). As a result, you never need to press Enter to have vi process your command.
<BR></NOTE>
<HR ALIGN=CENTER>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> EMACS is a modeless editor. In EMACS, the A key always adds the letter a to the file. Commands in EMACS are all indicated by holding down the Ctrl key while pressing the command key; for example,
Ctrl+C deletes a character.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The second important characteristic of vi is that it's a screen-oriented program. It must know what kind of terminal, computer, or system you're using to work with UNIX. This probably won't be a problem for you, because most systems are set up so that
the default terminal type matches the terminal or communications program you're using. Here you will learn how to recognize when vi cannot figure out what terminal you're using, and what to do about it.
<BR></P>
<P>You can start vi in a number of different ways, and you will learn about lots of helpful alternatives later. Right now you will learn the basics. The vi command by itself starts the editor, ready for you to create a new file. The vi command with a
filename starts vi with the specified file, so you can modify that file immediately.
<BR></P>
<P>To begin, enter vi at the prompt. If all is working well, the screen will clear, the first character on each line will become a tilde (~), and the cursor will be sitting at the top-left corner of the screen:
<BR></P>
<PRE>% vi
_
~
~
~
~
~
~
~
~
~
~</PRE>
<P>Type a colon character. Doing so moves the cursor to the bottom of the screen and replaces the last tilde with a colon:
<BR></P>
<PRE>~
~
~
~
~
~
~
~
:_</PRE>
<P>Press the q key and the Enter key, and you should be back at the shell prompt:
<BR></P>
<PRE>~
~
~
~
~
~
~
~
:q
%</PRE>
<P>If that operation worked without a problem, go ahead and append your command to your .login or .profile file. If the operation did not work, you received the unknown-terminal-type error message. You might see this on your screen:
<BR></P>
<PRE>% vi
"unknown": Unknown terminal type
I don't know what type of terminal you are on. All I have is "unknown"
[using open mode]
_</PRE>
<P>Alternatively, you might see this:
<BR></P>
<PRE>% vi
Visual needs addressible cursor or upline capability
:</PRE>
<P>Don't panic. You can fix this problem. The first step is to get back to the shell prompt. To do this, do exactly what you did in the first step: type :q and press Enter. You should then see this:
<BR></P>
<PRE>% vi
"unknown": Unknown terminal type
I don't know what type of terminal you are on. All I have is "unknown"
[using open mode]
:q
%</PRE>
<P>The problem here is that vi needs to know the type of terminal you're using, but it can't figure that out on its own. Therefore, you need to tell this to the operating system by setting the TERM environment variable. If you know what kind of terminal
you have, use that value; otherwise, try the default of vt100:
<BR></P>
<PRE>% setenv TERM vt100</PRE>
<P>If you have the $ prompt, which means you're using the Bourne shell (sh) or Korn shell (ksh) rather than the C shell (csh), try this:
<BR></P>
<PRE>$ TERM=vt100 ; export TERM</PRE>
<P>Either way, you can now try entering vi again, and it should work. If it does work, append the command (whichever of these two commands was successful for you) to your .login file if you use csh, or to .profile if you use sh or ksh:
<BR></P>
<PRE>% echo "setenv TERM vt100" >> .login</PRE>
<P>or
<BR></P>
<PRE>$ echo "TERM=vt100 ; export TERM" >> .profile</PRE>
<P>This way, the next time you log in, the system will remember what kind of terminal you're using.
<BR></P>
<P>If this didn't work, it's time to talk with your system administrator about the problem or to call your UNIX vendor to find out what the specific value should be. If you are connected through a modem or other line, and you are actually using a terminal
emulator or communications package, then you might also try using ansi as a TERM setting. If that fails, call the company that makes your software and ask them what terminal type the communications program is emulating.
<BR></P>
<P>Great! You have successfully launched vi, seen what it looks like, and even entered the most important command: the quit command. Now create a simple file and start vi so it shows you the contents of the file:
<BR></P>
<PRE>% ls -l > demo
% vi demo
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/
drwx——— 2 taylor 512 Oct 6 09:36 News/
drwx——— 4 taylor 512 Dec 2 22:08 OWL/
-rw-rw—— 1 taylor 126 Dec 3 16:34 awkscript
-rw-rw—— 1 taylor 165 Dec 3 16:42 bigfiles
drwx——— 2 taylor 512 Oct 13 10:45 bin/
-rw-rw—— 1 taylor 0 Dec 3 22:26 demo
-rw-rw—— 1 taylor 12556 Nov 16 09:49 keylime.pie
-rw-rw—— 1 taylor 8729 Dec 2 21:19 owl.c
-rw-rw—— 1 taylor 199 Dec 3 16:11 sample
-rw-rw—— 1 taylor 207 Dec 3 16:11 sample2
drwx——— 2 taylor 512 Oct 13 10:45 src/
drwxrwx— 2 taylor 512 Nov 8 22:20 temp/
-rw-rw—— 1 taylor 582 Nov 27 18:29 tetme
~
~
~
~
~
~
~
"demo" 17 lines, 846 characters</PRE>
<P>You can see that vi reads the file specified on the command line. In this example, my file is 17 lines long, but my screen can hold 25 lines. To show that some lines lack any text, vi uses the tilde on a line by itself. Finally, note that, at the
bottom, the program shows the name of the file, the number of lines it found in the file, and the total number of characters.
<BR></P>
<P>Type :q again to quit vi and return to the command line for now. When you type the colon, the cursor will flash down to the bottom line and wait for the q, as it did before.
<BR></P>
<P>You have learned the most basic command in vi—the :q command—and survived the experience. It's all downhill from here.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=3><B>Simple Character Motion in </B><B>vi</B>
<BR></FONT></A></CENTER></H4>
<P>Getting to a file isn't much good if you can't actually move around in it. Now you will learn how to use the cursor control keys in vi. To move left one character, press the h key. To move up, press the k key. To move down, press the j key. To move
right a single character, use the l key. You can move left one character by pressing the Backspace key, and you can move to the beginning of the next line with the Enter key.
<BR></P>
<P>Launch vi again, specifying the demo file:
<BR></P>
<PRE>% vi demo
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/
drwx——— 2 taylor 512 Oct 6 09:36 News/
drwx——— 4 taylor 512 Dec 2 22:08 OWL/
-rw-rw—— 1 taylor 126 Dec 3 16:34 awkscript
-rw-rw—— 1 taylor 165 Dec 3 16:42 bigfiles
drwx——— 2 taylor 512 Oct 13 10:45 bin/
-rw-rw—— 1 taylor 0 Dec 3 22:26 demo
-rw-rw—— 1 taylor 12556 Nov 16 09:49 keylime.pie
-rw-rw—— 1 taylor 8729 Dec 2 21:19 owl.c
-rw-rw—— 1 taylor 199 Dec 3 16:11 sample
-rw-rw—— 1 taylor 207 Dec 3 16:11 sample2
drwx——— 2 taylor 512 Oct 13 10:45 src/
drwxrwx— 2 taylor 512 Nov 8 22:20 temp/
-rw-rw—— 1 taylor 582 Nov 27 18:29 tetme
~
~
~
~
~
~
~
"demo" 17 lines, 846 characters</PRE>
<P>You should see the cursor sitting on top the t in total on the first line, or perhaps flashing underneath the t character. Perhaps you have a flashing box cursor or one that shows up in a different color. In any case, that's your starting spot in the
file.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -