ex: convert line input buffer from file encoding to internal encoding
[nvi.git] / docs / edit / edittut.ms
blobd317941bf24aeba1296c680a39e9af6aee76fa37
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.3 1996/08/18 11:36:05 bostic Exp $ (Berkeley) $Date: 1996/08/18 11:36:05 $
7 .\"
8 .ll 6.5i
9 .nr LL 6.5i
10 .EH 'USD:11-%''Edit:  A Tutorial'
11 .OH 'Edit:  A Tutorial''USD:11-%'
12 .LP
13 .ds u \s-2UNIX\s0
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:
1011 .in +1i
1013 l l.
1014 \fB/strange/\fP tells edit to find the characters ``strange'' in the text
1015 \fBs\fP tells edit to make a substitution
1016 \fB/strange, but //\fP  substitutes nothing at all for the characters ``strange, but ''
1018 .in -1i
1020 You should note the space after ``but'' in ``/strange, but /''. 
1021 If you do not indicate that the space is to be taken out,
1022 your line will read:
1023 .DS I 1i
1024 .if t Text editing is   nice.
1025 .if n Text editing is  nice.
1027 which looks a little funny   
1028 because of the extra space between ``is'' and ``nice''.
1029 Again, we realize from this that a blank space
1030 is a real character to a computer, and in editing text
1031 we need to be aware of spaces
1032 within a line just as we would be aware of an ``a'' or 
1033 a ``4''.
1035 Another way to list what's in the buffer (z)
1037 Although the print command is useful for looking at specific lines
1038 in the buffer,
1039 other commands may be more convenient for
1040 viewing large sections of text.
1041 You can ask to see a screen full of text at a time
1042 by using the command
1043 .B z.
1044 If you type
1045 .DS I 1i
1046 :\|\fB1z
1049 edit will start with line 1 and continue printing lines,
1050 stopping either when the screen of
1051 your terminal is full
1052 or when the last line in the buffer has been printed.
1053 If you want to read the next segment of text, type the command
1054 .DS I 1i
1055 :\|\fBz
1057 If no starting line number is given for the z command,
1058 printing will start at the ``current'' line, in this case the
1059 last line printed.
1060 Viewing lines in the buffer one screen full at a time
1061 is known as \fIpaging\fR.
1062 Paging can also be used to print
1063 a section of text on a hard-copy terminal.
1065 Saving the modified text
1067 This seems to be a good place to pause in our work,
1068 and so we should end the second session.
1069 If you (in haste) type ``q'' to quit the session
1070 your dialogue with edit will be:
1071 .DS I 1i
1072 :\|\fBq
1074 No write since last change (:quit! overrides)
1077 This is edit's warning that you have not written
1078 the modified contents of the buffer to disk.
1079 You run the risk of losing the work you did
1080 during the editing session since you typed the latest write
1081 command.
1082 Because in this lesson we have not written
1083 to disk at all, everything we have done
1084 would have been lost
1085 if edit had obeyed the \fBq\fR command.
1086 If you did not want to save the work done during
1087 this editing session, you would have to type ``q!''
1088 or (``quit!'')
1089 to confirm that you indeed wanted to end the session
1090 immediately,
1091 leaving the file as it was
1092 after the most recent ``write'' command.
1093 However,
1094 since you want to save what
1095 you have edited, you need to type:
1096 .DS I 1i
1097 :\|\fBw
1099 "text" 6 lines, 171 characters
1101 and then follow with the commands to quit and logout:
1102 .DS I 1i
1103 :\|\fBq
1104 % \fBlogout\fR
1106 and hang up the phone or turn off the terminal when
1107 \s-2UNIX\s0 asks for a name.
1108 Terminals connected to the port selector
1109 will stop after the logout command,
1110 and pressing keys on the keyboard will do nothing.
1111 .sp 1
1113 This is the end of the second session on \s-2UNIX\s0 text editing.
1116 Session 3
1118 Bringing text into the buffer (e)
1120 Login to \s-2UNIX\s0 and make contact with edit.  
1121 You should try to login without
1122 looking at the notes, but if you must
1123 then by all means do.
1125 Did you remember to give the name of the file
1126 you wanted to edit?
1127 That is, did you type
1128 .DS I 1i
1129 % \fBedit text\fR
1131 or simply
1132 .DS I 1i
1133 % \fBedit\fR
1135 Both ways get you in contact with edit, but the first way
1136 will bring a copy of the file named ``text'' into
1137 the buffer.  
1138 If you did forget to tell edit the name of your file,
1139 you can get it into the buffer by
1140 typing:
1141 .DS I 1i
1142 :\|\fBe text
1144 "text" 6 lines, 171 characters
1146 The command
1147 .B edit,
1148 which may be abbreviated \fBe\fR,
1149 tells edit that you want
1150 to erase anything that might already be in 
1151 the buffer and bring a copy of the file ``text'' into the buffer
1152 for editing.
1153 You may also use the edit (e) command to change files in
1154 the middle of an editing session,
1155 or to give edit the name of a new file that you want to create.
1156 Because the edit command clears the buffer,
1157 you will receive a warning if you try to edit a new file without
1158 having saved a copy of the old file.
1159 This gives you a chance to write the contents of the buffer to disk
1160 before editing the next file.
1162 Moving text in the buffer (m)
1164 Edit allows you to move lines of text
1165 from one location in the buffer to another
1166 by means of the
1167 .B move
1168 (\fBm\fR) command.
1169 The first two examples are for illustration only,
1170 though after you have read this Session
1171 you are welcome to return to them for practice.
1172 The command
1173 .DS I 1i
1174 :\|\fB2,4m$
1177 directs edit to move lines 2, 3, and 4
1178 to the end of the buffer ($).  
1179 The format for the move command is that you specify
1180 the first line to be moved, the last line to be moved,
1181 the move command ``m'', and the line after which
1182 the moved text is to be placed.
1184 .DS I 1i
1185 :\|\fB1,3m6
1188 would instruct edit to move lines 1 through 3 (inclusive) 
1189 to a location after line 6 in the buffer.
1190 To move only one line, say, line 4,
1191 to a location in the buffer after line 5, 
1192 the command would be ``4m5''.
1194 Let's move some text using the command:
1195 .DS I 1i
1196 :\|\fB5,$m1
1198 2 lines moved
1199 it does illustrate the editor.
1201 After executing a command that moves more than one line of the buffer,
1202 edit tells how many lines were affected by the move
1203 and prints the last moved line for your inspection.
1204 If you want to see more than just the last line,
1205 you can then
1206 use the print (p), z, or number (nu) command to view more text.
1207 The buffer should now contain:
1208 .DS I 1i
1209 This is some sample text.
1210 It doesn't mean much here, but
1211 it does illustrate the editor.
1212 And this is some more text.
1213 Text editing is nice.
1214 This is text added in Session 2.
1216 You can restore the original order by typing:
1217 .DS I 1i
1218 :\|\fB4,$m1
1221 or, combining context searching and the move command:
1222 .DS I 1i
1223 :\|\fB/And this is some/,/This is text/m/This is some sample/
1226 (Do not type both examples here!)
1227 The problem with combining context searching
1228 with the move command 
1229 is that your chance of making a typing error
1230 in such a long command is greater than
1231 if you type line numbers.
1233 Copying lines (copy)
1236 .B copy
1237 command
1238 is used to make a second copy of specified lines,
1239 leaving the original lines where they were.
1240 Copy
1241 has the same format as the move command, for example:
1242 .DS I 1i
1243 :\|\fB2,5copy $
1246 makes a copy of lines 2 through 5,
1247 placing the added lines after the buffer's end ($).
1248 Experiment with the copy command
1249 so that you can become familiar with how it works.
1250 Note that the shortest abbreviation for copy is
1251 \f3co\f1 (and
1252 not the letter ``c'', which has another meaning).
1254 Deleting lines (d)
1256 Suppose you want to delete 
1257 the line
1258 .DS I 1i
1259 This is text added in Session 2.
1261 from the buffer.
1262 If you know the number of the line to be deleted,
1263 you can type
1264 that number followed by
1265 \fBdelete\fR or \fBd\fR.
1266 This example deletes line 4,
1267 which is ``This is text added in Session 2.''
1268 if you typed the commands
1269 suggested so far.
1270 .DS I 1i
1271 :\|\fB4d
1273 It doesn't mean much here, but
1275 Here ``4'' is the number of the line to be deleted,
1276 and ``delete'' or ``d'' is the command to delete the line.
1277 After executing the delete command,
1278 edit prints the line that has become the current line (``.'').
1280 If you do not happen to know the line number
1281 you can search for the line and then delete it using this
1282 sequence of commands:
1283 .DS I 1i
1284 :\|\fB/added in Session 2./
1286 This is text added in Session 2.
1287 :\|\fBd
1289 It doesn't mean much here, but
1291 The ``/added in Session 2./''
1292 asks edit to locate and print
1293 the line containing the indicated text,
1294 starting its search at the current line
1295 and moving line by line
1296 until it finds the text.
1297 Once you are sure that you have correctly specified the line
1298 you want to delete,
1299 you can enter the delete (d) command.
1300 In this case it is not necessary to
1301 specify a line number before the ``d''.
1302 If no line number is given,
1303 edit deletes the current line (``.''),
1304 that is, the line found by our search.
1305 After the deletion, your buffer should contain:
1306 .DS I 1i
1307 This is some sample text.
1308 And this is some more text.
1309 Text editing is nice.
1310 It doesn't mean much here, but
1311 it does illustrate the editor.
1312 And this is some more text.
1313 Text editing is nice.
1314 This is text added in Session 2.
1315 It doesn't mean much here, but
1317 To delete both lines 2 and 3:
1318 .DS I 1i
1319 And this is some more text.
1320 Text editing is nice.
1322 you type
1323 .DS I 1i
1324 :\|\f32,3d\f1
1325 2 lines deleted
1327 which specifies the range of lines from 2 to 3,
1328 and the operation on those lines \(em ``d'' for delete.
1329 If you delete more than one line
1330 you will receive a message
1331 telling you the number of lines deleted,
1332 as indicated in the example above.
1334 The previous example assumes that you know the line numbers for
1335 the lines to be deleted.
1336 If you do not you might combine the search command
1337 with the delete command:
1338 .DS I 1i
1339 :\|\fB/And this is some/,/Text editing is nice./d
1343 A word or two of caution
1345 In using the search function to locate lines to
1346 be deleted you should be
1348 absolutely sure
1350 the characters you give as the basis for the search
1351 will take edit to the line you want deleted.
1352 Edit will search for the first
1353 occurrence of the characters starting from where
1354 you last edited \-
1355 that is, from the line you see printed if you type dot (.).
1357 A search based on too few
1358 characters may result in the wrong lines being deleted,
1359 which edit will do as easily as if you had meant it.
1360 For this reason, it is usually safer
1361 to specify the search and then delete in two separate steps,
1362 at least until you become familiar enough with using the editor
1363 that you understand how best to specify searches.
1364 For a beginner it is not a bad idea to double-check
1365 each command before pressing \s-2RETURN\s+2 to send the command on its way.
1367 Undo (u) to the rescue
1371 undo (u)
1373 command has the ability to
1374 reverse the effects of the last command that changed the buffer.
1375 To undo the previous command, type
1376 ``u'' or ``undo''.
1377 Undo can rescue
1378 the contents of the buffer from many an unfortunate mistake.
1379 However, its powers are not unlimited,
1380 so it is still wise to be reasonably
1381 careful about the commands you give.
1383 It is possible to undo only commands which
1384 have the power to change the buffer \(em for example,
1385 delete, append, move, copy, substitute, and even undo itself.
1386 The commands write (w) and edit (e), which interact with disk files,
1387 cannot be undone, nor can commands that do not change
1388 the buffer, such as print.
1389 Most importantly,
1391 .B only
1392 command that can be reversed by undo
1393 is the
1394 last ``undo-able'' command you typed.
1395 You can use control-H and @ to change
1396 commands while you are typing them,
1397 and undo to reverse the effect of the commands
1398 after you have typed them and pressed \s-2RETURN\s+2.
1400 To illustrate,
1401 let's issue an undo command.
1402 Recall that the last buffer-changing command we gave deleted
1403 the lines formerly numbered 2 and 3.
1404 Typing undo at this moment will reverse the effects
1405 of the deletion, causing those two lines to be
1406 replaced in the buffer.
1407 .DS I 1i
1408 :\|\fBu
1410 2 more lines in file after undo
1411 And this is some more text.
1413 Here again, edit informs you if the command affects more
1414 than one line,
1415 and prints
1416 the text of the line which is now ``dot'' (the current line).
1418 More about the dot (.) and buffer end ($)
1420 The function assumed by the symbol dot depends on its context.
1421 It can be used:
1423 1.  to exit from append mode; we type dot (and only a dot) on
1424 a line and press \s-2RETURN\s+2;
1426 2.  to refer to the line we are at in the buffer.
1428 Dot can also be combined with the equal sign to get
1429 the number of the line currently being edited:
1430 .DS I 1i
1431 :\|\fB\&.=
1434 If we type ``\fB.\fR='' we are asking for the number of the line,
1435 and if we type ``\fB.\fR'' we are asking for the text of the line.
1437 In this editing session and the last, we used the dollar
1438 sign to indicate the end of the buffer
1439 in commands such as print, copy, and move.
1440 The dollar sign as a command asks edit to print the last
1441 line in the buffer.
1442 If the dollar sign is combined with the equal sign (\f3$=\f1)
1443 edit will print the line number corresponding to the
1444 last line in the buffer.
1446 ``\fB.\fR'' and ``$'', then, represent line numbers.
1447 Whenever appropriate, these symbols can be used in
1448 place of line numbers in commands.
1449 For example
1450 .DS I 1i
1451 :\|\fB\s+2.\s-2,$d
1454 instructs edit to delete all lines from the current line (\fB.\fR)
1455 to the end of the buffer.
1457 Moving around in the buffer  (+ and \-)
1459 When you are editing
1460 you often want
1461 to go back and re-read a previous line.
1462 You could specify a context search for a line you want to
1463 read if you remember some of its text,
1464 but if you simply want to see what was written a few, say 3, lines
1465 ago, you can type
1466 .DS I 1i
1467 \-3p
1469 This tells edit to move back to a position 3 lines
1470 before the current line (.)
1471 and print that line.
1472 You can move forward in the buffer similarly:
1473 .DS I 1i
1476 instructs edit to print the line that is 2
1477 ahead of your current position.
1479 You may use ``+'' and ``\-'' in any command where edit
1480 accepts line numbers.
1481 Line numbers specified with ``+'' or ``\-''
1482 can be combined to print a range of lines.
1483 The command
1484 .DS I 1i
1485 :\|\fB\-1,+2copy$
1488 makes a copy of 4 lines:  the current line, the line before it,
1489 and the two after it.
1490 The copied lines will be placed after the last line
1491 in the buffer ($),
1492 and the original lines referred to by ``\-1'' and ``+2''
1493 remain where they are.
1495 Try typing only ``\-''; you will move back one line just as
1496 if you had typed ``\-1p''.
1497 Typing the command ``+'' works similarly.
1498 You might also try typing a few plus or minus signs in a row
1499 (such as ``+++'') to see edit's response.
1500 Typing \s-2RETURN\s+2 alone on a line is the equivalent
1501 of typing ``+1p''; it will move you one line ahead in the buffer
1502 and print that line.
1504 If you are at the last line of the buffer and try
1505 to move further ahead, perhaps by typing a ``+'' or
1506 a carriage return alone on the line,
1507 edit will remind you that you are at the end of the buffer:
1510 .ti 1i
1511 At end-of-file
1514 .ti 1i
1515 Not that many lines in buffer
1518 Similarly, if you try to move to a position before the first line,
1519 edit will print one of these messages:
1522 .ti 1i
1523 Nonzero address required on this command
1526 .ti 1i
1527 Negative address \- first buffer line is 1
1530 The number associated with a buffer line is the line's ``address'',
1531 in that it can be used to locate the line.
1533 Changing lines (c)
1535 You can also delete certain lines and
1536 insert new text in their place.
1537 This can be accomplished easily with the
1538 .B "change (c)"
1539 command.
1540 The change command instructs edit to delete specified lines
1541 and then switch to text input mode to
1542 accept the text that will replace them.
1543 Let's say you want to change the first two lines in the buffer:
1544 .DS I 1i
1545 This is some sample text.
1546 And this is some more text.
1548 to read
1549 .DS I 1i
1550 This text was created with the \s-2UNIX\s0 text editor.
1552 To do so, you type:
1553 .DS I 1i
1554 :\|\fB1,2c
1556 2 lines changed
1558 This text was created with the \s-2UNIX\s0 text editor.
1559 \s+2\&.\s-2
1563 In the command
1564 .B 1,2c
1565 we specify that we want to change
1566 the range of lines beginning with 1 and ending with 2
1567 by giving line numbers as with the print command.
1568 These lines will be deleted.
1569 After you type \s-2RETURN\s+2 to end the change command,
1570 edit notifies you if more than one line will be changed
1571 and places you in text input mode.
1572 Any text typed on the following lines will be inserted into
1573 the position where lines were deleted by the change command.
1575 You will remain in text input mode until you exit in the usual way,
1576 by typing a period alone on a line.
1578 Note that the number of lines added to the buffer need not be
1579 the same as the number of lines deleted.
1580 .sp 1
1582 This is the end of the third session on text editing with \s-2UNIX\s0.
1585 .ce 1
1586 \s+2Session 4\s0
1589 This lesson covers several topics, starting with
1590 commands that apply throughout the buffer,
1591 characters with special meanings,
1592 and how to issue \s-2UNIX\s0 commands while in the editor.
1593 The next topics deal with files:
1594 more on reading and writing,
1595 and methods of recovering files lost in a crash.
1596 The final section suggests sources of further information.
1598 Making commands global (g)
1600 One disadvantage to the commands we have used for
1601 searching or substituting is that if you
1602 have a number of instances of a word to change 
1603 it appears that you have to type the command 
1604 repeatedly, once for
1605 each time the change needs to be made.
1606 Edit, however, provides a way to make commands
1607 apply to the entire contents of the buffer \-
1610 global (g)
1612 command.
1614 To print all lines
1615 containing a certain sequence of characters
1616 (say, ``text'')
1617 the command is:
1618 .DS I 1i
1619 :\|\fBg/text/p
1622 The ``g'' instructs edit to
1623 make a global search for all lines
1624 in the buffer containing the characters  ``text''.
1625 The ``p'' prints the lines found.
1627 To issue a global command, start by typing a ``g'' and then a search
1628 pattern identifying
1629 the lines to be affected.
1630 Then, on the same line, type the command to be
1631 executed for the identified lines.
1632 Global substitutions are frequently useful.
1633 For example,
1634 to change all instances of the word ``text'' to the word ``material''
1635 the command would be a combination of the global search and the
1636 substitute command:
1637 .DS I 1i
1638 :\|\fBg/text/s/text/material/g
1641 Note the ``g'' at the end of the global command,
1642 which instructs edit to change
1643 each and every instance of ``text'' to ``material''.
1644 If you do not type the ``g'' at the end of the command
1645 only the
1646 .I first
1647 instance of ``text'' \fIin each line\fR will be changed
1648 (the normal result of the substitute command).
1649 The ``g'' at the end of the command is independent of the ``g''
1650 at the beginning.
1651 You may give a command such as:
1652 .DS I 1i
1653 :\|\fB5s/text/material/g
1656 to change every instance of ``text'' in line 5 alone.
1657 Further, neither command will change ``text'' to ``material''
1658 if ``Text'' begins with a capital rather than a lower-case
1659 .I t.
1661 Edit does not automatically print the lines modified by a
1662 global command.
1663 If you want the lines to be printed, type a ``p''
1664 at the end of the global command:
1665 .DS I 1i
1666 :\|\fBg/text/s/text/material/gp
1669 You should be careful
1670 about using the global command in combination with any other \-
1671 in essence, be sure of what you are telling edit to do
1672 to the entire buffer.
1673 For example,
1674 .DS I 1i
1675 :\|\fBg/ /d
1677 72 less lines in file after global
1679 will delete every line containing a blank anywhere in it.
1680 This could adversely affect
1681 your document, since most lines have spaces between words
1682 and thus would be deleted.
1683 After executing the global command,
1684 edit will print a warning if the command added or deleted more than one line.
1685 Fortunately, the undo command can reverse
1686 the effects of a global command.
1687 You should experiment with the global command
1688 on a small file of text to see what it can do for you.
1690 More about searching and substituting
1692 In using slashes to identify a character string
1693 that we want to search for or change,
1694 we have always specified the exact characters.
1695 There is a less tedious way to
1696 repeat the same string of characters.
1697 To change ``text'' to ``texts'' we may type either
1698 .DS I 1i
1699 :\|\fB/text/s/text/texts/
1702 as we have done in the past,
1703 or a somewhat abbreviated command:
1704 .DS I 1i
1705 :\|\fB/text/s//texts/
1708 In this example, the characters to be changed
1709 are not specified \-
1710 there are no characters, not even a space,
1711 between the two slash marks
1712 that indicate what is to be changed.
1713 This lack of characters between the slashes
1714 is taken by the editor to mean
1715 ``use the characters we last searched for as the characters to be changed.''
1717 Similarly, the last context search may be repeated
1718 by typing a pair of slashes with nothing between them:
1719 .DS I 1i
1720 :\|\fB/does/
1722 It doesn't mean much here, but
1723 :\|\fB//
1725 it does illustrate the editor.
1727 (You should note that the search command found the characters ``does''
1728 in the word ``doesn't'' in the first search request.)
1729 Because no characters are specified for the second search,
1730 the editor scans the buffer for the next occurrence of the
1731 characters ``does''.
1733 Edit normally searches forward through the buffer,
1734 wrapping around from the end of the buffer to the beginning,
1735 until the specified character string is found.
1736 If you want to search in the reverse direction,
1737 use question marks (?) instead of slashes
1738 to surround the characters you are searching for.
1740 It is also possible
1741 to repeat the last substitution
1742 without having to retype the entire command.
1743 An ampersand (&) used as a command
1744 repeats the most recent substitute command,
1745 using the same search and replacement patterns.
1746 After altering the current line by typing
1747 .DS I 1i
1748 :\|\fBs/text/texts/
1751 you type
1752 .DS I 1i
1753 :\|\fB/text/&
1756 or simply
1757 .DS I 1i
1758 :\|\fB//&
1761 to make the same change on the next line in the buffer
1762 containing the characters ``text''.
1764 Special characters
1766 Two characters have special meanings when
1767 used in specifying searches:  ``$'' and ``^''.
1768 ``$'' is taken by the editor to mean ``end of the line''
1769 and is used to identify strings
1770 that occur at the end of a line.
1771 .DS I 1i
1772 :\|\fBg/text.$/s//material./p
1775 tells the editor to search for all lines ending in ``text.''
1776 (and nothing else, not even a blank space),
1777 to change each final ``text.'' to ``material.'',
1778 and print the changed lines.
1780 The symbol ``^'' indicates the beginning of a line.
1781 Thus,
1782 .DS I 1i
1783 :\|\fBs/^/1. /
1786 instructs the editor to insert ``1.'' and a space at the beginning
1787 of the current line.
1789 The characters ``$'' and ``^'' have special meanings only in the context
1790 of searching.
1791 At other times, they are ordinary characters.
1792 If you ever need to search for a character that has a special meaning,
1793 you must indicate that the
1794 character is to lose temporarily
1795 its special significance by typing another special character,
1796 the backslash (\\), before it.
1797 .DS I 1i
1798 :\|\fBs/\\\\\&$/dollar/
1801 looks for the character ``$'' in the current
1802 line and replaces it by the word ``dollar''.
1803 Were it not for the backslash, the ``$'' would have represented
1804 ``the end of the line'' in your search
1805 rather than the character ``$''.
1806 The backslash retains its special significance
1807 unless it is preceded by another backslash.
1809 Issuing \s-2UNIX\s0 commands from the editor
1811 After creating several files with the editor,
1812 you may want to delete files
1813 no longer useful to you or ask for a list of your files.
1814 Removing and listing files are not functions of the editor,
1815 and so they require the use of \s-2UNIX\s0 system commands
1816 (also referred to as ``shell'' commands, as
1817 ``shell'' is the name of the program that processes \s-2UNIX\s0 commands).
1818 You do not need to quit the editor to execute a \s-2UNIX\s0 command
1819 as long as you indicate that it
1820 is to be sent to the shell for execution.
1821 To use the \s-2UNIX\s0 command
1822 .B rm
1823 to remove the file named ``junk'' type:
1824 .DS I 1i
1825 :\|\fB!rm junk
1830 The exclamation mark (!)
1831 indicates that the rest of the line is to be processed as a shell command.
1832 If the buffer contents have not been written since the last change,
1833 a warning will be printed before the command is executed:
1834 .DS I 1i
1835 [No write since last change]
1837 The editor prints a ``!'' when the command is completed.
1838 Other tutorials describe useful features of the system,
1839 of which an editor is only one part.
1841 Filenames and file manipulation
1843 Throughout each editing session,
1844 edit keeps track of the name of the file being edited as the
1845 .I "current filename."
1846 Edit remembers as the current filename the name given
1847 when you entered the editor.
1848 The current filename changes whenever the edit (e) command
1849 is used to specify a new file.
1850 Once edit has recorded a current filename,
1851 it inserts that name into any command where a filename has been omitted.
1852 If a write command does not specify a file,
1853 edit, as we have seen, supplies the current filename.
1854 If you are editing a file named ``draft3'' having 283 lines in it,
1855 you can have the editor write onto a different file
1856 by including its name in the write command:
1857 .DS I 1i
1858 :\fB\|w chapter3
1860 "chapter3" [new file] 283 lines, 8698 characters
1862 The current filename remembered by the editor
1864 will not be changed as a result of the write command.
1866 Thus, if the next write command
1867 does not specify a name,
1868 edit will write onto the current file (``draft3'')
1869 and not onto the file ``chapter3''.
1871 The file (f) command
1873 To ask for the current filename, type
1874 .B file
1876 .B f ).
1877 In response, the editor provides current information about the buffer,
1878 including the filename, your current position, the number of
1879 lines in the buffer,
1880 and the percent of the distance through the file
1881 your current location is.
1882 .DS I 1i
1883 :\|\fBf
1885 "text" [Modified] line 3 of 4 --75%--
1887 .\"The expression ``[Edited]'' indicates that the buffer contains
1888 .\"either the editor's copy of the existing file ``text''
1889 .\"or a file which you are just now creating.
1890 If the contents of the buffer have changed
1891 since the last time the file was written,
1892 the editor will tell you that the file has been ``[Modified]''.
1893 After you save the changes by writing onto a disk file,
1894 the buffer will no longer be considered modified:
1895 .DS I 1i
1896 :\|\fBw
1898 "text" 4 lines, 88 characters
1899 :\|\fBf
1901 "text" line 3 of 4 --75%--
1904 Reading additional files (r)
1907 \f3read (r)\f1 command allows you to add the contents of a file
1908 to the buffer
1909 at a specified location,
1910 essentially copying new lines
1911 between two existing lines.
1912 To use it, specify the line after which the new text will be placed,
1913 the \f3read (r)\f1 command,
1914 and then the name of the file.
1915 If you have a file named ``example'', the command
1916 .DS I 1i
1917 :\|\fB$r example
1919 "example" 18 lines, 473 characters
1921 reads the file ``example''
1922 and adds it to the buffer after the last line.
1923 The current filename is not changed by the read command.
1925 Writing parts of the buffer
1929 write (w)
1931 command can write all or part of the buffer
1932 to a file you specify.
1933 We are already familiar with
1934 writing the entire contents of the
1935 buffer to a disk file.
1936 To write only part of the buffer onto a file,
1937 indicate the beginning and ending lines before the write command,
1938 for example
1939 .DS I 1i
1940 :\|\fB45,$w ending
1943 Here all lines from 45 through the end of the buffer
1944 are written onto the file named
1945 .I ending.
1946 The lines remain in the buffer
1947 as part of the document you are editing,
1948 and you may continue to edit the entire buffer.
1949 Your original file is unaffected
1950 by your command to write part of the buffer
1951 to another file.
1952 Edit still remembers whether you have saved changes to the buffer
1953 in your original file or not.
1955 Recovering files
1957 Although it does not happen very often,
1958 there are times \s-2UNIX\s+2 stops working
1959 because of some malfunction.
1960 This situation is known as a \fIcrash\fR.
1961 Under most circumstances,
1962 edit's crash recovery feature
1963 is able to save work to within a few lines of changes
1964 before a crash (or an accidental phone hang up).
1965 If you lose the contents of an editing buffer in a system crash,
1966 you will normally receive mail when you login that gives
1967 the name of the recovered file.
1968 To recover the file,
1969 enter the editor and type the command
1970 .B recover
1971 (\fBrec\fR),
1972 followed by the name of the lost file.
1973 For example,
1974 to recover the buffer for an edit session
1975 involving the file ``chap6'', the command is:
1976 .DS I 1i
1978 :\|\fBrecover chap6
1981 Recover is sometimes unable to save the entire buffer successfully,
1982 so always check the contents of the saved buffer carefully
1983 before writing it back onto the original file.
1984 For best results,
1985 write the buffer to a new file temporarily
1986 so you can examine it without risk to the original file.
1987 Unfortunately,
1988 you cannot use the recover command
1989 to retrieve a file you removed
1990 using the shell command \f3rm\f1.
1992 Other recovery techniques
1994 If something goes wrong when you are using the editor,
1995 it may be possible to save your work by using the command
1996 .B preserve
1997 (\fBpre\fR),
1998 which saves the buffer as if the system had crashed.
1999 If you are writing a file and you get the message
2000 ``Quota exceeded'', you have tried to use more disk storage
2001 than is allotted to your account.
2003 Proceed with caution
2005 because it is likely that only a part
2006 of the editor's buffer is now present in the file you tried to write.
2007 In this case you should use the shell escape from the editor (!)
2008 to remove some files you don't need and try to write
2009 the file again.
2010 If this is not possible and you cannot find someone to help you,
2011 enter the command
2012 .DS I 1i
2013 :\|\fBpreserve
2016 and wait for the reply,
2017 .DS I 1i
2018 File preserved.
2020 If you do not receive this reply,
2021 seek help immediately.
2022 Do not simply leave the editor.
2023 If you do, the buffer will be lost, 
2024 and you may not be able to save your file.
2025 If the reply is ``File preserved.''
2026 you can leave the editor
2027 (or logout)
2028 to remedy the situation.
2029 After a preserve, you can use the recover command
2030 once the problem has been corrected,
2031 or the \fB\-r\fR option of the edit command
2032 if you leave the editor and want to return.
2034 If you make an undesirable change to the buffer
2035 and type a write command before discovering your mistake,
2036 the modified version will replace any previous version of the file.
2037 Should you ever lose a good version of a document in this way,
2038 do not panic and leave the editor.
2039 As long as you stay in the editor,
2040 the contents of the buffer remain accessible.
2041 Depending on the nature of the problem,
2042 it may be possible
2043 to restore the buffer to a more complete
2044 state with the undo command.
2045 After fixing the damaged buffer, you can again write the file
2046 to disk.
2048 Further reading and other information
2050 Edit is an editor designed for beginning and casual users.
2051 It is actually a version of a more powerful editor called
2052 .I ex.
2053 These lessons are intended to introduce you to the editor
2054 and its more commonly-used commands.
2055 We have not covered all of the editor's commands,
2056 but a selection of commands
2057 that should be sufficient to accomplish most of your editing tasks.
2058 You can find out more about the editor in the
2060 Ex Reference Manual,
2062 which is applicable to both
2063 .I ex
2065 .I edit.
2066 One way to become familiar with the manual is to begin by reading
2067 the description of commands that you already know.
2068 .bd I 3
2070 Using
2071 .I ex
2073 .bd I
2075 As you become more experienced with using the editor,
2076 you may still find that edit continues to meet your needs.
2077 However, should you become interested in using 
2078 .I ex,
2079 it is easy to switch.
2080 To begin an editing session with 
2081 .I ex,
2082 use the name
2083 .B ex
2084 in your command instead of
2085 .B edit.
2087 Edit commands also work in 
2088 .I ex,
2089 but the editing environment is somewhat different.
2090 You should be aware of a few differences
2091 between 
2092 .I ex
2093 and 
2094 .I edit.
2095 In edit, only the characters ``^'', ``$'', and ``\\'' have
2096 special meanings in searching the buffer
2097 or indicating characters to be changed by a substitute command.
2098 Several additional characters have special
2099 meanings in ex, as described in the
2101 Ex Reference Manual.
2103 Another feature of the edit environment prevents users from
2104 accidently entering two alternative modes of editing,
2105 .I open
2107 .I visual,
2108 in which
2109 the editor behaves quite differently from normal command mode.
2110 If you are using ex and you encounter strange behavior,
2111 you may have accidently entered open mode by typing ``o''.
2112 Type the \s-2ESC\s0 key and then a ``Q''
2113 to get out of open or visual mode and back into
2114 the regular editor command mode.
2115 The document
2117 An Introduction to Display Editing with Vi\|\|
2119 provide full details of visual mode.
2122 .ce 1
2123 \s+2Index\s0
2125 .sp 2
2128 addressing, \fIsee\fR line numbers
2129 ampersand, 20
2130 append mode, 6-7
2131 append (a) command, 6, 7, 9
2132 ``At end of file'' (message), 18
2133 backslash (\\), 21
2134 buffer, 3
2135 caret (^), 10, 20
2136 change (c) command, 18
2137 command mode, 5-6
2138 ``Command not found'' (message), 6
2139 context search, 10-12, 19-21
2140 control characters (``^'' notation), 10
2141 control-H, 7
2142 copy (co) command, 15
2143 corrections, 7, 16
2144 current filename, 21
2145 current line (\|.\|), 11, 17
2146 delete (d) command, 15-16
2147 dial-up, 5
2148 disk, 3
2149 documentation, 3, 23
2150 dollar ($), 10, 11, 17, 20-21
2151 dot (\f3\|.\|\f1) 11, 17
2152 edit (text editor), 3, 5, 23
2153 edit (e) command, 5, 9, 14
2154 editing commands:
2155 .in +.25i
2156 append (a), 6, 7, 9
2157 change (c), 18
2158 copy (co), 15
2159 delete (d), 15-16
2160 edit (text editor), 3, 5, 23
2161 edit (e), 5, 9, 14
2162 file (f), 21-22
2163 global (g), 19
2164 move (m), 14-15
2165 number (nu), 11
2166 preserve (pre), 22-23
2167 print (p), 10
2168 quit (q), 8, 13
2169 read (r), 22
2170 recover (rec), 22, 23
2171 substitute (s), 11-12, 19, 20
2172 undo (u), 16-17, 23
2173 write (w), 8, 13, 21, 22
2174 z, 12-13
2175 ! (shell escape), 21
2176 $=, 17
2177 +, 17
2178 \-, 17
2179 //, 12, 20
2180 ??, 20
2181 \&., 11, 17
2182 \&.=, 11, 17
2183 .in -.25i
2184 entering text, 3, 6-7
2185 erasing
2186 .in +.25i
2187 characters (^H), 7
2188 lines (@), 7
2189 .in -.25i
2190 error corrections, 7, 16
2191 ex (text editor), 23
2192 \fIEx Reference Manual\fR, 23
2193 exclamation (!), 21
2194 file, 3
2195 file (f) command, 21-22
2196 file recovery, 22-23
2197 filename, 3, 21
2198 global (g) command, 19
2199 input mode, 6-7
2200 Interrupt (message), 9
2201 line numbers, \fIsee also\fR current line
2202 .in +.25i
2203 dollar sign ($), 10, 11, 17
2204 dot (\|.\|), 11, 17
2205 relative (+ and \-), 17
2206 .in -.25i
2207 list, 10
2208 logging in, 4-6
2209 logging out, 8
2210 ``Login incorrect'' (message), 5
2211 minus (\-), 17
2212 move (m) command, 14-15
2213 ``Negative address\(emfirst buffer line is 1'' (message), 18
2214 ``No current filename'' (message), 8
2215 ``No such file or directory'' (message), 5, 6
2216 ``No write since last change'' (message), 21
2217 non-printing characters, 10
2218 ``Nonzero address required'' (message), 18
2219 ``Not an editor command'' (message), 6
2220 ``Not that many lines in buffer'' (message), 18
2221 number (nu) command, 11
2222 password, 5
2223 period (\|.\|), 11, 17
2224 plus (+), 17
2225 preserve (pre) command, 22-23
2226 print (p) command, 10
2227 program, 3
2228 prompts
2229 .in .25i
2230 % (\s-2UNIX\s0), 5
2231 : (edit), 5, 6, 7
2232 \0 (append), 7
2233 .in -.25i
2234 question (?), 20
2235 quit (q) command, 8, 13
2236 read (r) command, 22
2237 recover (rec) command, 22, 23
2238 recovery, \fIsee\fR\| file recovery
2239 references, 3, 23
2240 remove (rm) command, 21, 22
2241 reverse command effects (undo), 16-17, 23
2242 searching, 10-12, 19-21
2243 shell, 21
2244 shell escape (!), 21
2245 slash (/), 11-12, 20
2246 special characters (^, $, \\), 10, 11, 17, 20-21
2247 substitute (s) command, 11-12, 19, 20
2248 terminals, 4-5
2249 text input mode, 7
2250 undo (u) command, 16-17, 23
2251 \s-1UNIX\s0, 3
2252 write (w) command, 8, 13, 21, 22
2253 z command, 12-13