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