sam.tut.ms - sam - An updated version of the sam text editor.
 (HTM) git clone git://vernunftzentrum.de/sam.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       sam.tut.ms (40250B)
       ---
            1 .de P1
            2 .KS
            3 .DS
            4 .ft CW
            5 .ta 5n 10n 15n 20n 25n 30n 35n 40n 45n 50n 55n 60n 65n 70n 75n 80n
            6 ..
            7 .de P2
            8 .ft 1
            9 .DE
           10 .KE
           11 ..
           12 .de CW
           13 .lg 0
           14 \%\&\\$3\fB\\$1\fP\&\\$2
           15 .lg
           16 ..
           17 .de WC
           18 .lg 0
           19 \%\&\\$3\fI\\$1\fP\&\\$2
           20 .lg
           21 ..
           22 .TL
           23 A tutorial for the
           24 .CW sam
           25 .B
           26 command language
           27 .AU
           28 Rob Pike
           29 .AI
           30 .MH
           31 .AB
           32 .CW sam
           33 is an interactive text editor with a command language that makes heavy use
           34 of regular expressions.
           35 Although the language is syntactically similar to
           36 .CW ed (1),
           37 the details are interestingly different.
           38 This tutorial introduces the command language, but does not discuss
           39 the screen and mouse interface.
           40 With apologies to those unfamiliar with the Ninth Edition Blit software,
           41 it is assumed that the similarity of
           42 .CW sam
           43 to
           44 .CW mux (9)
           45 at this level makes
           46 .CW sam 's
           47 mouse language easy to learn.
           48 .PP
           49 The
           50 .CW sam
           51 command language applies identically to two environments:
           52 when running
           53 .CW sam
           54 on an ordinary terminal
           55 (\f2via\f1\f1
           56 .CW sam\ -d ),
           57 and in the command window of a
           58 .I downloaded
           59 .CW sam ,
           60 that is, one using the bitmap display and mouse.
           61 .AE
           62 .SH
           63 Introduction
           64 .PP
           65 This tutorial describes the command language of
           66 .CW sam ,
           67 an interactive text editor that runs on Blits and
           68 some computers with bitmap displays.
           69 For most editing tasks, the mouse-based editing features
           70 are sufficient, and they are easy to use and to learn.
           71 .PP
           72 The command language is often useful, however, particularly
           73 when making global changes.
           74 Unlike the commands in
           75 .CW ed ,
           76 which are necessary to make changes,
           77 .CW sam
           78 commands tend to be used
           79 only for complicated or repetitive editing tasks.
           80 It is in these more involved uses that
           81 the differences between
           82 .CW sam
           83 and other text editors are most evident.
           84 .PP
           85 .CW sam 's
           86 language makes it easy to do some things that other editors,
           87 including programs like
           88 .CW sed
           89 and
           90 .CW awk ,
           91 do not handle gracefully, so this tutorial serves partly as a
           92 lesson in
           93 .CW sam 's
           94 manner of manipulating text.
           95 The examples below therefore concentrate entirely on the language,
           96 assuming that facility with the use of the mouse in
           97 .CW sam
           98 is at worst easy to pick up.
           99 In fact,
          100 .CW sam
          101 can be run without the mouse at all (not
          102 .I downloaded ),
          103 by specifying the
          104 .CW -d
          105 flag, and it is this domain that the tutorial
          106 occupies; the command language in these modes
          107 are identical.
          108 .PP
          109 A word to the Unix adept:
          110 although
          111 .CW sam
          112 is syntactically very similar to
          113 .CW ed ,
          114 it is fundamentally and deliberately different in design and detailed semantics.
          115 You might use knowledge of
          116 .CW ed
          117 to predict how the substitute command works,
          118 but you'd only be right if you had used some understanding of
          119 .CW sam 's
          120 workings to influence your prediction.
          121 Be particularly careful about idioms.
          122 Idioms form in curious nooks of languages and depend on
          123 undependable peculiarities.
          124 .CW ed
          125 idioms simply don't work in
          126 .CW sam :
          127 .CW 1,$s/a/b/
          128 makes one substitution in the whole file, not one per line.
          129 .CW sam
          130 has its own idioms.
          131 Much of the purpose of this tutorial is to publish them
          132 and make fluency in
          133 .CW sam
          134 a matter of learning, not cunning.
          135 .PP
          136 The tutorial depends on familiarity with regular expressions, although
          137 some experience with a more traditional Unix editor may be helpful.
          138 To aid readers familiar with
          139 .CW ed ,
          140 I have pointed out in square brackets [] some of
          141 the relevant differences between
          142 .CW ed
          143 and
          144 .CW sam .
          145 Read these comments only if you wish
          146 to understand the differences; the lesson is about
          147 .CW sam ,
          148 not
          149 .CW sam
          150 .I vs.
          151 .CW ed .
          152 Another typographic convention is that output appears in
          153 .CW "this font,
          154 while typed input appears as
          155 .WC "slanty text.
          156 .PP
          157 Nomenclature:
          158 .CW sam
          159 keeps a copy of the text it is editing.
          160 This copy is called a
          161 .I file .
          162 To avoid confusion, I have called the permanent storage on disc a
          163 .I
          164 Unix file.
          165 .R
          166 .SH
          167 Text
          168 .PP
          169 To get started, we need some text to play with.
          170 Any text will do; try something from
          171 James Gosling's Emacs manual:
          172 .P1
          173 $ \fIsam -d
          174 a
          175 This manual is organized in a rather haphazard manner.  The first
          176 several sections were written hastily in an attempt to provide a
          177 general introduction to the commands in Emacs and to try to show
          178 the method in the madness that is the Emacs command structure.
          179 \&.
          180 .ft
          181 .P2
          182 .WC "sam -d
          183 starts
          184 .CW sam
          185 running.
          186 The
          187 .CW a
          188 command adds text until a line containing just a period, and sets the
          189 .I
          190 current text
          191 .R
          192 (also called
          193 .I dot )
          194 to what was typed \(em everything between the
          195 .CW a
          196 and the period.
          197 .CW ed "" [
          198 would leave dot set to only the last line.]
          199 The
          200 .CW p
          201 command prints the current text:
          202 .P1
          203 .WC p
          204 This manual is organized in a rather haphazard manner.  The first
          205 several sections were written hastily in an attempt to provide a
          206 general introduction to the commands in Emacs and to try to show
          207 the method in the madness that is the Emacs command structure.
          208 .P2
          209 [Again,
          210 .CW ed
          211 would print only the last line.]
          212 The
          213 .CW a
          214 command adds its text
          215 .I after
          216 dot; the
          217 .CW i
          218 command is like
          219 .CW a,
          220 but adds the text
          221 .I before
          222 dot.
          223 .P1
          224 .ft I
          225 i
          226 Introduction
          227 \&.
          228 p
          229 .ft
          230 Introduction
          231 .P2
          232 There is also a
          233 .CW c
          234 command that changes (replaces) the current text,
          235 and
          236 .CW d
          237 that deletes it; these are illustrated below.
          238 .PP
          239 To see all the text, we can specify what text to print;
          240 for the moment, suffice it to say that
          241 .WC 0,$
          242 specifies the entire file.
          243 .CW ed "" [
          244 users would probably type
          245 .WC 1,$ ,
          246 which in practice is the same thing, but see below.]
          247 .P1
          248 .WC 0,$p
          249 Introduction
          250 This manual is organized in a rather haphazard manner.  The first
          251 several sections were written hastily in an attempt to provide a
          252 general introduction to the commands in Emacs and to try to show
          253 the method in the madness that is the Emacs command structure.
          254 .P2
          255 Except for the
          256 .CW w
          257 command described below,
          258 .I all
          259 commands,
          260 including
          261 .CW p ,
          262 set dot to the text they touch.
          263 Thus,
          264 .CW a
          265 and
          266 .CW i
          267 set dot to the new text,
          268 .CW p
          269 to the text printed, and so on.
          270 Similarly, all commands
          271 (except
          272 .CW w )
          273 by default operate on the current
          274 text [unlike
          275 .CW ed ,
          276 for which some commands (such as
          277 .CW g )
          278 default to the entire file].
          279 .PP
          280 Things are not going to get very interesting until we can
          281 set dot arbitrarily.
          282 This is done by
          283 .I addresses ,
          284 which specify a piece of the file.
          285 The address
          286 .CW 1 ,
          287 for example, sets dot to the first line of the file.
          288 .P1
          289 .WC 1p
          290 Introduction
          291 .WC c
          292 .WC Preamble
          293 .WC .
          294 .P2
          295 The
          296 .CW c
          297 command didn't need to specify dot; the
          298 .CW p
          299 left it on line one.
          300 It's therefore easy to delete the first line utterly;
          301 the last command left dot set to line one:
          302 .P1
          303 .WC d
          304 .WC 1p
          305 This manual is organized in a rather haphazard manner.  The first
          306 .P2
          307 (Line numbers change
          308 to reflect changes to the file.)
          309 .PP
          310 The address \f(CW/\f2text\f(CW/\f1
          311 sets dot to the first appearance of
          312 .I text ,
          313 after dot.
          314 .CW ed "" [
          315 matches the first line containing
          316 .I text .]
          317 If
          318 .I text
          319 is not found, the search restarts at the beginning of the file
          320 and continues until dot.
          321 .P1
          322 .WC /Emacs/p
          323 Emacs
          324 .P2
          325 It's difficult to indicate typographically, but in this example no newline appears
          326 after
          327 .CW Emacs :
          328 the text to be printed is the string
          329 .CW Emacs ', `
          330 exactly.
          331 (The final
          332 .CW p
          333 may be left off \(em it is the default command.
          334 When downloaded, however, the default is instead to select the text,
          335 to highlight it,
          336 and to make it visible by moving the window on the file if necessary.
          337 Thus,
          338 .CW /Emacs/
          339 indicates on the display the next occurrence of the text.)
          340 .PP
          341 Imagine we wanted to change the word
          342 .CW haphazard
          343 to
          344 .CW thoughtless .
          345 Obviously, what's needed is another
          346 .CW c
          347 command, but the method used so far to insert text includes a newline.
          348 The syntax for including text without newlines is to surround the
          349 text with slashes (which is the same as the syntax for
          350 text searches, but what is going on should be clear from context).
          351 The text must appear immediately after the
          352 .CW c
          353 (or
          354 .CW a
          355 or
          356 .CW i ).
          357 Given this, it is easy to make the required change:
          358 .P1
          359 .WC /haphazard/c/thoughtless/
          360 1p
          361 This manual is organized in a rather thoughtless manner.  The first
          362 .P2
          363 [Changes can always be done with a
          364 .CW c
          365 command, even if the text is smaller than a line].
          366 You'll find that this way of providing text to commands is much
          367 more common than is the multiple-lines syntax.
          368 If you want to include a slash
          369 .CW /
          370 in the text, just precede it with a backslash
          371 .CW \e ,
          372 and use a backslash to protect a backslash itself.
          373 .P1
          374 .WC /Emacs/c/Emacs\e\e360/
          375 .WC 4p
          376 general introduction to the commands in Emacs\e360 and to try to show
          377 .P2
          378 We could also make this particular change by
          379 .P1
          380 .WC /Emacs/a/\e\e360/
          381 .P2
          382 .PP
          383 This is as good a place as any to introduce the
          384 .CW u
          385 command, which undoes the last command.
          386 A second
          387 .CW u
          388 will undo the penultimate command, and so on.
          389 .P1
          390 .WC u
          391 .WC 4p
          392 general introduction to the commands in Emacs and to try to show
          393 .WC u
          394 .WC 3p
          395 This manual is organized in a rather haphazard manner.  The first
          396 .P2
          397 Undoing can only back up; there is no way to undo a previous
          398 .CW u .
          399 .SH
          400 Addresses
          401 .PP
          402 We've seen the simplest forms of addresses, but there is more
          403 to learn before we can get too much further.
          404 An address selects a region in the file \(em a substring \(em
          405 and therefore must define the beginning and the end of a region.
          406 Thus, the address
          407 .CW 13
          408 selects from the beginning of line thirteen to the end of line thirteen, and
          409 .CW /Emacs/
          410 selects from the beginning of the word
          411 .CW Emacs ' `
          412 to the end.
          413 .PP
          414 Addresses may be combined with a comma:
          415 .P1
          416 13,15
          417 .P2
          418 selects lines thirteen through fifteen.  The definition of the comma
          419 operator is to select from the beginning of the left hand address (the
          420 beginning of line 13) to the end of the right hand address (the
          421 end of line 15).
          422 .PP
          423 A few special simple addresses come in handy:
          424 .CW .
          425 (a period) represents dot, the current text,
          426 .CW 0
          427 (line zero) selects the null string at the beginning of the file, and
          428 .CW $
          429 selects the null string at the end of the file
          430 [not the last line of the file].
          431 Therefore,
          432 .P1
          433 0,13
          434 .P2
          435 selects from the beginning of the file to the end of line thirteen,
          436 .P1
          437 \&.,$
          438 .P2
          439 selects from the beginning of the current text to the end of the file, and
          440 .P1
          441 0,$
          442 .P2
          443 selects the whole file [that is, a single string containing the whole file,
          444 not a list of all the lines in the file].
          445 .PP
          446 These are all
          447 .I absolute
          448 addresses: they refer to specific places in the file.
          449 .CW sam
          450 also has relative addresses, which depend
          451 on the value of dot,
          452 and in fact we have already seen one form:
          453 .CW /Emacs/
          454 finds the first occurrence of
          455 .CW Emacs
          456 searching forwards from dot.
          457 Which occurrence of
          458 .CW Emacs
          459 it finds depends on the value of dot.
          460 What if you wanted the first occurrence
          461 .CW before
          462 dot?  Just precede the pattern with a minus sign, which reverses the direction
          463 of the search:
          464 .P1
          465 -/Emacs/
          466 .P2
          467 In fact, the complete syntax for forward searching is
          468 .P1
          469 +/Emacs/
          470 .P2
          471 but the plus sign is the default, and in practice is rarely used.
          472 Here is an example that includes it for clarity:
          473 .P1
          474 0+/Emacs/
          475 .P2
          476 selects the first occurrence of
          477 .CW Emacs
          478 in the file; read it as ``go to line 0, then search forwards for
          479 .CW Emacs .''
          480 Since the
          481 .CW +
          482 is optional, this can be written
          483 .CW 0/Emacs/ .
          484 Similarly,
          485 .P1
          486 $-/Emacs/
          487 .P2
          488 finds the last occurrence in the file, so
          489 .P1
          490 0/Emacs/,$-/Emacs/
          491 .P2
          492 selects the text from the first to last
          493 .CW Emacs ,
          494 inclusive.
          495 Slightly more interesting:
          496 .P1
          497 /Emacs/+/Emacs/
          498 .P2
          499 (there is an implicit
          500 .CW .+
          501 at the beginning) selects the second
          502 .CW Emacs
          503 following dot.
          504 .PP
          505 Line numbers may also be relative.
          506 .P1
          507 -2
          508 .P2
          509 selects the second previous line, and
          510 .P1
          511 +5
          512 .P2
          513 selects the fifth following line (here the plus sign is obligatory).
          514 .PP
          515 Since addresses may select (and dot may be) more than one line,
          516 we need a definition of `previous' and `following:'
          517 `previous' means
          518 .I
          519 before the beginning
          520 .R
          521 of dot, and `following'
          522 means
          523 .I
          524 after the end
          525 .R
          526 of dot.
          527 For example, if the file contains \fBA\fIAA\fBA\f1,
          528 with dot set to the middle two
          529 .CW A 's
          530 (the slanting characters),
          531 .CW -/A/
          532 sets dot to the first
          533 .CW A ,
          534 and
          535 .CW +/A/
          536 sets dot to the last
          537 .CW A .
          538 Except under odd circumstances (such as when the only occurrence of the
          539 text in the file is already the current text), the text selected by a
          540 search will be disjoint from dot.
          541 .PP
          542 To select the
          543 .CW "troff -ms
          544 paragraph containing dot, however long it is, use
          545 .P1
          546 -/.PP/,/.PP/-1
          547 .P2
          548 which will include the
          549 .CW .PP
          550 that begins the paragraph, and exclude the one that ends it.
          551 .PP
          552 When typing relative line number addresses, the default number is
          553 .CW 1 ,
          554 so the above could be written slightly more simply:
          555 .P1
          556 -/.PP/,/.PP/-
          557 .P2
          558 .PP
          559 What does the address
          560 .CW +1-1
          561 or the equivalent
          562 .CW +-
          563 mean?  It looks like it does nothing, but recall that dot need not be a
          564 complete line of text.
          565 .CW +1
          566 selects the line after the end of the current text, and
          567 .CW -1
          568 selects the line before the beginning.  Therefore
          569 .CW +1-1
          570 selects the line before the line after the end of dot, that is,
          571 the complete line containing the end of dot.
          572 We can use this construction to expand a selection to include a complete line,
          573 say the first line in the file containing
          574 .CW Emacs :
          575 .P1
          576 .WC 0/Emacs/+-p
          577 general introduction to the commands in Emacs and to try to show
          578 .P2
          579 The address
          580 .CW +-
          581 is an idiom.
          582 .SH
          583 Loops
          584 .PP
          585 Above, we changed one occurrence of
          586 .CW Emacs
          587 to
          588 .CW Emacs\e360 ,
          589 but if the name of the editor is really changing, it would be useful
          590 to change
          591 .I all
          592 instances of the name in a single command.
          593 .CW sam
          594 provides a command,
          595 .CW x
          596 (extract), for just that job.
          597 The syntax is
          598 \f(CWx/\f2pattern\f(CW/\f2command\f1.
          599 For each occurrence of the pattern in the selected text,
          600 .CW x
          601 sets dot to the occurrence and runs command.
          602 For example, to change
          603 .CW Emacs
          604 to
          605 .CW vi,
          606 .P1
          607 .WC 0,$x/Emacs/c/vi/
          608 .WC 0,$p
          609 This manual is organized in a rather haphazard manner.  The first
          610 several sections were written hastily in an attempt to provide a
          611 general introduction to the commands in vi and to try to show
          612 the method in the madness that is the vi command structure.
          613 .P2
          614 This
          615 works by subdividing the current text
          616 .CW 0,$ "" (
          617 \(em the whole file) into appearances of its textual argument
          618 .CW Emacs ), (
          619 and then running the command that follows
          620 .CW c/vi/ ) (
          621 with dot set to the text.
          622 We can read this example as, ``find all occurrences of
          623 .CW Emacs
          624 in the file, and for each one,
          625 set the current text to the occurrence and run the command
          626 .CW c/vi/ ,
          627 which will replace the current text by
          628 .CW vi. ''
          629 [This command is somewhat similar to
          630 .CW ed 's
          631 .CW g
          632 command.  The differences will develop below, but note that the
          633 default address, as always, is dot rather than the whole file.]
          634 .PP
          635 A single
          636 .CW u
          637 command is sufficient to undo an
          638 .CW x
          639 command, regardless of how many individual changes the
          640 .CW x
          641 makes.
          642 .P1
          643 .WC u
          644 .WC 0,$p
          645 This manual is organized in a rather haphazard manner.  The first
          646 several sections were written hastily in an attempt to provide a
          647 general introduction to the commands in Emacs and to try to show
          648 the method in the madness that is the Emacs command structure.
          649 .P2
          650 .PP
          651 Of course,
          652 .CW c
          653 is not the only command
          654 .CW x
          655 can run.  An
          656 .CW a
          657 command can be used to put proprietary markings on
          658 .CW Emacs :
          659 .P1
          660 .WC 0,$x/Emacs/a/{TM}/
          661 .WC /Emacs/+-p
          662 general introduction to the commands in Emacs{TM} and to try to show
          663 .P2
          664 [There is no way to see the changes as they happen, as in
          665 .CW ed 's
          666 .CW g/Emacs/s//&{TM}/p ;
          667 see the section on Multiple Changes, below.]
          668 .PP
          669 The
          670 .CW p
          671 command is also useful when driven by an
          672 .CW x ,
          673 but be careful that you say what you mean;
          674 .P1
          675 .WC 0,$x/Emacs/p
          676 EmacsEmacs
          677 .P2
          678 since
          679 .CW x
          680 sets dot to the text in the slashes, printing only that text
          681 is not going to be very
          682 informative.  But the command that
          683 .CW x
          684 runs can contain addresses.  For example, if we want to print all
          685 lines containing
          686 .CW Emacs ,
          687 just use
          688 .CW +- :
          689 .P1
          690 .WC 0,$x/Emacs/+-p
          691 general introduction to the commands in Emacs{TM} and to try to show
          692 the method in the madness that is the Emacs{TM} command structure.
          693 .P2
          694 Finally, let's restore the state of the file with another
          695 .CW x
          696 command, and make use of a handy shorthand:
          697 a comma in an address has its left side default to
          698 .CW 0 ,
          699 and its right side default to
          700 .CW $ ,
          701 so the easy-to-type address
          702 .CW ,
          703 refers to the whole file:
          704 .P1
          705 .WC ",x/Emacs/ /{TM}/d
          706 .WC ,p
          707 This manual is organized in a rather haphazard manner.  The first
          708 several sections were written hastily in an attempt to provide a
          709 general introduction to the commands in Emacs and to try to show
          710 the method in the madness that is the Emacs command structure.
          711 .P2
          712 Notice what this
          713 .CW x
          714 does: for each occurrence of Emacs,
          715 find the
          716 .CW {TM}
          717 that follows, and delete it.
          718 .PP
          719 The `text'
          720 .CW sam
          721 accepts
          722 for searches in addresses and in
          723 .CW x
          724 commands is not simple text, but rather
          725 .I regular\ expressions.
          726 Unix has several distinct interpretations of regular expressions.
          727 The form used by
          728 .CW sam
          729 is that of
          730 .CW regexp (6),
          731 including parentheses
          732 .CW ()
          733 for grouping and an `or' operator
          734 .CW |
          735 for matching strings in parallel.
          736 .CW sam
          737 also matches the character sequence
          738 .CW \en
          739 with a newline character.
          740 Replacement text, such as used in the
          741 .CW a
          742 and
          743 .CW c
          744 commands, is still plain text, but the sequence
          745 .CW \en
          746 represents newline in that context, too.
          747 .PP
          748 Here is an example.  Say we wanted to double space the document, that is,
          749 turn every newline into two newlines.
          750 The following all do the job:
          751 .P1
          752 .WC ",x/\en/ a/\en/
          753 .WC ",x/\en/ c/\en\en/
          754 .WC ",x/$/ a/\en/
          755 .WC ",x/^/ i/\en/
          756 .P2
          757 The last example is slightly different, because it puts a newline
          758 .I before
          759 each line; the other examples place it after.
          760 The first two examples manipulate newlines directly
          761 [something outside
          762 .CW ed 's
          763 ken]; the last two
          764 use regular expressions:
          765 .CW $
          766 is the empty string at the end of a line, while
          767 .CW ^
          768 is the empty string at the beginning.
          769 .PP
          770 These solutions all have a possible drawback: if there is already a blank line
          771 (that is, two consecutive newlines), they make it much larger (four
          772 consecutive newlines).
          773 A better method is to extend every group of newlines by one:
          774 .P1
          775 .WC ",x/\en+/ a/\en/
          776 .P2
          777 The regular expression operator
          778 .CW +
          779 means `one or more;'
          780 .CW \en+
          781 is identical to
          782 .CW \en\en* .
          783 Thus, this example
          784 takes every sequence of newlines and adds another
          785 to the end.
          786 .PP
          787 A more common example is indenting a block of text by a tab stop.
          788 The following all work,
          789 although the first is arguably the cleanest (the blank text in slashes is a tab):
          790 .P1
          791 .WC ",x/^/a/         /
          792 .WC ",x/^/c/         /
          793 .WC ",x/.*\en/i/         /
          794 .P2
          795 The last example uses the pattern (idiom, really)
          796 .CW .*\en
          797 to match lines:
          798 .CW .*
          799 matches the longest possible string of non-newline characters.
          800 Taking initial tabs away is just as easy:
          801 .P1
          802 .WC ",x/^    /d
          803 .P2
          804 In these examples I have specified an address (the whole file), but
          805 in practice commands like these are more likely to be run without
          806 an address, using the value of dot set by selecting text with the mouse.
          807 .SH
          808 Conditionals
          809 .PP
          810 The
          811 .CW x
          812 command is a looping construct:
          813 for each match of a regular expression,
          814 it extracts (sets dot to) the match and runs a command.
          815 .CW sam
          816 also has a conditional,
          817 .CW g :
          818 \f(CWg/\f2pattern\f(CW/\f2command\f1
          819 runs the command if dot contains a match of the pattern
          820 .I
          821 without changing the value of dot.
          822 .R
          823 The inverse,
          824 .CW v ,
          825 runs the command if dot does
          826 .I not
          827 contain a match of the pattern.
          828 (The letters
          829 .CW g
          830 and
          831 .CW v
          832 are historical and have no mnemonic significance.  You might
          833 think of
          834 .CW g
          835 as `guard.')
          836 .CW ed "" [
          837 users should read the above definitions very carefully; the
          838 .CW g
          839 command in
          840 .CW sam
          841 is fundamentally different from that in
          842 .CW ed .]
          843 Here is an example of the difference between
          844 .CW x
          845 and
          846 .CW g:
          847 .P1
          848 ,x/Emacs/c/vi/
          849 .P2
          850 changes each occurrence of the word
          851 .CW Emacs
          852 in the file to the word
          853 .CW vi ,
          854 but
          855 .P1
          856 ,g/Emacs/c/vi/
          857 .P2
          858 changes the
          859 .I "whole file
          860 to
          861 .CW vi
          862 if there is the word
          863 .CW Emacs
          864 anywhere in the file.
          865 .PP
          866 Neither of these commands is particularly interesting in isolation,
          867 but they are valuable when combined with
          868 .CW x
          869 and with themselves.
          870 .SH
          871 Composition
          872 .PP
          873 One way to think about the
          874 .CW x
          875 command is that, given a selection (a value of dot)
          876 it iterates through interesting subselections (values of dot within).
          877 In other words, it takes a piece of text and cuts it into smaller pieces.
          878 But the text that it cuts up may already be a piece cut by a previous
          879 .CW x
          880 command or selected by a
          881 .CW g .
          882 .CW sam 's
          883 most interesting property is the ability to define a sequence of commands
          884 to perform a particular task.\(dg
          885 .FS
          886 \(dg
          887 The obvious analogy with shell pipelines is only partially valid,
          888 because the individual
          889 .CW sam
          890 commands are all working on the same text; it is only how the text is
          891 sliced up that is changing.
          892 .FE
          893 A simple example is to change all occurrences of
          894 .CW Emacs
          895 to
          896 .CW emacs ;
          897 certainly the command
          898 .P1
          899 .WC ",x/Emacs/ c/emacs/
          900 .P2
          901 will work, but we can use an
          902 .CW x
          903 command to save retyping most of the word
          904 .CW Emacs :
          905 .P1
          906 .WC ",x/Emacs/ x/E/ c/e/
          907 .P2
          908 (Blanks can be used
          909 to separate commands on a line to make them easier to read.)
          910 What this command does is find all occurrences of
          911 .CW Emacs
          912 .CW ,x/Emacs/ ), (
          913 and then
          914 .I
          915 with dot set to that text,
          916 .R
          917 find all occurrences of the letter
          918 .CW E
          919 .CW x/E/ ), (
          920 and then
          921 .I
          922 with dot set to that text,
          923 .R
          924 run the command
          925 .CW c/e/
          926 to change the character to lower case.
          927 Note that the address for the command \(em the whole file, specified by a comma
          928 \(em is only given to the leftmost
          929 piece of the command; the rest of the pieces have dot set for them by
          930 the execution of the pieces to their left.
          931 .PP
          932 As another simple example, consider a problem
          933 solved above: printing all lines in the file containing the word
          934 .CW Emacs:
          935 .P1
          936 .WC ",x/.*\en/ g/Emacs/p
          937 general introduction to the commands in Emacs and to try to show
          938 the method in the madness that is the Emacs command structure.
          939 .P2
          940 This command says to break the file into lines
          941 .CW ,x/.*\en/ ), (
          942 and for each line that contains the string
          943 .CW Emacs
          944 .CW g/Emacs/ ), (
          945 run the command
          946 .CW p
          947 with dot set to the line (not the match of
          948 .CW Emacs ),
          949 which prints the line.
          950 To save typing, because
          951 .CW .*\en
          952 is a common pattern in
          953 .CW x
          954 commands,
          955 if the
          956 .CW x
          957 is followed immediately by a space, the pattern
          958 .CW .*\en
          959 is assumed.
          960 Therefore, the above could be written more succinctly:
          961 .P1
          962 .WC ",x g/Emacs/p
          963 .P2
          964 The solution we used before was
          965 .P1
          966 .WC ,x/Emacs/+-p
          967 .P2
          968 which runs the command
          969 .CW +-p
          970 with dot set to each match of
          971 .CW Emacs
          972 in the file (recall that the idiom
          973 .CW +-p
          974 prints the line containing the end of dot).
          975 .PP
          976 The two commands usually produce the same result
          977 (the
          978 .CW +-p
          979 form will print a line twice if it contains
          980 .CW Emacs
          981 twice).  Which is better?
          982 .CW ,x/Emacs/+-p
          983 is easier to type and will be much faster if the file is large and
          984 there are few occurrences of the string, but it is really an odd special case.
          985 .CW ",x/.*\en/ g/Emacs/p
          986 is slower \(em it breaks each line out separately, then examines
          987 it for a match \(em but is conceptually cleaner, and generalizes more easily.
          988 For example, consider the following piece of the Emacs manual:
          989 .P1
          990 command name="append-to-file", key="[unbound]"
          991 Takes the contents of the current buffer and appends it to the
          992 named file. If the file doesn't exist, it will be created.
          993 
          994 command name="apropos", key="ESC-?"
          995 Prompts for a keyword and then prints a list of those commands
          996 whose short description contains that keyword.  For example,
          997 if you forget which commands deal with windows, just type
          998 "@b[ESC-?]@t[window]@b[ESC]".
          999 
         1000 \&\f2and so on\f(CW
         1001 .P2
         1002 This text consists of groups of non-empty lines, with a simple format
         1003 for the text within each group.
         1004 Imagine that we wanted to find the description of the `apropos'
         1005 command.
         1006 The problem is to break the file into individual descriptions,
         1007 and then to find the description of `apropos' and to print it.
         1008 The solution is straightforward:
         1009 .P1
         1010 .WC ,x/(.+\en)+/\ g/command\ name="apropos"/p
         1011 command name="apropos", key="ESC-?"
         1012 Prompts for a keyword and then prints a list of those commands
         1013 whose short description contains that keyword.  For example,
         1014 if you forget which commands deal with windows, just type
         1015 "@b[ESC-?]@t[window]@b[ESC]".
         1016 .P2
         1017 The regular expression
         1018 .CW (.+\en)+
         1019 matches one or more lines with one or more characters each, that is,
         1020 the text between blank lines, so
         1021 .CW ,x/(.+\en)+/
         1022 extracts each description; then
         1023 .CW g/command\ name="apropos"/
         1024 selects the description for `apropos' and
         1025 .CW p
         1026 prints it.
         1027 .PP
         1028 Imagine that we had a C program containing the variable
         1029 .CW n ,
         1030 but we wanted to change it to
         1031 .CW num .
         1032 This command is a first cut:
         1033 .P1
         1034 .WC ",x/n/ c/num/
         1035 .P2
         1036 but is obviously flawed: it will change all
         1037 .CW n 's
         1038 in the file, not just the
         1039 .I identifier
         1040 .CW n .
         1041 A better solution is to use an
         1042 .CW x
         1043 command to extract the identifiers, and then use
         1044 .CW g
         1045 to find the
         1046 .CW n 's:
         1047 .P1
         1048 .WC ",x/[a-zA-Z_][a-zA-Z_0-9]*/ g/n/ v/../ c/num/
         1049 .P2
         1050 It looks awful, but it's fairly easy to understand when read
         1051 left to right.
         1052 A C identifier is an alphabetic or underscore followed by zero or more
         1053 alphanumerics or underscores, that is, matches of the regular expression
         1054 .CW [a-zA-Z_][a-zA-Z_0-9]* .
         1055 The
         1056 .CW g
         1057 command selects those identifiers containing
         1058 .CW n ,
         1059 and the
         1060 .CW v
         1061 is a trick: it rejects those identifiers containing more than one
         1062 character.  Hence the
         1063 .CW c/num/
         1064 applies only to free-standing
         1065 .CW n 's.
         1066 .PP
         1067 There is still a problem here:
         1068 we don't want to change
         1069 .CW n 's
         1070 that are part of the character constant
         1071 .CW \en .
         1072 There is a command
         1073 .CW y ,
         1074 complementary to
         1075 .CW x ,
         1076 that is just what we need:
         1077 \f(CWy/\f2pattern\f(CW/\f2command\f1
         1078 runs the command on the pieces of text
         1079 .I between
         1080 matches of the pattern;
         1081 if
         1082 .CW x
         1083 selects,
         1084 .CW y
         1085 rejects.
         1086 Here is the final command:
         1087 .P1
         1088 .WC ",y/\e\en/ x/[a-zA-Z_][a-zA-Z_0-9]*/ g/n/ v/../ c/num/
         1089 .P2
         1090 The
         1091 .CW y/\e\en/
         1092 (with backslash doubled to make it a literal character)
         1093 removes the two-character sequence
         1094 .CW \en
         1095 from consideration, so the rest of the command will not touch it.
         1096 There is more we could do here; for example, another
         1097 .CW y
         1098 could be prefixed to protect comments in the code.
         1099 I won't elaborate the example any further, but you should have
         1100 an idea of the way in which the looping and conditional commands
         1101 in
         1102 .CW sam
         1103 may be composed to do interesting things.
         1104 .SH
         1105 Grouping
         1106 .PP
         1107 There is another way to arrange commands.
         1108 By enclosing them in brace brackets
         1109 .CW {} ,
         1110 commands may be applied in parallel.
         1111 This example uses the
         1112 .CW =
         1113 command, which reports the line and character numbers of dot,
         1114 together with
         1115 .CW p ,
         1116 to report on appearances of
         1117 .CW Emacs
         1118 in our original file:
         1119 .P1
         1120 .WC ,p
         1121 This manual is organized in a rather haphazard manner.  The first
         1122 several sections were written hastily in an attempt to provide a
         1123 general introduction to the commands in Emacs and to try to show
         1124 the method in the madness that is the Emacs command structure.
         1125 .ft I
         1126 ,x/Emacs/{
         1127         =
         1128         +-p
         1129 }
         1130 .ft
         1131 3; #171,#176
         1132 general introduction to the commands in Emacs and to try to show
         1133 4; #234,#239
         1134 the method in the madness that is the Emacs command structure.
         1135 .P2
         1136 (The number before the semicolon is the line number;
         1137 the numbers beginning with
         1138 .CW #
         1139 are character numbers.)
         1140 As a more interesting example, consider changing all occurrences of
         1141 .CW Emacs
         1142 to
         1143 .CW vi
         1144 and vice versa.  We can type
         1145 .P1
         1146 .ft I
         1147 ,x/Emacs|vi/{
         1148         g/Emacs/ c/vi/
         1149         g/vi/ c/Emacs/
         1150 }
         1151 .ft
         1152 .P2
         1153 or even
         1154 .P1
         1155 .ft I
         1156 ,x/[a-zA-Z]+/{
         1157         g/Emacs/ v/....../ c/vi/
         1158         g/vi/ v/.../ c/Emacs/
         1159 }
         1160 .ft
         1161 .P2
         1162 to make sure we don't change strings embedded in words.
         1163 .SH
         1164 Multiple Changes
         1165 .PP
         1166 You might wonder why, once
         1167 .CW Emacs
         1168 has been changed to
         1169 .CW vi
         1170 in the above example,
         1171 the second command in the braces doesn't put it back again.
         1172 The reason is that the commands are run in parallel:
         1173 within any top-level
         1174 .CW sam
         1175 command, all changes to the file refer to the state of the file
         1176 before any of the changes in that command are made.
         1177 After all the changes have been determined, they are all applied
         1178 simultaneously.
         1179 .PP
         1180 This means, as mentioned, that commands within a compound
         1181 command see the state of the file before any of the changes apply.
         1182 This method of evaluation makes some things easier (such as the exchange of
         1183 .CW Emacs
         1184 and
         1185 .CW vi ),
         1186 and some things harder.
         1187 For instance, it is impossible to use a
         1188 .CW p
         1189 command to print the changes as they happen,
         1190 because they haven't happened when the
         1191 .CW p
         1192 is executed.
         1193 An indirect ramification is that changes must occur in forward
         1194 order through the file,
         1195 and must not overlap.
         1196 .SH
         1197 Unix
         1198 .PP
         1199 .CW sam
         1200 has a few commands to connect to Unix processes.
         1201 The simplest is
         1202 .CW ! ,
         1203 which runs the command with input and output connected to the terminal.
         1204 .P1
         1205 .WC !date
         1206 Wed May 28 23:25:21 EDT 1986
         1207 !
         1208 .P2
         1209 (When downloaded, the input is connected to
         1210 .CW /dev/null
         1211 and only the first few lines of output are printed;
         1212 any overflow is stored in
         1213 .CW $HOME/sam.err .)
         1214 The final
         1215 .CW !
         1216 is a prompt to indicate when the command completes.
         1217 .PP
         1218 Slightly more interesting is
         1219 .CW > ,
         1220 which provides the current text as standard input to the Unix command:
         1221 .P1
         1222 .WC "1,2 >wc
         1223       2       22      131
         1224 !
         1225 .P2
         1226 The complement of
         1227 .CW >
         1228 is, naturally,
         1229 .CW < :
         1230 it replaces the current text with the standard output of the Unix command:
         1231 .P1
         1232 .WC "1 <date
         1233 !
         1234 .WC 1p
         1235 Wed May 28 23:26:44 EDT 1986
         1236 .P2
         1237 The last command is
         1238 .CW | ,
         1239 which is a combination of
         1240 .CW <
         1241 and
         1242 .CW > :
         1243 the current text is provided as standard input to the Unix command,
         1244 and the Unix command's standard output is collected and used to
         1245 replace the original text.
         1246 For example,
         1247 .P1
         1248 .WC ",| sort
         1249 .P2
         1250 runs
         1251 .CW sort (1)
         1252 on the file, sorting the lines of the text lexicographically.
         1253 Note that
         1254 .CW < ,
         1255 .CW >
         1256 and
         1257 .CW |
         1258 are
         1259 .CW sam
         1260 commands, not Unix shell operators.
         1261 .PP
         1262 The next example converts all appearances of
         1263 .CW Emacs
         1264 to upper case using
         1265 .CW tr (1):
         1266 .P1
         1267 .WC ",x/Emacs/ | tr a-z A-Z
         1268 .P2
         1269 .CW tr
         1270 is run once for each occurrence of
         1271 .CW Emacs .
         1272 Of course, you could do this example more efficiently with a simple
         1273 .CW c
         1274 command, but here's a trickier one:
         1275 given a Unix mail box as input,
         1276 convert all the
         1277 .CW Subject
         1278 headers to distinct fortunes:
         1279 .P1
         1280 .WC ",x/^Subject:.*\en/ x/[^:]*\en/ < /usr/games/fortune
         1281 .P2
         1282 (The regular expression
         1283 .CW [^:]
         1284 refers to any character
         1285 .I except
         1286 .CW :
         1287 and newline; the negation operator
         1288 .CW ^
         1289 excludes newline from the list of characters.) 
         1290 Again,
         1291 .CW /usr/games/fortune
         1292 is run once for each
         1293 .CW Subject
         1294 line, so each
         1295 .CW Subject
         1296 line is changed to a different fortune.
         1297 .SH
         1298 A few other text commands
         1299 .PP
         1300 For completeness, I should mention three other commands that
         1301 manipulate text.  The
         1302 .CW m
         1303 command moves the current text to after the text specified by the
         1304 (obligatory) address after the command.
         1305 Thus
         1306 .P1
         1307 .WC "/Emacs/+- m 0
         1308 .P2
         1309 moves the next line containing
         1310 .CW Emacs
         1311 to the beginning of the file.
         1312 Similarly,
         1313 .CW t
         1314 (another historic character) copies the text:
         1315 .P1
         1316 .WC "/Emacs/+- t 0
         1317 .P2
         1318 would make, at the beginning of the file, a copy of the next line
         1319 containing
         1320 .CW Emacs .
         1321 .PP
         1322 The third command is more interesting: it makes substitutions.
         1323 Its syntax is
         1324 \f(CWs/\f2pattern\f(CW/\f2replacement\f(CW/\f1.
         1325 Within the current text, it finds the first occurrence of
         1326 the pattern and replaces it by the replacement text,
         1327 leaving dot set to the entire address of the substitution.
         1328 .P1
         1329 .WC 1p
         1330 This manual is organized in a rather haphazard manner.  The first
         1331 .WC s/haphazard/thoughtless/
         1332 .WC p
         1333 This manual is organized in a rather thoughtless manner.  The first
         1334 .P2
         1335 Occurrences of the character
         1336 .CW &
         1337 in the replacement text stand for the text matching the pattern.
         1338 .P1
         1339 .WC s/T/"&&&&"/
         1340 .WC p
         1341 "TTTT"his manual is organized in a rather thoughtless manner.  The first
         1342 .P2
         1343 There are two variants.  The first is that a number may be specified
         1344 after the
         1345 .CW s ,
         1346 to indicate which occurrence of the pattern to substitute; the default
         1347 is the first.
         1348 .P1
         1349 .WC s2/is/was/
         1350 .WC p
         1351 "TTTT"his manual was organized in a rather thoughtless manner.  The first
         1352 .P2
         1353 The second is that suffixing a
         1354 .CW g
         1355 (global) causes replacement of all occurrences, not just the first.
         1356 .P1
         1357 .WC s/[a-zA-Z]/x/g
         1358 .WC p
         1359 "xxxx"xxx xxxxxx xxx xxxxxxxxx xx x xxxxxx xxxxxxxxxxx xxxxxxx  xxx xxxxx
         1360 .P2
         1361 Notice that in all these examples
         1362 dot is left
         1363 set to the entire line.
         1364 .PP
         1365 [The substitute command is vital to
         1366 .CW ed,
         1367 because it is the only way to make changes within a line.
         1368 It is less valuable in
         1369 .CW sam ,
         1370 in which the concept of a line is much less important.
         1371 For example, many
         1372 .CW ed
         1373 substitution idioms are handled well by
         1374 .CW sam 's
         1375 basic commands. Consider the commands
         1376 .P1
         1377 s/good/bad/
         1378 s/good//
         1379 s/good/& bye/
         1380 .P2
         1381 which are equivalent in
         1382 .CW sam
         1383 to
         1384 .P1
         1385 /good/c/bad/
         1386 /good/d
         1387 /good/a/ bye/
         1388 .P2
         1389 and for which the context search is likely unnecessary because the desired
         1390 text is already dot.
         1391 Also, beware this
         1392 .CW ed
         1393 idiom:
         1394 .P1
         1395 1,$s/good/bad/
         1396 .P2
         1397 which changes the first
         1398 .CW good
         1399 on each line; the same command in
         1400 .CW sam
         1401 will only change the first one in the whole file.
         1402 The correct
         1403 .CW sam
         1404 version is
         1405 .P1
         1406 ,x s/good/bad/
         1407 .P2
         1408 but what is more likely meant is
         1409 .P1
         1410 ,x/good/ c/bad/
         1411 .P2
         1412 .CW sam
         1413 operates under different rules.]
         1414 .SH
         1415 Files
         1416 .PP
         1417 So far, we have only been working with a single file,
         1418 but
         1419 .CW sam
         1420 is a multi-file editor.
         1421 Only one file may be edited at a time, but
         1422 it is easy to change which file is the `current' file for editing.
         1423 To see how to do this, we need a
         1424 .CW sam
         1425 with a few files;
         1426 the easiest way to do this is to start it
         1427 with a list of Unix file names to edit.
         1428 .P1
         1429 $ \fIecho *.ms\f(CW
         1430 conquest.ms death.ms emacs.ms famine.ms slaughter.ms
         1431 $ \fIsam -d *.ms\f(CW
         1432  -. conquest.ms
         1433 .P2
         1434 (I'm sorry the Horsemen don't appear in liturgical order.)
         1435 The line printed by
         1436 .CW sam
         1437 is an indication that the Unix file
         1438 .CW conquest.ms
         1439 has been read, and is now the current file.
         1440 .CW sam
         1441 does not read the Unix file until
         1442 the associated
         1443 .CW sam
         1444 file becomes current.
         1445 .PP
         1446 The
         1447 .CW n
         1448 command prints the names of all the files:
         1449 .P1
         1450 .WC n
         1451  -. conquest.ms
         1452  -  death.ms
         1453  -  emacs.ms
         1454  -  famine.ms
         1455  -  slaughter.ms
         1456 .P2
         1457 This list is also available in the menu on mouse button 3.
         1458 The command
         1459 .CW f
         1460 tells the name of just the current file:
         1461 .P1
         1462 .WC f
         1463  -. conquest.ms
         1464 .P2
         1465 The characters to the left of the file name encode helpful information about
         1466 the file.
         1467 The minus sign becomes a plus sign if the file has a window open, and an
         1468 asterisk if more than one is open.
         1469 The period (another meaning of dot) identifies the current file.
         1470 The leading blank changes to an apostrophe if the file is different
         1471 from the contents of the associated Unix file, as far as
         1472 .CW sam
         1473 knows.
         1474 This becomes evident if we make a change.
         1475 .P1
         1476 .WC 1d
         1477 .WC f
         1478 \&'-. conquest.ms
         1479 .P2
         1480 If the file is restored by an undo command, the apostrophe disappears.
         1481 .P1
         1482 .WC u
         1483 .WC f
         1484  -. conquest.ms
         1485 .P2
         1486 The file name may be changed by providing a new name with the
         1487 .CW f
         1488 command:
         1489 .P1
         1490 .CW "f pestilence.ms
         1491 \&'-. pestilence.ms
         1492 .P2
         1493 .WC f
         1494 prints the new status of the file,
         1495 that is, it changes the name if one is provided, and prints the
         1496 name regardless.
         1497 A file name change may also be undone.
         1498 .P1
         1499 .WC u
         1500 .WC f
         1501  -. conquest.ms
         1502 .P2
         1503 .PP
         1504 When
         1505 .CW sam
         1506 is downloaded, the current file may be changed simply by selecting
         1507 the desired file from the menu (selecting the same file subsequently
         1508 cycles through the windows opened on the file).
         1509 Otherwise, the
         1510 .CW b
         1511 command can be used to choose the desired file:\(dg
         1512 .FS
         1513 \(dg A bug prevents the
         1514 .CW b
         1515 command from working when downloaded.
         1516 Because the menu is more convenient anyway, and
         1517 because the method
         1518 of choosing files from the command language is slated to change,
         1519 the bug hasn't been fixed.
         1520 .FE
         1521 .P1
         1522 .WC "b emacs.ms
         1523  -. emacs.ms
         1524 .P2
         1525 Again,
         1526 .CW sam
         1527 prints the name (actually, executes an implicit
         1528 .CW f
         1529 command) because the Unix file
         1530 .CW emacs.ms
         1531 is being read for the first time.
         1532 It is an error to ask for a file
         1533 .CW sam
         1534 doesn't know about, but the
         1535 .CW B
         1536 command will prime
         1537 .CW sam 's
         1538 menu with a new file, and make it current.
         1539 .P1
         1540 .WC "b flood.pic
         1541 ?no such file `flood.pic'
         1542 .WC "B flood.pic
         1543  -. flood.pic
         1544 .WC n
         1545  -  conquest.ms
         1546  -  death.ms
         1547  -  emacs.ms
         1548  -  famine.ms
         1549  -. flood.pic
         1550  -  slaughter.ms
         1551 .P2
         1552 Both
         1553 .CW b
         1554 and
         1555 .CW B
         1556 will accept a list of file names.
         1557 .CW b
         1558 simply takes the first file in the list, but
         1559 .CW B
         1560 loads them all.
         1561 The list may be typed on one line \(em
         1562 .P1
         1563 .WC "B devil.tex satan.tex 666.tex emacs.tex
         1564 .P2
         1565 \(em or generated by a Unix command \(em
         1566 .P1
         1567 .WC "B <echo *.tex
         1568 .P2
         1569 The latter form requires a Unix command;
         1570 .CW sam
         1571 does not understand the shell file name metacharacters, so
         1572 .CW "B *.tex
         1573 attempts to load a single file named
         1574 .CW *.tex .
         1575 (The
         1576 .CW <
         1577 form is of course derived from
         1578 .CW sam 's
         1579 .CW <
         1580 command.)
         1581 .CW echo
         1582 is not the only useful command to run subservient to
         1583 .CW B ;
         1584 for example,
         1585 .P1
         1586 .WC "B <grep -l Emacs *
         1587 .P2
         1588 will load only those files containing the string
         1589 .CW Emacs .
         1590 Finally, a special case: a
         1591 .CW B
         1592 with no arguments creates an empty, nameless file within
         1593 .CW sam .
         1594 .PP
         1595 The complement of
         1596 .CW B
         1597 is
         1598 .CW D :
         1599 .P1
         1600 .WC "D devil.tex satan.tex 666.tex emacs.tex
         1601 .P2
         1602 eradicates the files from
         1603 .CW sam 's
         1604 memory (not from the Unix machine's disc).
         1605 .CW D
         1606 without any file names removes the current file from
         1607 .CW sam .
         1608 .PP
         1609 There are three other commands that relate the current file
         1610 to Unix files.
         1611 The
         1612 .CW w
         1613 command writes the file to disc;
         1614 without arguments, it writes the entire file to the Unix file associated
         1615 with the current file in
         1616 .CW sam
         1617 (it is the only command whose default address is not dot).
         1618 Of course, you can specify an address to be written,
         1619 and a different file name, with the obvious syntax:
         1620 .P1
         1621 .WC "1,2w /tmp/revelations
         1622 /tmp/revelations: #44
         1623 .P2
         1624 .CW sam
         1625 responds with the file name and the number of characters written to the file.
         1626 The
         1627 .CW write
         1628 command on the button 3 menu is identical in function to an unadorned
         1629 .CW w
         1630 command.
         1631 .PP
         1632 The other two commands,
         1633 .CW e
         1634 and
         1635 .CW r ,
         1636 read data from Unix files.
         1637 The
         1638 .CW e
         1639 command clears out the current file,
         1640 reads the data from the named file (or uses the current file's old name if
         1641 none is explicitly provided), and sets the file name.
         1642 It's much like a
         1643 .CW B
         1644 command, but puts the information in the current file instead of a new one.
         1645 .CW e
         1646 without any file name is therefore an easy way to refresh
         1647 .CW sam 's
         1648 copy of a Unix file.
         1649 [Unlike in
         1650 .CW ed ,
         1651 .CW e
         1652 doesn't complain if the file is modified.  The principle is not
         1653 to protect against things that can be undone if wrong.]
         1654 Since its job is to replace the whole text,
         1655 .CW e
         1656 never takes an address.
         1657 .PP
         1658 The
         1659 .CW r
         1660 command is like
         1661 .CW e ,
         1662 but it doesn't clear the file:
         1663 the text in the Unix file replaces dot, or the specified text if an
         1664 address is given.
         1665 .P1
         1666 .WC "r emacs.ms
         1667 .P2
         1668 has essentially the effect of
         1669 .P1
         1670 .WC "<cat emacs.ms
         1671 .P2
         1672 The commands
         1673 .CW r
         1674 and
         1675 .CW w
         1676 will set the name of the file if the current file has no name already defined;
         1677 .CW e
         1678 sets the name even if the file already has one.
         1679 .PP
         1680 There is a command, analogous to
         1681 .CW x ,
         1682 that iterates over files instead of pieces of text:
         1683 .CW X
         1684 (capital
         1685 .CW x ).
         1686 The syntax is easy; it's just like that of
         1687 .CW x
         1688 \(em \f(CWX/\f2pattern\f(CW/\f2command\f1.
         1689 (The complementary command is
         1690 .CW Y ,
         1691 analogous to
         1692 .CW y .)
         1693 The effect is to run the command in each file whose menu entry
         1694 (that is, whose line printed by an
         1695 .CW f
         1696 command) matches the pattern.
         1697 For example, since an apostrophe identifies modified files,
         1698 .P1
         1699 .WC "X/'/ w
         1700 .P2
         1701 writes the changed files out to disc.
         1702 Here is a longer example: find all uses of a particular variable
         1703 in the C source files:
         1704 .P1
         1705 .WC "X/\e.c$/ ,x/variable/+-p
         1706 .P2
         1707 We can use an
         1708 .CW f
         1709 command to identify which file the variable appears in:
         1710 .P1
         1711 .ft I
         1712 X/\e.c$/ ,g/variable/ {
         1713         f
         1714         ,x/variable/+-{
         1715                 =
         1716                 p
         1717         }
         1718 }
         1719 .ft
         1720 .P2
         1721 Here, the
         1722 .CW g
         1723 command guarantees that only the names of files containing the variable
         1724 will be printed (but beware that
         1725 .CW sam
         1726 may confuse matters by printing the names of files it reads in during
         1727 the command).
         1728 The
         1729 .CW =
         1730 command shows where in the file the variable appears, and the
         1731 .CW p
         1732 command prints the line.
         1733 .PP
         1734 The
         1735 .CW D
         1736 command is handy as the target of an
         1737 .CW X .
         1738 This example deletes from the menu all C files that do not contain
         1739 a particular variable:
         1740 .P1
         1741 .WC "X/\e.c$/ ,v/variable/ D
         1742 .P2
         1743 If no pattern is provided for the
         1744 .CW X ,
         1745 the command (which defaults to
         1746 .CW f )
         1747 is run in all files, so
         1748 .P1
         1749 .WC "X D
         1750 .P2
         1751 cleans
         1752 .CW sam
         1753 up for a fresh start.
         1754 .PP
         1755 But rather than working any further, let's stop now:
         1756 .P1
         1757 .WC q
         1758 $
         1759 .P2
         1760 .fi
         1761 .PP
         1762 Some of the file manipulating commands can be undone:
         1763 undoing a
         1764 .CW f ,
         1765 .CW e ,
         1766 or
         1767 .CW r
         1768 restores the previous state of the file,
         1769 but
         1770 .CW w ,
         1771 .CW B
         1772 and
         1773 .CW D
         1774 are irrevocable.
         1775 And, of course, so is
         1776 .CW q .