rework initial error handling -- since we check for any screens on
[nvi.git] / docs / edit / edittut.ms
blob0f6b4df76157a7eb020e0f157df728040c0538ad
1 .\" Copyright (c) 1980, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %sccs.include.redist.roff%
5 .\"
6 .\"     $Id: edittut.ms,v 8.1 1993/06/08 12:48:42 bostic Exp $ (Berkeley) $Date: 1993/06/08 12:48:42 $
7 .\"
8 .EH 'USD:11-%''Edit:  A Tutorial'
9 .OH 'Edit:  A Tutorial''USD:11-%'
10 .LP
11 .ds u \s-2UNIX\s0
12 .ll 5i
13 .nr LL 5i
14 .ND
15 .sp 4
16 .ce
17 \f3\s+2Edit:  A Tutorial\s0\f1
18 .sp
19 .ce 3
21 Ricki Blau
22 .sp
23 James Joyce
25 .sp
26 .ce 3
27 Computing Services
28 University of California
29 Berkeley, California 94720
30 .sp 3
31 .ce
33 ABSTRACT
35 .sp
36 .LP
37 This narrative introduction to the use of the text editor
38 .I edit
39 assumes no prior familiarity with computers or with text editing.
40 Its aim is to lead the beginning \s-2UNIX\(dg\s+2 user through the
41 .FS
42 \(dgUNIX is a trademark of Bell Laboratories.
43 .FE
44 fundamental steps of writing and revising a file of text.
45 Edit,
46 a version of the text editor
47 .I ex,
48 was designed to provide an informative environment
49 for new and casual users.
50 .PP
51 We welcome comments and suggestions about this tutorial
52 and the \s-2UNIX\s+2 documentation in general.
53 .sp .5v
54 September 1981
55 .bp
56 .ll 6.5i
57 .nr LL 6.5i
58 .nr LT 6.5i
59 .ds u \s-2UNIX\s0
60 .ce
61 \s+2\f3Contents\f1\s0
62 .LP
63 .nf
64 Introduction\ \ \ 3
65 .sp
66 Session 1\ \ \4
67 .in +.5i
68 Making contact with \s-2UNIX\s+2\ \ \ 4
69 Logging in\ \ \4
70 Asking for \fIedit\fR\ \ \ 4
71 The ``Command not found'' message\ \ \ 5
72 A summary\ \ \5
73 Entering text\ \ \ 5
74 Messages from \fIedit\fR\ \ \ 5
75 Text input mode\ \ \ 6
76 Making corrections\ \ \ 6
77 Writing text to disk\ \ \ 7
78 Signing off\ \ \7
79 .in -.5i
80 .sp
81 Session 2\ \ \ 8
82 .in +.5i
83 Adding more text to the file\ \ \ 8
84 Interrupt\ \ \ 8
85 Making corrections\ \ \ 8
86 Listing what's in the buffer (p)\ \ \ 9
87 Finding things in the buffer\ \ \ 9
88 The current line\ \ \ 10
89 Numbering lines (nu)\ \ \ 10
90 Substitute command (s)\ \ \ 10
91 Another way to list what's in the buffer (z)\ \ \ 11
92 Saving the modified text\ \ \ 12
93 .in -.5i
94 .sp
95 Session 3\ \ \ 13
96 .in +.5i
97 Bringing text into the buffer (e)\ \ \ 13
98 Moving text in the buffer (m)\ \ \ 13
99 Copying lines (copy)\ \ \ 14
100 Deleting lines (d)\ \ \ 14
101 A word or two of caution\ \ \ 15
102 Undo (u) to the rescue\ \ \ 15
103 More about the dot (.) and buffer end ($)\ \ \ 16
104 Moving around in the buffer (+ and \-)\ \ \ 16
105 Changing lines (c)\ \ \ 17
106 .in -.5i
108 Session 4\ \ \ 18
109 .in +.5i
110 Making commands global (g)\ \ \ 18
111 More about searching and substituting\ \ \ 19
112 Special characters\ \ \ 19
113 Issuing \s-2UNIX\s+2 commands from the editor\ \ \ 20
114 Filenames and file manipulation\ \ \ 20
115 The file (f) command\ \ \ 20
116 Reading additional files (r)\ \ \ 21
117 Writing parts of the buffer\ \ \ 21
118 Recovering files\ \ \ 21
119 Other recovery techniques\ \ \ 21
120 Further reading and other information\ \ \ 22
121 Using \fIex\fR\ \ \ 22
122 .in -.5i
124 Index\ \ \ 23
128 \s+2Introduction\s0
130 Text editing using a terminal connected to a computer
131 allows you to create, modify, and print text
132 easily.
135 text editor
137 is a program
138 that assists you
139 as you create and modify text.
140 The text editor you will learn here is named
141 .I edit.
142 Creating text using edit is as easy as typing it
143 on an electric typewriter.
144 Modifying text involves telling the text editor 
145 what you want to add, change, or delete.
146 You can review your text
147 by typing a command
148 to print the file contents
149 as they are currently.
150 Another program (which we do not discuss in this
151 document), a text formatter,
152 rearranges your text
153 for you into ``finished form.''
155 These lessons assume no prior familiarity with computers
156 or with text editing.
157 They consist of a series of text editing sessions
158 which lead you through the fundamental steps
159 of creating and revising text.
160 After scanning each lesson and before beginning the next,
161 you should try the examples at a terminal to get a feeling
162 for the actual process of text editing.
163 If you set aside some time for experimentation,
164 you will soon become familiar with using the
165 computer to write and modify text.
166 In addition to the actual use of the text editor,
167 other features of \s-2UNIX\s0 will be very important to your work.
168 You can begin to
169 learn about these other features by
170 reading one of the other tutorials
171 that provide a general introduction to the system.
172 You will be ready to proceed with this lesson as soon as
173 you are familiar with (1) your terminal and its special keys,
174 (2) how to login,
175 (3) and the ways of correcting typing errors.
176 Let's first define some terms:
177 .sp .5
178 .IP program 12
179 A set of instructions, given to the computer,
180 describing the sequence of steps the computer performs
181 in order to accomplish a specific task.
182 The task must be specific,
183 such as balancing your checkbook
184 or editing your text.
185 A general task,
186 such as working for world peace,
187 is something we can all do,
188 but not something we can currently write programs to do.
189 .IP UNIX
190 \s-2UNIX\s0 is a special type of program,
191 called an operating system, that supervises the machinery
192 and all other programs comprising the total
193 computer system.
194 .IP edit
195 .I edit
196 is the name of the \s-2UNIX\s0 text editor you will be learning to use,
197 and is a program that aids you in writing or revising text.
198 Edit was designed for beginning users,
199 and is a simplified version of an editor named
200 .I ex.
201 .IP file
202 Each \s-2UNIX\s0 account is allotted
203 space for the permanent storage of information,
204 such as programs, data or text.
205 A file is a logical unit of data,
206 for example, an essay, a program,
207 or a chapter from a book,
208 which is stored on a computer system.
209 Once you create a file,
210 it is kept until you instruct the system to remove it.
211 You may create a file during one \s-2UNIX\s0 session,
212 end the session,
213 and return to use it at a later time.
214 Files contain anything you choose to write and store in them.
215 The sizes of files vary to suit your needs;
216 one file might hold only a single number,
217 yet another might contain
218 a very long document or program.
219 The only way to save
220 information from one session to the next is to store it in a file,
221 which you will learn in Session 1.
222 .IP filename
223 Filenames are used to distinguish one file from another,
224 serving the same purpose as the labels of manila
225 folders in a file cabinet.
226 In order to write or access information in a file,
227 you use the name of that file in a \s-2UNIX\s0 command,
228 and the system will automatically locate the file.
229 .IP disk
230 Files are stored on an input/output device called a disk,
231 which looks something like a stack of phonograph records.
232 Each surface is coated with a material similar to that
233 on magnetic recording tape,
234 and information is recorded on it.
235 .IP buffer
236 A temporary work space, made available to the user
237 for the duration of a session of text editing
238 and used for creating and modifying
239 the text file.
240 We can think of the buffer as a blackboard that is
241 erased after each class, where each session with the editor
242 is a class.
245 .ce 1
246 \s+2Session 1\s0
247 .sp 1
249 Making contact with \s-1UNIX\s0
251 To use the editor you must first make contact with the computer
252 by logging in to \s-2UNIX\s0.
253 We'll quickly review the standard \s-2UNIX\s0 login procedure
254 for the two ways you can make contact:
255 on a terminal that is directly linked to the computer,
256 or over a telephone line where the computer answers your call.
258 Directly-linked terminals
260 Turn on your terminal and press the \s-1RETURN\s0 key.
261 You are now ready to login.
263 Dial-up terminals
265 If your terminal connects with the computer over a telephone line,
266 turn on the terminal, dial the system access number,
267 and, when you hear a high-pitched tone, place the 
268 telephone handset in the acoustic coupler, if you are using one.
269 You are now ready to login.
271 Logging in
273 The message inviting you to login is:
274 .DS I 1i
275 login:
278 Type your login name, which identifies you to \s-2UNIX\s0,
279 on the same line as the login message,
280 and press \s-2RETURN\s+2.
281 If the terminal you are using
282 has both upper and lower case,
284 be sure you enter your login name in lower case;
286 otherwise \s-2UNIX\s0 assumes your terminal
287 has only upper case and will not recognize lower case
288 letters you may type.
289 \s-2UNIX\s0 types ``login:'' and you reply
290 with your login name, for example ``susan'':
291 .DS I 1i
292 login: \fBsusan\fR \fI(and press the \s-2RETURN\s0 key)\fR
294 (In the examples, input you would type appears in
295 .B "bold face"
296 to distinguish it from the responses from \s-2UNIX\s0.)
298 \s-2UNIX\s0 will next respond with a request for a password
299 as an additional precaution to prevent
300 unauthorized people from using your account.
301 The password will not appear when you type it,
302 to prevent others from seeing it.
303 The message is:
304 .DS I 1i
305 Password:    \fI(type your password and press \s-2RETURN\s+2)\fR
307 If any of the information you gave during the login
308 sequence was mistyped or incorrect,
309 \s-2UNIX\s0 will respond with
310 .DS I 1i
311 Login incorrect.
312 .if t .sp .2v
313 .if n .sp 1
314 login:
316 in which case you should start the login process anew.
317 Assuming that you have successfully
318 logged in, \s-2UNIX\s0
319 will print the message of the day and eventually will present
320 you with a % at the beginning of a fresh line.
321 The % is the \s-2UNIX\s0 prompt symbol
322 which tells you that \s-2UNIX\s0 is ready to accept a command.
323 .bd I 3
325 Asking for \fIedit\fP
327 .bd I
329 You are ready to tell \s-2UNIX\s0 that you
330 want to work with edit, the text editor.
331 Now is a convenient time to choose
332 a name for the file of text you are about to create.
333 To begin your editing session,
334 type
335 .B edit
336 followed by a space and then the filename
337 you have selected; for example, ``text''.
338 After that,
339 press the \s-2RETURN\s0 key and wait for edit's response:
340 .DS I 1i
341 % \fBedit text\fP    \fI(followed by a \s-2RETURN\s+2)\fR
342 "text" No such file or directory
345 If you typed the command correctly,
346 you will now be in communication with edit.
347 Edit has set aside a buffer for use as
348 a temporary working space during your current editing session.
349 Since ``text'' is a new file we are about to create
350 the editor was unable to find that file, which it
351 confirms by saying:
352 .DS I 1i
353 "text" No such file or directory
355 On the next line appears edit's prompt ``:'',
356 announcing that you are in \f2command mode\f1 and
357 edit expects a command from you.
358 You may now begin to create the new file.
360 The ``Command not found'' message
362 If you misspelled edit by typing, say, ``editor'',
363 this might appear:
364 .DS I 1i
365 % \fBeditor\fP
366 editor: Command not found
369 Your mistake in calling edit ``editor'' was
370 treated by \s-2UNIX\s0 as a request
371 for a program named ``editor''.
372 Since there is no program
373 named ``editor'',
374 \s-2UNIX\s0 reported that the program was ``not found''.
375 A new % indicates that \s-2UNIX\s0 is ready for another command,
376 and you may then enter the correct command.
378 A summary
380 Your exchange with \s-2UNIX\s0 as you logged in and made contact with edit
381 should look something like this:
382 .DS I 1i
383 login: \fBsusan\fP
384 Password:
385 \&... A Message of General Interest ...
386 % \fBedit text\fP
387 "text" No such file or directory
391 Entering text
393 You may now begin entering text into the buffer.
394 This is done by \fIappending\fP (or adding) text to whatever
395 is currently in the buffer.
396 Since there is nothing in the buffer at the moment,
397 you are appending text to nothing;
398 in effect, 
399 since you are adding text to nothing
400 you are creating text.
401 Most edit commands have two equivalent forms:
402 a word that suggests what the command does,
403 and a shorter abbreviation of that word.
404 Many beginners find the full command names
405 easier to remember at first,
406 but once you are familiar with editing you may
407 prefer to type the shorter abbreviations.
408 The command to input text is ``append''.
409 (It may be abbreviated ``a''.)
410 Type
411 .B append
412 and press the \s-2RETURN\s0 key.
413 .DS I 1i
414 % \fBedit text
415 \fR:\|\fBappend
419 .bd I 3
420 Messages from
421 .I edit
423 .bd I
425 If you make a mistake in entering a command and
426 type something that edit does not recognize,
427 edit will respond with a message
428 intended to help you diagnose your error.
429 For example, if you misspell the command to input text by typing,
430 perhaps, ``add'' instead of ``append'' or ``a'',
431 you will receive this message:
432 .DS I 1i
433 :\|\fBadd\fR
434 add: Not an editor command
437 When you receive a diagnostic message,
438 check what you typed in order to determine what
439 part of your command confused edit.
440 The message above means that edit
441 was unable to recognize your mistyped command
442 and, therefore, did not execute it.
443 Instead, a new ``:''
444 appeared to let you know that
445 edit is again ready to execute a command.
447 Text input mode
449 By giving the command ``append'' (or using the abbreviation ``a''),
450 you entered
452 text input mode,
454 also known as
456 append mode.
458 When you enter text input mode,
459 edit stops sending you a prompt.
460 You will not receive any prompts
461 or error messages
462 while in text input mode.
463 You can enter
464 pretty much anything you want on the lines.
465 The lines are transmitted one by one to the buffer
466 and held there during the editing session.
467 You may append as much text as you want, and
469 when you wish to stop entering text lines you should
470 type a period as the only character on the line
471 and press the \s-2RETURN\s0 key.
473 When you type the period and press \s-2RETURN\s0,
474 you signal that you want to stop appending text,
475 and edit responds by allowing
476 you to exit text input mode and reenter command mode.
477 Edit will again
478 prompt you for a command by printing ``:''.
480 Leaving append mode does not destroy the text in
481 the buffer.
482 You have to leave append
483 mode to do any of the other kinds of editing,
484 such as changing, adding, or printing text.
485 If you type a period as the first character and
486 type any other character on the same line,
487 edit will believe you want to remain in append mode
488 and will not let you out.
489 As this can be very frustrating, 
490 be sure to type
491 .B only
492 the period and the \s-2RETURN\s0 key.
494 This is a good place to learn an important
495 lesson about computers and text:  a blank space is
496 a character as far as a computer is concerned.  
497 If you so much as type a period followed by a blank
498 (that is, type a period and then the space bar on the keyboard),
499 you will remain in append mode with the last line of text
500 being:
501 .DS I 1i
503 .ps +2
505 .ps -2
508 Let's say that you enter the lines 
509 (try to type
510 .B exactly
511 what you see, including ``thiss''):
512 .DS I 1i
514 This is some sample text.
515 And thiss is some more text.
516 Text editing is strange, but nice.
520 The last line is the period followed by a \s-2RETURN\s0
521 that gets you out of append mode.  
523 Making corrections
525 If you have read a general introduction to \s-2UNIX\s0,
526 you will recall that it is possible to erase individual
527 letters that you have typed.
528 This is done by typing the designated erase character
529 as many times as there are characters
530 you want to erase.
532 The usual erase character varies from place to place and 
533 user to user.  Often it
534 is the backspace (control-H),
535 so you can correct typing errors
536 in the line you are typing
537 by holding down the \s-1CTRL\s+1 key
538 and typing the ``H'' key.  (Sometimes it is the DEL key.)
539 If you type the erase character
540 you will notice
541 that the terminal backspaces in the line you are on.
542 You can backspace over your error,
543 and then type what you want to be the rest of the line.
545 If you make a bad start
546 in a line
547 and would like to begin again,
548 you can either backspace to the beginning of the line
549 or you can use the at-sign ``@'' to erase everything on the line:
550 .DS I 1i
552 Text edtiing is strange, but@
553 Text editing is strange, but nice.
556 .bd S
558 When you type the at-sign (@), you erase
559 the entire line typed so far
560 and are given a fresh line to type on.
561 You may immediately begin to retype the line.
562 This, unfortunately, does not work after you type the
563 line and press \s-2RETURN\s+2.  
564 To make corrections in lines that have been completed,
565 it is necessary to use the editing commands
566 covered in the next sessions.
568 Writing text to disk
570 You are now ready to edit the text.  One common operation
571 is to write the text to disk as a file for safekeeping
572 after the session is over.
573 This is the only way to save information from one session to the next,
574 since the editor's buffer is temporary and will last only until the
575 end of the editing session.
576 Learning how to write a file to disk is second in
577 importance only to entering the text.
578 To write the contents of the buffer to a disk
579 file, use the command ``write''
580 (or its abbreviation ``w''):
581 .DS I 1i
582 :\|\fBwrite
585 Edit will copy the contents of the buffer to a disk file.
586 If the file does not yet exist,
587 a new file will be created automatically
588 and the presence of a ``[New file]'' will be noted.
589 The newly-created file will be given the name specified when
590 you entered the editor, in this case ``text''.
591 To confirm that the disk file has been successfully written,
592 edit will repeat the filename and give
593 the number of lines and the total
594 number of characters in the file.
595 The buffer remains unchanged by the ``write'' command.
596 All of the lines that were written to disk will still be
597 in the buffer,
598 should you want to modify or add to them.
600 Edit must have a name for the file to be written.
601 If you forgot to indicate the name of the file
602 when you began to edit,
603 edit will print in response to your write command:
604 .DS I 1i
605 No current filename
607 If this happens, you can specify the filename in a new write command:
608 .DS I 1i
609 :\|\fBwrite text
612 After the ``write'' (or ``w''), type a space and then the name of the file.
614 Signing off
616 We have done enough for this first lesson on using the
617 \s-2UNIX\s0 text editor, and are ready to quit the session with edit.
618 To do this we type ``quit'' (or ``q'') and press \s-2RETURN\s+2:
619 .DS I 1i
620 :\|\fBwrite
622 "text" [New file]  3 lines, 90 characters
623 :\|\fBquit\fR
626 The % is from \s-2UNIX\s0 to tell you that your session with edit is
627 over and you may command \s-2UNIX\s0 further.
628 Since we want
629 to end the entire session at the terminal, we also need to
630 exit from \s-2UNIX\s0.
631 In response to the \s-2UNIX\s0 prompt of ``\|%\|''
632 type the command
633 .DS I 1i
634 %\|\fBlogout\fR
636 This will end your session with \s-2UNIX\s0, and will ready the
637 terminal for the next user.
638 It is always important to type \fBlogout\fR at the end of a session
639 to make absolutely sure no one
640 could accidentally stumble into your abandoned 
641 session and thus gain access to your files,
642 tempting even the most honest of souls.
643 .sp 1
645 This is the end of the first session on \s-2UNIX\s0 text editing.
648 Session 2
651 Login with \s-2UNIX\s0 as in the first session:
652 .DS I 1i
653 login: \fBsusan\fP  \fI(carriage return)\fR
654 Password:       \fI(give password and carriage return)\fR
655 .if t .sp .2v
656 .if n .sp 1
657 \&... A Message of General Interest ...
660 When you indicate you want to edit,
661 you can specify the name of the file you worked on last time.
662 This will
663 start edit working, and it will fetch the contents of the
664 file into the buffer, so that you can resume editing the same file.
665 When edit has copied the file into the buffer, it
666 will repeat its name and tell
667 you the number of lines and characters it contains.
668 Thus,
669 .DS I 1i
671 % edit text
673 "text" 3 lines, 90 characters
676 means you asked edit to fetch
677 the file named ``text'' for editing,
678 causing it to copy the
679 90 characters of text into the buffer.
680 Edit awaits
681 your further instructions,
682 and indicates this by its prompt character, the colon (:).
683 In this session, we will append more text to our file,
684 print the contents of the buffer, and learn to change the text of a line.
686 Adding more text to the file
688 If you want to add more to the end of your
689 text you may do so by using the append command to enter text input mode.
690 When ``append'' is the first command
691 of your editing session,
692 the lines you enter
693 are placed at the end of the buffer.
694 Here we'll use the abbreviation for the append command, ``a'':
695 .DS I 1i
696 :\|\fBa
697 This is text added in Session 2.
698 It doesn't mean much here, but
699 it does illustrate the editor.
700 \|\fB\s+2\&.\s-2
703 You may recall that once you enter append mode
704 using the ``a'' (or ``append'') command,
705 you need to type a line containing only a period (.)
706 to exit append mode.
708 Interrupt
710 Should you press the \s-2RUB\s+2 key (sometimes labelled \s-2DELETE\s+2)
711 while working with edit,
712 it will send this message to you:
713 .DS I 1i
714 Interrupt
717 Any command that edit might be executing
718 is terminated by rub or delete,
719 causing edit to prompt you for a new command.
720 If you are appending text at the time,
721 you will exit from append mode
722 and be expected to give another command.
723 The line of text you were typing
724 when the append command was interrupted
725 will not be entered into the buffer.
727 Making corrections
729 If while typing the line you hit an incorrect key,
730 recall that
731 you may delete the incorrect character
732 or cancel the entire line of input by erasing in the usual way.
733 Refer either
734 to the last few pages of Session 1
735 if you need to review
736 the procedures for making a correction.
737 The most important idea to remember is that
738 erasing a character or cancelling a line must be done
739 before you press the \s-2RETURN\s+2 key.
741 Listing what's in the buffer (p)
743 Having appended text to what you wrote in Session 1,
744 you might want to see all the lines in the buffer.
745 To print the contents of the buffer, type the command:
746 .DS I 1i
747 :\|\fB1,$p
750 The ``1''\(dg
752 \(dgThe numeral ``one'' is the top left-most key,
753 and should not be confused with the letter ``el''.
755 stands for line 1 of the buffer,
756 the ``$'' is a special symbol designating the last line
757 of the buffer,
758 and ``p'' (or \fBprint\fR) is the command to print from line 1
759 to the end of the buffer.
760 The command ``1,$p'' gives you:
761 .DS I 1i
762 This is some sample text.
763 And thiss is some more text.
764 Text editing is strange, but nice.
765 This is text added in Session 2.
766 It doesn't mean much here, but
767 it does illustrate the editor.
769 Occasionally, you may accidentally
770 type a character that can't be printed,
771 which can be done by striking a key
772 while the \s-2CTRL\s0 key is pressed.
773 In printing lines, edit uses a special notation to
774 show the existence of non-printing characters.
775 Suppose you had introduced the non-printing character ``control-A''
776 into the word ``illustrate''
777 by accidently pressing the \s-2CTRL\s0 key while
778 typing ``a''.
779 This can happen on many terminals
780 because the \s-2CTRL\s+2 key and the ``A'' key
781 are beside each other.
782 If your finger presses between the two keys,
783 control-A results.
784 When asked to print the contents of the buffer,
785 edit would display
786 .DS I 1i
787 it does illustr^Ate the editor.
789 To represent the control-A, edit shows ``^A''.
790 The sequence ``^'' followed by a capital
791 letter stands for the one character
792 entered by holding down the \s-2CTRL\s0 key and typing the letter
793 which appears after the ``^''.
794 We'll soon discuss the commands that can be used
795 to correct this typing error.
797 In looking over the text we see that
798 ``this'' is typed as ``thiss'' in the second line,
799 a deliberate error so we can learn to make corrections.
800 Let's correct the spelling.
802 Finding things in the buffer
804 In order to change something in the buffer we first need to
805 find it.
806 We can find ``thiss'' in the text we have
807 entered by looking at a listing
808 of the lines.
809 Physically speaking, we search the lines
810 of text looking for ``thiss'' and stop searching when
811 we have found it.
812 The way to tell edit to search for something
813 is to type it inside slash marks:
814 .DS I 1i
815 :\|\fB/thiss/
818 By typing
819 .B /thiss/
820 and pressing \s-1RETURN\s0,
821 you instruct edit to search for ``thiss''.
822 If you ask edit to look for a pattern of characters
823 which it cannot find in the buffer,
824 it will respond ``Pattern not found''.
825 When edit finds
826 the characters ``thiss'', it will print the line of text
827 for your inspection:
828 .DS I 1i
829 And thiss is some more text.
831 Edit is now positioned in the buffer at the
832 line it just printed,
833 ready to make a change in the line.
836 The current line
838 Edit keeps track of the line in the buffer where it is located
839 at all times during an editing session.
840 In general, the line that has been most recently
841 printed, entered, or changed
842 is the current location in the buffer.
843 The editor is prepared to make changes
844 at the current location in the buffer,
845 unless you direct it to another location.
847 In particular,
848 when you bring a file into the buffer,
849 you will be located at the last line in the file,
850 where the editor left off copying the lines
851 from the file to the buffer.
852 If your first editing command is ``append'',
853 the lines you enter are added
854 to the end of the file,
855 after the current line \(em
856 the last line in the file.
858 You can refer to your current location in the buffer by the
859 symbol
860 period (.) usually known by the name ``dot''.
861 If you type ``.'' and carriage
862 return you will be instructing edit to print the current line:
863 .DS I 1i
864 :\|\fB\s+2\&.\s-2
866 And thiss is some more text.
869 If you want to know the number of the current line,
870 you can type
871 .B \&.=
872 and press \s-2RETURN\s+2,
873 and edit will respond with the line number:
874 .DS I 1i
875 :\|\fB\s+2.\s-2=
879 If you type the number of any line and press \s-2RETURN\s+2,
880 edit will position you at that line and
881 print its contents:
882 .DS I 1i
883 :\|\fB2
885 And thiss is some more text.
887 You should experiment with these commands
888 to gain experience in using them to make changes.
890 Numbering lines (nu)
894 number (nu)
896 command is similar to print,
897 giving both the number and the text of each printed line.
898 To see the number and the text of the current line type
899 .DS I 1i
900 :\|\fBnu
902 \0\0\0\0\02\0\0And thiss is some more text.
904 Note that the shortest abbreviation for the number command is
905 ``nu'' (and not ``n'', which is used for a different command).
906 You may specify a range of lines
907 to be listed by the number command in the same way that lines
908 are specified for print.
909 For example, \f31,$nu\f1 lists all lines in the buffer with their
910 corresponding line numbers.
912 Substitute command (s)
914 Now that you have found the misspelled word, 
915 you can change it from ``thiss'' to ``this''.
916 As far as edit is concerned,
917 changing things is a matter of
918 substituting one thing for another.
920 .I a
921 stood for
922 .I append,
924 .I s
925 stands for
926 .I substitute.
927 We will use the abbreviation ``s'' to reduce the chance
928 of mistyping the substitute command.
929 This command will instruct edit to make the change:
930 .DS I 1i
931 \f32s/thiss/this/\f1
933 We first indicate the line to be changed, line 2,
934 and then
935 type an ``s'' to indicate we want
936 edit to make a substitution.
937 Inside the first set of slashes
938 are the characters that we want to change,
939 followed by the characters to replace them,
940 and then a closing slash mark.
941 To summarize:
942 .DS I 1i
943 2s/ \fIwhat is to be changed\fR / \fIwhat to change it to \fR/
945 If edit finds an exact match of the characters to be
946 changed it will make the change
947 .B only
948 in the first occurrence of the characters.
949 If it does not find the characters
950 to be changed, it will respond:
951 .DS I 1i
952 Substitute pattern match failed
954 indicating that your instructions could not be carried out.
955 When edit does find the characters that you want to change,
956 it will make the substitution and automatically print
957 the changed line, so that you can check that the correct substitution
958 was made.
959 In the example,
960 .DS I 1i
961 :\|\fB2s/thiss/this/
963 And this is some more text.
965 line 2 (and line 2 only) will be searched for the characters
966 ``thiss'', and when the first exact match is found, ``thiss''
967 will be changed to ``this''.
968 Strictly speaking, it was not necessary above to
969 specify  the number of the line to be changed.
971 .DS I 1i
972 :\|\fBs/thiss/this/
975 edit will assume that we mean to change
976 the line where we are currently located (``.'').
977 In this case,
978 the command without a line number would have produced the same result
979 because we were already located
980 at the line we wished to change.
982 For another illustration of the substitute command,
983 let us choose the line:
984 .DS I 1i
985 Text editing is strange, but nice.
987 You can make this line a bit more positive
988 by taking out the characters ``strange, but\ '' so the line 
989 reads:
990 .DS I 1i
991 Text editing is nice.
993 A command that will first position edit at the desired line
994 and then make the substitution is:
995 .DS I 1i
996 :\|\fB/strange/s/strange, but //
1000 What we have done here is combine our search with
1001 our substitution.
1002 Such combinations are perfectly legal,
1003 and speed up editing quite a bit
1004 once you get used to them.
1005 That is, you do not necessarily have to use
1006 line numbers to identify a line to edit.
1007 Instead, you may identify the line you want to change
1008 by asking edit to search for a specified pattern of letters
1009 that occurs in that line.
1010 The parts of the above command are:
1012 .in +1i
1013 .nr 35 \n(.u
1015 .ds #d .d
1016 .if \(ts\n(.z\(ts\(ts .ds #d nl
1017 .nr 80 0
1018 .nr 38 \w\x02\f3/strange/\fP\x02
1019 .if \n(80<\n(38 .nr 80 \n(38
1020 .nr 38 \w\x02\f3s\fP\x02
1021 .if \n(80<\n(38 .nr 80 \n(38
1022 .nr 38 \w\x02\f3/strange, but //\fP\x02
1023 .if \n(80<\n(38 .nr 80 \n(38
1024 .nr 81 0
1025 .nr 38 \w\x02tells edit to find the characters ``strange'' in the text\x02
1026 .if \n(81<\n(38 .nr 81 \n(38
1027 .nr 38 \w\x02tells edit to make a substitution\x02
1028 .if \n(81<\n(38 .nr 81 \n(38
1029 .nr 38 \w\x02substitutes nothing at all for the characters ``strange, but ''\x02
1030 .if \n(81<\n(38 .nr 81 \n(38
1031 .nr 38 1n
1032 .nr 79 0
1033 .nr 40 \n(79+(0*\n(38)
1034 .nr 80 +\n(40
1035 .nr 41 \n(80+(3*\n(38)
1036 .nr 81 +\n(41
1037 .nr TW \n(81
1038 .if t .if (\n(TW+\n(.o)>7.75i .tm Table at line 307 file ed2.tbl is too wide - \n(TW units
1039 .fc \x02 \x03
1040 .nr #T 0
1042 .de T#
1043 .ds #d .d
1044 .if \(ts\n(.z\(ts\(ts .ds #d nl
1045 .mk ##
1046 .nr ## -1v
1049 .ta \n(80u \n(81u 
1050 \&\h'|\n(40u'\x02\f3/strange/\fP\x03\x02\h'|\n(41u'\x02tells edit to find the characters ``strange'' in the text\x03\x02
1051 .ta \n(80u \n(81u 
1052 \&\h'|\n(40u'\x02\f3s\fP\x03\x02\h'|\n(41u'\x02tells edit to make a substitution\x03\x02
1053 .ta \n(80u \n(81u 
1054 \&\h'|\n(40u'\x02\f3/strange, but //\fP\x03\x02\h'|\n(41u'\x02substitutes nothing at all for the characters ``strange, but ''\x03\x02
1056 .nr T. 1
1057 .T# 1
1058 .if \n(35>0 .fi
1059 .in -1i
1062 You should note the space after ``but'' in ``/strange, but /''. 
1063 If you do not indicate that the space is to be taken out,
1064 your line will read:
1065 .DS I 1i
1066 .if t Text editing is   nice.
1067 .if n Text editing is  nice.
1069 which looks a little funny   
1070 because of the extra space between ``is'' and ``nice''.
1071 Again, we realize from this that a blank space
1072 is a real character to a computer, and in editing text
1073 we need to be aware of spaces
1074 within a line just as we would be aware of an ``a'' or 
1075 a ``4''.
1077 Another way to list what's in the buffer (z)
1079 Although the print command is useful for looking at specific lines
1080 in the buffer,
1081 other commands may be more convenient for
1082 viewing large sections of text.
1083 You can ask to see a screen full of text at a time
1084 by using the command
1085 .B z.
1086 If you type
1087 .DS I 1i
1088 :\|\fB1z
1091 edit will start with line 1 and continue printing lines,
1092 stopping either when the screen of
1093 your terminal is full
1094 or when the last line in the buffer has been printed.
1095 If you want to read the next segment of text, type the command
1096 .DS I 1i
1097 :\|\fBz
1099 If no starting line number is given for the z command,
1100 printing will start at the ``current'' line, in this case the
1101 last line printed.
1102 Viewing lines in the buffer one screen full at a time
1103 is known as \fIpaging\fR.
1104 Paging can also be used to print
1105 a section of text on a hard-copy terminal.
1107 Saving the modified text
1109 This seems to be a good place to pause in our work,
1110 and so we should end the second session.
1111 If you (in haste) type ``q'' to quit the session
1112 your dialogue with edit will be:
1113 .DS I 1i
1114 :\|\fBq
1116 No write since last change (:quit! overrides)
1119 This is edit's warning that you have not written
1120 the modified contents of the buffer to disk.
1121 You run the risk of losing the work you did
1122 during the editing session since you typed the latest write
1123 command.
1124 Because in this lesson we have not written
1125 to disk at all, everything we have done
1126 would have been lost
1127 if edit had obeyed the \fBq\fR command.
1128 If you did not want to save the work done during
1129 this editing session, you would have to type ``q!''
1130 or (``quit!'')
1131 to confirm that you indeed wanted to end the session
1132 immediately,
1133 leaving the file as it was
1134 after the most recent ``write'' command.
1135 However,
1136 since you want to save what
1137 you have edited, you need to type:
1138 .DS I 1i
1139 :\|\fBw
1141 "text" 6 lines, 171 characters
1143 and then follow with the commands to quit and logout:
1144 .DS I 1i
1145 :\|\fBq
1146 % \fBlogout\fR
1148 and hang up the phone or turn off the terminal when
1149 \s-2UNIX\s0 asks for a name.
1150 Terminals connected to the port selector
1151 will stop after the logout command,
1152 and pressing keys on the keyboard will do nothing.
1153 .sp 1
1155 This is the end of the second session on \s-2UNIX\s0 text editing.
1158 Session 3
1160 Bringing text into the buffer (e)
1162 Login to \s-2UNIX\s0 and make contact with edit.  
1163 You should try to login without
1164 looking at the notes, but if you must
1165 then by all means do.
1167 Did you remember to give the name of the file
1168 you wanted to edit?
1169 That is, did you type
1170 .DS I 1i
1171 % \fBedit text\fR
1173 or simply
1174 .DS I 1i
1175 % \fBedit\fR
1177 Both ways get you in contact with edit, but the first way
1178 will bring a copy of the file named ``text'' into
1179 the buffer.  
1180 If you did forget to tell edit the name of your file,
1181 you can get it into the buffer by
1182 typing:
1183 .DS I 1i
1184 :\|\fBe text
1186 "text" 6 lines, 171 characters
1188 The command
1189 .B edit,
1190 which may be abbreviated \fBe\fR,
1191 tells edit that you want
1192 to erase anything that might already be in 
1193 the buffer and bring a copy of the file ``text'' into the buffer
1194 for editing.
1195 You may also use the edit (e) command to change files in
1196 the middle of an editing session,
1197 or to give edit the name of a new file that you want to create.
1198 Because the edit command clears the buffer,
1199 you will receive a warning if you try to edit a new file without
1200 having saved a copy of the old file.
1201 This gives you a chance to write the contents of the buffer to disk
1202 before editing the next file.
1204 Moving text in the buffer (m)
1206 Edit allows you to move lines of text
1207 from one location in the buffer to another
1208 by means of the
1209 .B move
1210 (\fBm\fR) command.
1211 The first two examples are for illustration only,
1212 though after you have read this Session
1213 you are welcome to return to them for practice.
1214 The command
1215 .DS I 1i
1216 :\|\fB2,4m$
1219 directs edit to move lines 2, 3, and 4
1220 to the end of the buffer ($).  
1221 The format for the move command is that you specify
1222 the first line to be moved, the last line to be moved,
1223 the move command ``m'', and the line after which
1224 the moved text is to be placed.
1226 .DS I 1i
1227 :\|\fB1,3m6
1230 would instruct edit to move lines 1 through 3 (inclusive) 
1231 to a location after line 6 in the buffer.
1232 To move only one line, say, line 4,
1233 to a location in the buffer after line 5, 
1234 the command would be ``4m5''.
1236 Let's move some text using the command:
1237 .DS I 1i
1238 :\|\fB5,$m1
1240 2 lines moved
1241 it does illustrate the editor.
1243 After executing a command that moves more than one line of the buffer,
1244 edit tells how many lines were affected by the move
1245 and prints the last moved line for your inspection.
1246 If you want to see more than just the last line,
1247 you can then
1248 use the print (p), z, or number (nu) command to view more text.
1249 The buffer should now contain:
1250 .DS I 1i
1251 This is some sample text.
1252 It doesn't mean much here, but
1253 it does illustrate the editor.
1254 And this is some more text.
1255 Text editing is nice.
1256 This is text added in Session 2.
1258 You can restore the original order by typing:
1259 .DS I 1i
1260 :\|\fB4,$m1
1263 or, combining context searching and the move command:
1264 .DS I 1i
1265 :\|\fB/And this is some/,/This is text/m/This is some sample/
1268 (Do not type both examples here!)
1269 The problem with combining context searching
1270 with the move command 
1271 is that your chance of making a typing error
1272 in such a long command is greater than
1273 if you type line numbers.
1275 Copying lines (copy)
1278 .B copy
1279 command
1280 is used to make a second copy of specified lines,
1281 leaving the original lines where they were.
1282 Copy
1283 has the same format as the move command, for example:
1284 .DS I 1i
1285 :\|\fB2,5copy $
1288 makes a copy of lines 2 through 5,
1289 placing the added lines after the buffer's end ($).
1290 Experiment with the copy command
1291 so that you can become familiar with how it works.
1292 Note that the shortest abbreviation for copy is
1293 \f3co\f1 (and
1294 not the letter ``c'', which has another meaning).
1296 Deleting lines (d)
1298 Suppose you want to delete 
1299 the line
1300 .DS I 1i
1301 This is text added in Session 2.
1303 from the buffer.
1304 If you know the number of the line to be deleted,
1305 you can type
1306 that number followed by
1307 \fBdelete\fR or \fBd\fR.
1308 This example deletes line 4,
1309 which is ``This is text added in Session 2.''
1310 if you typed the commands
1311 suggested so far.
1312 .DS I 1i
1313 :\|\fB4d
1315 It doesn't mean much here, but
1317 Here ``4'' is the number of the line to be deleted,
1318 and ``delete'' or ``d'' is the command to delete the line.
1319 After executing the delete command,
1320 edit prints the line that has become the current line (``.'').
1322 If you do not happen to know the line number
1323 you can search for the line and then delete it using this
1324 sequence of commands:
1325 .DS I 1i
1326 :\|\fB/added in Session 2./
1328 This is text added in Session 2.
1329 :\|\fBd
1331 It doesn't mean much here, but
1333 The ``/added in Session 2./''
1334 asks edit to locate and print
1335 the line containing the indicated text,
1336 starting its search at the current line
1337 and moving line by line
1338 until it finds the text.
1339 Once you are sure that you have correctly specified the line
1340 you want to delete,
1341 you can enter the delete (d) command.
1342 In this case it is not necessary to
1343 specify a line number before the ``d''.
1344 If no line number is given,
1345 edit deletes the current line (``.''),
1346 that is, the line found by our search.
1347 After the deletion, your buffer should contain:
1348 .DS I 1i
1349 This is some sample text.
1350 And this is some more text.
1351 Text editing is nice.
1352 It doesn't mean much here, but
1353 it does illustrate the editor.
1354 And this is some more text.
1355 Text editing is nice.
1356 This is text added in Session 2.
1357 It doesn't mean much here, but
1359 To delete both lines 2 and 3:
1360 .DS I 1i
1361 And this is some more text.
1362 Text editing is nice.
1364 you type
1365 .DS I 1i
1366 :\|\f32,3d\f1
1367 2 lines deleted
1369 which specifies the range of lines from 2 to 3,
1370 and the operation on those lines \(em ``d'' for delete.
1371 If you delete more than one line
1372 you will receive a message
1373 telling you the number of lines deleted,
1374 as indicated in the example above.
1376 The previous example assumes that you know the line numbers for
1377 the lines to be deleted.
1378 If you do not you might combine the search command
1379 with the delete command:
1380 .DS I 1i
1381 :\|\fB/And this is some/,/Text editing is nice./d
1385 A word or two of caution
1387 In using the search function to locate lines to
1388 be deleted you should be
1390 absolutely sure
1392 the characters you give as the basis for the search
1393 will take edit to the line you want deleted.
1394 Edit will search for the first
1395 occurrence of the characters starting from where
1396 you last edited \-
1397 that is, from the line you see printed if you type dot (.).
1399 A search based on too few
1400 characters may result in the wrong lines being deleted,
1401 which edit will do as easily as if you had meant it.
1402 For this reason, it is usually safer
1403 to specify the search and then delete in two separate steps,
1404 at least until you become familiar enough with using the editor
1405 that you understand how best to specify searches.
1406 For a beginner it is not a bad idea to double-check
1407 each command before pressing \s-2RETURN\s+2 to send the command on its way.
1409 Undo (u) to the rescue
1413 undo (u)
1415 command has the ability to
1416 reverse the effects of the last command that changed the buffer.
1417 To undo the previous command, type
1418 ``u'' or ``undo''.
1419 Undo can rescue
1420 the contents of the buffer from many an unfortunate mistake.
1421 However, its powers are not unlimited,
1422 so it is still wise to be reasonably
1423 careful about the commands you give.
1425 It is possible to undo only commands which
1426 have the power to change the buffer \(em for example,
1427 delete, append, move, copy, substitute, and even undo itself.
1428 The commands write (w) and edit (e), which interact with disk files,
1429 cannot be undone, nor can commands that do not change
1430 the buffer, such as print.
1431 Most importantly,
1433 .B only
1434 command that can be reversed by undo
1435 is the
1436 last ``undo-able'' command you typed.
1437 You can use control-H and @ to change
1438 commands while you are typing them,
1439 and undo to reverse the effect of the commands
1440 after you have typed them and pressed \s-2RETURN\s+2.
1442 To illustrate,
1443 let's issue an undo command.
1444 Recall that the last buffer-changing command we gave deleted
1445 the lines formerly numbered 2 and 3.
1446 Typing undo at this moment will reverse the effects
1447 of the deletion, causing those two lines to be
1448 replaced in the buffer.
1449 .DS I 1i
1450 :\|\fBu
1452 2 more lines in file after undo
1453 And this is some more text.
1455 Here again, edit informs you if the command affects more
1456 than one line,
1457 and prints
1458 the text of the line which is now ``dot'' (the current line).
1460 More about the dot (.) and buffer end ($)
1462 The function assumed by the symbol dot depends on its context.
1463 It can be used:
1465 1.  to exit from append mode; we type dot (and only a dot) on
1466 a line and press \s-2RETURN\s+2;
1468 2.  to refer to the line we are at in the buffer.
1470 Dot can also be combined with the equal sign to get
1471 the number of the line currently being edited:
1472 .DS I 1i
1473 :\|\fB\&.=
1476 If we type ``\fB.\fR='' we are asking for the number of the line,
1477 and if we type ``\fB.\fR'' we are asking for the text of the line.
1479 In this editing session and the last, we used the dollar
1480 sign to indicate the end of the buffer
1481 in commands such as print, copy, and move.
1482 The dollar sign as a command asks edit to print the last
1483 line in the buffer.
1484 If the dollar sign is combined with the equal sign (\f3$=\f1)
1485 edit will print the line number corresponding to the
1486 last line in the buffer.
1488 ``\fB.\fR'' and ``$'', then, represent line numbers.
1489 Whenever appropriate, these symbols can be used in
1490 place of line numbers in commands.
1491 For example
1492 .DS I 1i
1493 :\|\fB\s+2.\s-2,$d
1496 instructs edit to delete all lines from the current line (\fB.\fR)
1497 to the end of the buffer.
1499 Moving around in the buffer  (+ and \-)
1501 When you are editing
1502 you often want
1503 to go back and re-read a previous line.
1504 You could specify a context search for a line you want to
1505 read if you remember some of its text,
1506 but if you simply want to see what was written a few, say 3, lines
1507 ago, you can type
1508 .DS I 1i
1509 \-3p
1511 This tells edit to move back to a position 3 lines
1512 before the current line (.)
1513 and print that line.
1514 You can move forward in the buffer similarly:
1515 .DS I 1i
1518 instructs edit to print the line that is 2
1519 ahead of your current position.
1521 You may use ``+'' and ``\-'' in any command where edit
1522 accepts line numbers.
1523 Line numbers specified with ``+'' or ``\-''
1524 can be combined to print a range of lines.
1525 The command
1526 .DS I 1i
1527 :\|\fB\-1,+2copy$
1530 makes a copy of 4 lines:  the current line, the line before it,
1531 and the two after it.
1532 The copied lines will be placed after the last line
1533 in the buffer ($),
1534 and the original lines referred to by ``\-1'' and ``+2''
1535 remain where they are.
1537 Try typing only ``\-''; you will move back one line just as
1538 if you had typed ``\-1p''.
1539 Typing the command ``+'' works similarly.
1540 You might also try typing a few plus or minus signs in a row
1541 (such as ``+++'') to see edit's response.
1542 Typing \s-2RETURN\s+2 alone on a line is the equivalent
1543 of typing ``+1p''; it will move you one line ahead in the buffer
1544 and print that line.
1546 If you are at the last line of the buffer and try
1547 to move further ahead, perhaps by typing a ``+'' or
1548 a carriage return alone on the line,
1549 edit will remind you that you are at the end of the buffer:
1552 .ti 1i
1553 At end-of-file
1556 .ti 1i
1557 Not that many lines in buffer
1560 Similarly, if you try to move to a position before the first line,
1561 edit will print one of these messages:
1564 .ti 1i
1565 Nonzero address required on this command
1568 .ti 1i
1569 Negative address \- first buffer line is 1
1572 The number associated with a buffer line is the line's ``address'',
1573 in that it can be used to locate the line.
1575 Changing lines (c)
1577 You can also delete certain lines and
1578 insert new text in their place.
1579 This can be accomplished easily with the
1580 .B "change (c)"
1581 command.
1582 The change command instructs edit to delete specified lines
1583 and then switch to text input mode to
1584 accept the text that will replace them.
1585 Let's say you want to change the first two lines in the buffer:
1586 .DS I 1i
1587 This is some sample text.
1588 And this is some more text.
1590 to read
1591 .DS I 1i
1592 This text was created with the \s-2UNIX\s0 text editor.
1594 To do so, you type:
1595 .DS I 1i
1596 :\|\fB1,2c
1598 2 lines changed
1600 This text was created with the \s-2UNIX\s0 text editor.
1601 \s+2\&.\s-2
1605 In the command
1606 .B 1,2c
1607 we specify that we want to change
1608 the range of lines beginning with 1 and ending with 2
1609 by giving line numbers as with the print command.
1610 These lines will be deleted.
1611 After you type \s-2RETURN\s+2 to end the change command,
1612 edit notifies you if more than one line will be changed
1613 and places you in text input mode.
1614 Any text typed on the following lines will be inserted into
1615 the position where lines were deleted by the change command.
1617 You will remain in text input mode until you exit in the usual way,
1618 by typing a period alone on a line.
1620 Note that the number of lines added to the buffer need not be
1621 the same as the number of lines deleted.
1622 .sp 1
1624 This is the end of the third session on text editing with \s-2UNIX\s0.
1627 .ce 1
1628 \s+2Session 4\s0
1631 This lesson covers several topics, starting with
1632 commands that apply throughout the buffer,
1633 characters with special meanings,
1634 and how to issue \s-2UNIX\s0 commands while in the editor.
1635 The next topics deal with files:
1636 more on reading and writing,
1637 and methods of recovering files lost in a crash.
1638 The final section suggests sources of further information.
1640 Making commands global (g)
1642 One disadvantage to the commands we have used for
1643 searching or substituting is that if you
1644 have a number of instances of a word to change 
1645 it appears that you have to type the command 
1646 repeatedly, once for
1647 each time the change needs to be made.
1648 Edit, however, provides a way to make commands
1649 apply to the entire contents of the buffer \-
1652 global (g)
1654 command.
1656 To print all lines
1657 containing a certain sequence of characters
1658 (say, ``text'')
1659 the command is:
1660 .DS I 1i
1661 :\|\fBg/text/p
1664 The ``g'' instructs edit to
1665 make a global search for all lines
1666 in the buffer containing the characters  ``text''.
1667 The ``p'' prints the lines found.
1669 To issue a global command, start by typing a ``g'' and then a search
1670 pattern identifying
1671 the lines to be affected.
1672 Then, on the same line, type the command to be
1673 executed for the identified lines.
1674 Global substitutions are frequently useful.
1675 For example,
1676 to change all instances of the word ``text'' to the word ``material''
1677 the command would be a combination of the global search and the
1678 substitute command:
1679 .DS I 1i
1680 :\|\fBg/text/s/text/material/g
1683 Note the ``g'' at the end of the global command,
1684 which instructs edit to change
1685 each and every instance of ``text'' to ``material''.
1686 If you do not type the ``g'' at the end of the command
1687 only the
1688 .I first
1689 instance of ``text'' \fIin each line\fR will be changed
1690 (the normal result of the substitute command).
1691 The ``g'' at the end of the command is independent of the ``g''
1692 at the beginning.
1693 You may give a command such as:
1694 .DS I 1i
1695 :\|\fB5s/text/material/g
1698 to change every instance of ``text'' in line 5 alone.
1699 Further, neither command will change ``text'' to ``material''
1700 if ``Text'' begins with a capital rather than a lower-case
1701 .I t.
1703 Edit does not automatically print the lines modified by a
1704 global command.
1705 If you want the lines to be printed, type a ``p''
1706 at the end of the global command:
1707 .DS I 1i
1708 :\|\fBg/text/s/text/material/gp
1711 You should be careful
1712 about using the global command in combination with any other \-
1713 in essence, be sure of what you are telling edit to do
1714 to the entire buffer.
1715 For example,
1716 .DS I 1i
1717 :\|\fBg/ /d
1719 72 less lines in file after global
1721 will delete every line containing a blank anywhere in it.
1722 This could adversely affect
1723 your document, since most lines have spaces between words
1724 and thus would be deleted.
1725 After executing the global command,
1726 edit will print a warning if the command added or deleted more than one line.
1727 Fortunately, the undo command can reverse
1728 the effects of a global command.
1729 You should experiment with the global command
1730 on a small file of text to see what it can do for you.
1732 More about searching and substituting
1734 In using slashes to identify a character string
1735 that we want to search for or change,
1736 we have always specified the exact characters.
1737 There is a less tedious way to
1738 repeat the same string of characters.
1739 To change ``text'' to ``texts'' we may type either
1740 .DS I 1i
1741 :\|\fB/text/s/text/texts/
1744 as we have done in the past,
1745 or a somewhat abbreviated command:
1746 .DS I 1i
1747 :\|\fB/text/s//texts/
1750 In this example, the characters to be changed
1751 are not specified \-
1752 there are no characters, not even a space,
1753 between the two slash marks
1754 that indicate what is to be changed.
1755 This lack of characters between the slashes
1756 is taken by the editor to mean
1757 ``use the characters we last searched for as the characters to be changed.''
1759 Similarly, the last context search may be repeated
1760 by typing a pair of slashes with nothing between them:
1761 .DS I 1i
1762 :\|\fB/does/
1764 It doesn't mean much here, but
1765 :\|\fB//
1767 it does illustrate the editor.
1769 (You should note that the search command found the characters ``does''
1770 in the word ``doesn't'' in the first search request.)
1771 Because no characters are specified for the second search,
1772 the editor scans the buffer for the next occurrence of the
1773 characters ``does''.
1775 Edit normally searches forward through the buffer,
1776 wrapping around from the end of the buffer to the beginning,
1777 until the specified character string is found.
1778 If you want to search in the reverse direction,
1779 use question marks (?) instead of slashes
1780 to surround the characters you are searching for.
1782 It is also possible
1783 to repeat the last substitution
1784 without having to retype the entire command.
1785 An ampersand (&) used as a command
1786 repeats the most recent substitute command,
1787 using the same search and replacement patterns.
1788 After altering the current line by typing
1789 .DS I 1i
1790 :\|\fBs/text/texts/
1793 you type
1794 .DS I 1i
1795 :\|\fB/text/&
1798 or simply
1799 .DS I 1i
1800 :\|\fB//&
1803 to make the same change on the next line in the buffer
1804 containing the characters ``text''.
1806 Special characters
1808 Two characters have special meanings when
1809 used in specifying searches:  ``$'' and ``^''.
1810 ``$'' is taken by the editor to mean ``end of the line''
1811 and is used to identify strings
1812 that occur at the end of a line.
1813 .DS I 1i
1814 :\|\fBg/text.$/s//material./p
1817 tells the editor to search for all lines ending in ``text.''
1818 (and nothing else, not even a blank space),
1819 to change each final ``text.'' to ``material.'',
1820 and print the changed lines.
1822 The symbol ``^'' indicates the beginning of a line.
1823 Thus,
1824 .DS I 1i
1825 :\|\fBs/^/1. /
1828 instructs the editor to insert ``1.'' and a space at the beginning
1829 of the current line.
1831 The characters ``$'' and ``^'' have special meanings only in the context
1832 of searching.
1833 At other times, they are ordinary characters.
1834 If you ever need to search for a character that has a special meaning,
1835 you must indicate that the
1836 character is to lose temporarily
1837 its special significance by typing another special character,
1838 the backslash (\\), before it.
1839 .DS I 1i
1840 :\|\fBs/\\\\\&$/dollar/
1843 looks for the character ``$'' in the current
1844 line and replaces it by the word ``dollar''.
1845 Were it not for the backslash, the ``$'' would have represented
1846 ``the end of the line'' in your search
1847 rather than the character ``$''.
1848 The backslash retains its special significance
1849 unless it is preceded by another backslash.
1851 Issuing \s-2UNIX\s0 commands from the editor
1853 After creating several files with the editor,
1854 you may want to delete files
1855 no longer useful to you or ask for a list of your files.
1856 Removing and listing files are not functions of the editor,
1857 and so they require the use of \s-2UNIX\s0 system commands
1858 (also referred to as ``shell'' commands, as
1859 ``shell'' is the name of the program that processes \s-2UNIX\s0 commands).
1860 You do not need to quit the editor to execute a \s-2UNIX\s0 command
1861 as long as you indicate that it
1862 is to be sent to the shell for execution.
1863 To use the \s-2UNIX\s0 command
1864 .B rm
1865 to remove the file named ``junk'' type:
1866 .DS I 1i
1867 :\|\fB!rm junk
1872 The exclamation mark (!)
1873 indicates that the rest of the line is to be processed as a shell command.
1874 If the buffer contents have not been written since the last change,
1875 a warning will be printed before the command is executed:
1876 .DS I 1i
1877 [No write since last change]
1879 The editor prints a ``!'' when the command is completed.
1880 Other tutorials describe useful features of the system,
1881 of which an editor is only one part.
1883 Filenames and file manipulation
1885 Throughout each editing session,
1886 edit keeps track of the name of the file being edited as the
1887 .I "current filename."
1888 Edit remembers as the current filename the name given
1889 when you entered the editor.
1890 The current filename changes whenever the edit (e) command
1891 is used to specify a new file.
1892 Once edit has recorded a current filename,
1893 it inserts that name into any command where a filename has been omitted.
1894 If a write command does not specify a file,
1895 edit, as we have seen, supplies the current filename.
1896 If you are editing a file named ``draft3'' having 283 lines in it,
1897 you can have the editor write onto a different file
1898 by including its name in the write command:
1899 .DS I 1i
1900 :\fB\|w chapter3
1902 "chapter3" [new file] 283 lines, 8698 characters
1904 The current filename remembered by the editor
1906 will not be changed as a result of the write command.
1908 Thus, if the next write command
1909 does not specify a name,
1910 edit will write onto the current file (``draft3'')
1911 and not onto the file ``chapter3''.
1913 The file (f) command
1915 To ask for the current filename, type
1916 .B file
1918 .B f ).
1919 In response, the editor provides current information about the buffer,
1920 including the filename, your current position, the number of
1921 lines in the buffer,
1922 and the percent of the distance through the file
1923 your current location is.
1924 .DS I 1i
1925 :\|\fBf
1927 "text" [Modified] line 3 of 4 --75%--
1929 .\"The expression ``[Edited]'' indicates that the buffer contains
1930 .\"either the editor's copy of the existing file ``text''
1931 .\"or a file which you are just now creating.
1932 If the contents of the buffer have changed
1933 since the last time the file was written,
1934 the editor will tell you that the file has been ``[Modified]''.
1935 After you save the changes by writing onto a disk file,
1936 the buffer will no longer be considered modified:
1937 .DS I 1i
1938 :\|\fBw
1940 "text" 4 lines, 88 characters
1941 :\|\fBf
1943 "text" line 3 of 4 --75%--
1946 Reading additional files (r)
1949 \f3read (r)\f1 command allows you to add the contents of a file
1950 to the buffer
1951 at a specified location,
1952 essentially copying new lines
1953 between two existing lines.
1954 To use it, specify the line after which the new text will be placed,
1955 the \f3read (r)\f1 command,
1956 and then the name of the file.
1957 If you have a file named ``example'', the command
1958 .DS I 1i
1959 :\|\fB$r example
1961 "example" 18 lines, 473 characters
1963 reads the file ``example''
1964 and adds it to the buffer after the last line.
1965 The current filename is not changed by the read command.
1967 Writing parts of the buffer
1971 write (w)
1973 command can write all or part of the buffer
1974 to a file you specify.
1975 We are already familiar with
1976 writing the entire contents of the
1977 buffer to a disk file.
1978 To write only part of the buffer onto a file,
1979 indicate the beginning and ending lines before the write command,
1980 for example
1981 .DS I 1i
1982 :\|\fB45,$w ending
1985 Here all lines from 45 through the end of the buffer
1986 are written onto the file named
1987 .I ending.
1988 The lines remain in the buffer
1989 as part of the document you are editing,
1990 and you may continue to edit the entire buffer.
1991 Your original file is unaffected
1992 by your command to write part of the buffer
1993 to another file.
1994 Edit still remembers whether you have saved changes to the buffer
1995 in your original file or not.
1997 Recovering files
1999 Although it does not happen very often,
2000 there are times \s-2UNIX\s+2 stops working
2001 because of some malfunction.
2002 This situation is known as a \fIcrash\fR.
2003 Under most circumstances,
2004 edit's crash recovery feature
2005 is able to save work to within a few lines of changes
2006 before a crash (or an accidental phone hang up).
2007 If you lose the contents of an editing buffer in a system crash,
2008 you will normally receive mail when you login that gives
2009 the name of the recovered file.
2010 To recover the file,
2011 enter the editor and type the command
2012 .B recover
2013 (\fBrec\fR),
2014 followed by the name of the lost file.
2015 For example,
2016 to recover the buffer for an edit session
2017 involving the file ``chap6'', the command is:
2018 .DS I 1i
2020 :\|\fBrecover chap6
2023 Recover is sometimes unable to save the entire buffer successfully,
2024 so always check the contents of the saved buffer carefully
2025 before writing it back onto the original file.
2026 For best results,
2027 write the buffer to a new file temporarily
2028 so you can examine it without risk to the original file.
2029 Unfortunately,
2030 you cannot use the recover command
2031 to retrieve a file you removed
2032 using the shell command \f3rm\f1.
2034 Other recovery techniques
2036 If something goes wrong when you are using the editor,
2037 it may be possible to save your work by using the command
2038 .B preserve
2039 (\fBpre\fR),
2040 which saves the buffer as if the system had crashed.
2041 If you are writing a file and you get the message
2042 ``Quota exceeded'', you have tried to use more disk storage
2043 than is allotted to your account.
2045 Proceed with caution
2047 because it is likely that only a part
2048 of the editor's buffer is now present in the file you tried to write.
2049 In this case you should use the shell escape from the editor (!)
2050 to remove some files you don't need and try to write
2051 the file again.
2052 If this is not possible and you cannot find someone to help you,
2053 enter the command
2054 .DS I 1i
2055 :\|\fBpreserve
2058 and wait for the reply,
2059 .DS I 1i
2060 File preserved.
2062 If you do not receive this reply,
2063 seek help immediately.
2064 Do not simply leave the editor.
2065 If you do, the buffer will be lost, 
2066 and you may not be able to save your file.
2067 If the reply is ``File preserved.''
2068 you can leave the editor
2069 (or logout)
2070 to remedy the situation.
2071 After a preserve, you can use the recover command
2072 once the problem has been corrected,
2073 or the \fB\-r\fR option of the edit command
2074 if you leave the editor and want to return.
2076 If you make an undesirable change to the buffer
2077 and type a write command before discovering your mistake,
2078 the modified version will replace any previous version of the file.
2079 Should you ever lose a good version of a document in this way,
2080 do not panic and leave the editor.
2081 As long as you stay in the editor,
2082 the contents of the buffer remain accessible.
2083 Depending on the nature of the problem,
2084 it may be possible
2085 to restore the buffer to a more complete
2086 state with the undo command.
2087 After fixing the damaged buffer, you can again write the file
2088 to disk.
2090 Further reading and other information
2092 Edit is an editor designed for beginning and casual users.
2093 It is actually a version of a more powerful editor called
2094 .I ex.
2095 These lessons are intended to introduce you to the editor
2096 and its more commonly-used commands.
2097 We have not covered all of the editor's commands,
2098 but a selection of commands
2099 that should be sufficient to accomplish most of your editing tasks.
2100 You can find out more about the editor in the
2102 Ex Reference Manual,
2104 which is applicable to both
2105 .I ex
2107 .I edit.
2108 One way to become familiar with the manual is to begin by reading
2109 the description of commands that you already know.
2110 .bd I 3
2112 Using
2113 .I ex
2115 .bd I
2117 As you become more experienced with using the editor,
2118 you may still find that edit continues to meet your needs.
2119 However, should you become interested in using 
2120 .I ex,
2121 it is easy to switch.
2122 To begin an editing session with 
2123 .I ex,
2124 use the name
2125 .B ex
2126 in your command instead of
2127 .B edit.
2129 Edit commands also work in 
2130 .I ex,
2131 but the editing environment is somewhat different.
2132 You should be aware of a few differences
2133 between 
2134 .I ex
2135 and 
2136 .I edit.
2137 In edit, only the characters ``^'', ``$'', and ``\\'' have
2138 special meanings in searching the buffer
2139 or indicating characters to be changed by a substitute command.
2140 Several additional characters have special
2141 meanings in ex, as described in the
2143 Ex Reference Manual.
2145 Another feature of the edit environment prevents users from
2146 accidently entering two alternative modes of editing,
2147 .I open
2149 .I visual,
2150 in which
2151 the editor behaves quite differently from normal command mode.
2152 If you are using ex and you encounter strange behavior,
2153 you may have accidently entered open mode by typing ``o''.
2154 Type the \s-2ESC\s0 key and then a ``Q''
2155 to get out of open or visual mode and back into
2156 the regular editor command mode.
2157 The document
2159 An Introduction to Display Editing with Vi\|\|
2161 provide full details of visual mode.
2164 .ce 1
2165 \s+2Index\s0
2167 .sp 2
2170 addressing, \fIsee\fR line numbers
2171 ampersand, 20
2172 append mode, 6-7
2173 append (a) command, 6, 7, 9
2174 ``At end of file'' (message), 18
2175 backslash (\\), 21
2176 buffer, 3
2177 caret (^), 10, 20
2178 change (c) command, 18
2179 command mode, 5-6
2180 ``Command not found'' (message), 6
2181 context search, 10-12, 19-21
2182 control characters (``^'' notation), 10
2183 control-H, 7
2184 copy (co) command, 15
2185 corrections, 7, 16
2186 current filename, 21
2187 current line (\|.\|), 11, 17
2188 delete (d) command, 15-16
2189 dial-up, 5
2190 disk, 3
2191 documentation, 3, 23
2192 dollar ($), 10, 11, 17, 20-21
2193 dot (\f3\|.\|\f1) 11, 17
2194 edit (text editor), 3, 5, 23
2195 edit (e) command, 5, 9, 14
2196 editing commands:
2197 .in +.25i
2198 append (a), 6, 7, 9
2199 change (c), 18
2200 copy (co), 15
2201 delete (d), 15-16
2202 edit (text editor), 3, 5, 23
2203 edit (e), 5, 9, 14
2204 file (f), 21-22
2205 global (g), 19
2206 move (m), 14-15
2207 number (nu), 11
2208 preserve (pre), 22-23
2209 print (p), 10
2210 quit (q), 8, 13
2211 read (r), 22
2212 recover (rec), 22, 23
2213 substitute (s), 11-12, 19, 20
2214 undo (u), 16-17, 23
2215 write (w), 8, 13, 21, 22
2216 z, 12-13
2217 ! (shell escape), 21
2218 $=, 17
2219 +, 17
2220 \-, 17
2221 //, 12, 20
2222 ??, 20
2223 \&., 11, 17
2224 \&.=, 11, 17
2225 .in -.25i
2226 entering text, 3, 6-7
2227 erasing
2228 .in +.25i
2229 characters (^H), 7
2230 lines (@), 7
2231 .in -.25i
2232 error corrections, 7, 16
2233 ex (text editor), 23
2234 \fIEx Reference Manual\fR, 23
2235 exclamation (!), 21
2236 file, 3
2237 file (f) command, 21-22
2238 file recovery, 22-23
2239 filename, 3, 21
2240 global (g) command, 19
2241 input mode, 6-7
2242 Interrupt (message), 9
2243 line numbers, \fIsee also\fR current line
2244 .in +.25i
2245 dollar sign ($), 10, 11, 17
2246 dot (\|.\|), 11, 17
2247 relative (+ and \-), 17
2248 .in -.25i
2249 list, 10
2250 logging in, 4-6
2251 logging out, 8
2252 ``Login incorrect'' (message), 5
2253 minus (\-), 17
2254 move (m) command, 14-15
2255 ``Negative address\(emfirst buffer line is 1'' (message), 18
2256 ``No current filename'' (message), 8
2257 ``No such file or directory'' (message), 5, 6
2258 ``No write since last change'' (message), 21
2259 non-printing characters, 10
2260 ``Nonzero address required'' (message), 18
2261 ``Not an editor command'' (message), 6
2262 ``Not that many lines in buffer'' (message), 18
2263 number (nu) command, 11
2264 password, 5
2265 period (\|.\|), 11, 17
2266 plus (+), 17
2267 preserve (pre) command, 22-23
2268 print (p) command, 10
2269 program, 3
2270 prompts
2271 .in .25i
2272 % (\s-2UNIX\s0), 5
2273 : (edit), 5, 6, 7
2274 \0 (append), 7
2275 .in -.25i
2276 question (?), 20
2277 quit (q) command, 8, 13
2278 read (r) command, 22
2279 recover (rec) command, 22, 23
2280 recovery, \fIsee\fR\| file recovery
2281 references, 3, 23
2282 remove (rm) command, 21, 22
2283 reverse command effects (undo), 16-17, 23
2284 searching, 10-12, 19-21
2285 shell, 21
2286 shell escape (!), 21
2287 slash (/), 11-12, 20
2288 special characters (^, $, \\), 10, 11, 17, 20-21
2289 substitute (s) command, 11-12, 19, 20
2290 terminals, 4-5
2291 text input mode, 7
2292 undo (u) command, 16-17, 23
2293 \s-1UNIX\s0, 3
2294 write (w) command, 8, 13, 21, 22
2295 z command, 12-13