Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_posix/sys/doc/sam/sam.tut – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 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\f(CW\\$1\fP\&\\$2
15
.lg
16
..
17
.de WC
18
.lg 0
19
\%\&\\$3\f(CI\\$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
$ \f(CIsam -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 CI
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
.WC 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 \f(CWA\f(CIAA\f(CWA\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 egrep (1),
731
including parentheses
732
.CW ()
733
for grouping and an `or' operator
734
.CW |
735
for matching strings in parallel.
736
.CW sam
737
makes two extensions:
738
although
739
.CW .
740
(the most overloaded character in Unix) matches any character
741
.I except
742
newline, the regular expression
743
.CW @
744
(think of it as a big dot) matches any character, even newlines;
745
and the character sequence
746
.CW \en
747
matches a newline character.
748
Replacement text, such as used in the
749
.CW a
750
and
751
.CW c
752
commands, is still plain text, but the sequence
753
.CW \en
754
represents newline in that context, too.
755
.PP
756
Here is an example.  Say we wanted to double space the document, that is,
757
turn every newline into two newlines.
758
The following all do the job:
759
.P1
760
.WC ",x/\en/ a/\en/
761
.WC ",x/\en/ c/\en\en/
762
.WC ",x/$/ a/\en/
763
.WC ",x/^/ i/\en/
764
.P2
765
The last example is slightly different, because it puts a newline
766
.I before
767
each line; the other examples place it after.
768
The first two examples manipulate newlines directly
769
[something outside
770
.CW ed 's
771
ken]; the last two
772
use regular expressions:
773
.CW $
774
is the empty string at the end of a line, while
775
.CW ^
776
is the empty string at the beginning.
777
.PP
778
These solutions all have a possible drawback: if there is already a blank line
779
(that is, two consecutive newlines), they make it much larger (four
780
consecutive newlines).
781
A better method is to extend every group of newlines by one:
782
.P1
783
.WC ",x/\en+/ a/\en/
784
.P2
785
The regular expression operator
786
.CW +
787
means `one or more;'
788
.CW \en+
789
is identical to
790
.CW \en\en* .
791
Thus, this example
792
takes every sequence of newlines and adds another
793
to the end.
794
.PP
795
A more common example is indenting a block of text by a tab stop.
796
The following all work,
797
although the first is arguably the cleanest (the blank text in slashes is a tab):
798
.P1
799
.WC ",x/^/a/	 /
800
.WC ",x/^/c/	 /
801
.WC ",x/.*\en/i/	 /
802
.P2
803
The last example uses the pattern (idiom, really)
804
.CW .*\en
805
to match lines:
806
.CW .*
807
matches the longest possible string of non-newline characters.
808
Taking initial tabs away is just as easy:
809
.P1
810
.WC ",x/^    /d
811
.P2
812
In these examples I have specified an address (the whole file), but
813
in practice commands like these are more likely to be run without
814
an address, using the value of dot set by selecting text with the mouse.
815
.SH
816
Conditionals
817
.PP
818
The
819
.CW x
820
command is a looping construct:
821
for each match of a regular expression,
822
it extracts (sets dot to) the match and runs a command.
823
.CW sam
824
also has a conditional,
825
.CW g :
826
\f(CWg/\f2pattern\f(CW/\f2command\f1
827
runs the command if dot contains a match of the pattern
828
.I
829
without changing the value of dot.
830
.R
831
The inverse,
832
.CW v ,
833
runs the command if dot does
834
.I not
835
contain a match of the pattern.
836
(The letters
837
.CW g
838
and
839
.CW v
840
are historical and have no mnemonic significance.  You might
841
think of
842
.CW g
843
as `guard.')
844
.CW ed "" [
845
users should read the above definitions very carefully; the
846
.CW g
847
command in
848
.CW sam
849
is fundamentally different from that in
850
.CW ed .]
851
Here is an example of the difference between
852
.CW x
853
and
854
.CW g:
855
.P1
856
,x/Emacs/c/vi/
857
.P2
858
changes each occurrence of the word
859
.CW Emacs
860
in the file to the word
861
.CW vi ,
862
but
863
.P1
864
,g/Emacs/c/vi/
865
.P2
866
changes the
867
.I "whole file
868
to
869
.CW vi
870
if there is the word
871
.CW Emacs
872
anywhere in the file.
873
.PP
874
Neither of these commands is particularly interesting in isolation,
875
but they are valuable when combined with
876
.CW x
877
and with themselves.
878
.SH
879
Composition
880
.PP
881
One way to think about the
882
.CW x
883
command is that, given a selection (a value of dot)
884
it iterates through interesting subselections (values of dot within).
885
In other words, it takes a piece of text and cuts it into smaller pieces.
886
But the text that it cuts up may already be a piece cut by a previous
887
.CW x
888
command or selected by a
889
.CW g .
890
.CW sam 's
891
most interesting property is the ability to define a sequence of commands
892
to perform a particular task.\(dg
893
.FS
894
\(dg
895
The obvious analogy with shell pipelines is only partially valid,
896
because the individual
897
.CW sam
898
commands are all working on the same text; it is only how the text is
899
sliced up that is changing.
900
.FE
901
A simple example is to change all occurrences of
902
.CW Emacs
903
to
904
.CW emacs ;
905
certainly the command
906
.P1
907
.WC ",x/Emacs/ c/emacs/
908
.P2
909
will work, but we can use an
910
.CW x
911
command to save retyping most of the word
912
.CW Emacs :
913
.P1
914
.WC ",x/Emacs/ x/E/ c/e/
915
.P2
916
(Blanks can be used
917
to separate commands on a line to make them easier to read.)
918
What this command does is find all occurrences of
919
.CW Emacs
920
.CW ,x/Emacs/ ), (
921
and then
922
.I
923
with dot set to that text,
924
.R
925
find all occurrences of the letter
926
.CW E
927
.CW x/E/ ), (
928
and then
929
.I
930
with dot set to that text,
931
.R
932
run the command
933
.CW c/e/
934
to change the character to lower case.
935
Note that the address for the command \(em the whole file, specified by a comma
936
\(em is only given to the leftmost
937
piece of the command; the rest of the pieces have dot set for them by
938
the execution of the pieces to their left.
939
.PP
940
As another simple example, consider a problem
941
solved above: printing all lines in the file containing the word
942
.CW Emacs:
943
.P1
944
.WC ",x/.*\en/ g/Emacs/p
945
general introduction to the commands in Emacs and to try to show
946
the method in the madness that is the Emacs command structure.
947
.P2
948
This command says to break the file into lines
949
.CW ,x/.*\en/ ), (
950
and for each line that contains the string
951
.CW Emacs
952
.CW g/Emacs/ ), (
953
run the command
954
.CW p
955
with dot set to the line (not the match of
956
.CW Emacs ),
957
which prints the line.
958
To save typing, because
959
.CW .*\en
960
is a common pattern in
961
.CW x
962
commands,
963
if the
964
.CW x
965
is followed immediately by a space, the pattern
966
.CW .*\en
967
is assumed.
968
Therefore, the above could be written more succinctly:
969
.P1
970
.WC ",x g/Emacs/p
971
.P2
972
The solution we used before was
973
.P1
974
.WC ,x/Emacs/+-p
975
.P2
976
which runs the command
977
.CW +-p
978
with dot set to each match of
979
.CW Emacs
980
in the file (recall that the idiom
981
.CW +-p
982
prints the line containing the end of dot).
983
.PP
984
The two commands usually produce the same result
985
(the
986
.CW +-p
987
form will print a line twice if it contains
988
.CW Emacs
989
twice).  Which is better?
990
.CW ,x/Emacs/+-p
991
is easier to type and will be much faster if the file is large and
992
there are few occurrences of the string, but it is really an odd special case.
993
.CW ",x/.*\en/ g/Emacs/p
994
is slower \(em it breaks each line out separately, then examines
995
it for a match \(em but is conceptually cleaner, and generalizes more easily.
996
For example, consider the following piece of the Emacs manual:
997
.P1
998
command name="append-to-file", key="[unbound]"
999
Takes the contents of the current buffer and appends it to the
1000
named file. If the files doesn't exist, it will be created.
1001
 
1002
command name="apropos", key="ESC-?"
1003
Prompts for a keyword and then prints a list of those commands
1004
whose short description contains that keyword.  For example,
1005
if you forget which commands deal with windows, just type
1006
"@b[ESC-?]@t[window]@b[ESC]".
1007
 
1008
\&\f2and so on\f(CW
1009
.P2
1010
This text consists of groups of non-empty lines, with a simple format
1011
for the text within each group.
1012
Imagine that we wanted to find the description of the `apropos'
1013
command.
1014
The problem is to break the file into individual descriptions,
1015
and then to find the description of `apropos' and to print it.
1016
The solution is straightforward:
1017
.P1
1018
.WC ,x/(.+\en)+/\ g/command\ name="apropos"/p
1019
command name="apropos", key="ESC-?"
1020
Prompts for a keyword and then prints a list of those commands
1021
whose short description contains that keyword.  For example,
1022
if you forget which commands deal with windows, just type
1023
"@b[ESC-?]@t[window]@b[ESC]".
1024
.P2
1025
The regular expression
1026
.CW (.+\en)+
1027
matches one or more lines with one or more characters each, that is,
1028
the text between blank lines, so
1029
.CW ,x/(.+\en)+/
1030
extracts each description; then
1031
.CW g/command\ name="apropos"/
1032
selects the description for `apropos' and
1033
.CW p
1034
prints it.
1035
.PP
1036
Imagine that we had a C program containing the variable
1037
.CW n ,
1038
but we wanted to change it to
1039
.CW num .
1040
This command is a first cut:
1041
.P1
1042
.WC ",x/n/ c/num/
1043
.P2
1044
but is obviously flawed: it will change all
1045
.CW n 's
1046
in the file, not just the
1047
.I identifier
1048
.CW n .
1049
A better solution is to use an
1050
.CW x
1051
command to extract the identifiers, and then use
1052
.CW g
1053
to find the
1054
.CW n 's:
1055
.P1
1056
.WC ",x/[a-zA-Z_][a-zA-Z_0-9]*/ g/n/ v/../ c/num/
1057
.P2
1058
It looks awful, but it's fairly easy to understand when read
1059
left to right.
1060
A C identifier is an alphabetic or underscore followed by zero or more
1061
alphanumerics or underscores, that is, matches of the regular expression
1062
.CW [a-zA-Z_][a-zA-Z_0-9]* .
1063
The
1064
.CW g
1065
command selects those identifiers containing
1066
.CW n ,
1067
and the
1068
.CW v
1069
is a trick: it rejects those identifiers containing more than one
1070
character.  Hence the
1071
.CW c/num/
1072
applies only to free-standing
1073
.CW n 's.
1074
.PP
1075
There is still a problem here:
1076
we don't want to change
1077
.CW n 's
1078
that are part of the character constant
1079
.CW \en .
1080
There is a command
1081
.CW y ,
1082
complementary to
1083
.CW x ,
1084
that is just what we need:
1085
\f(CWy/\f2pattern\f(CW/\f2command\f1
1086
runs the command on the pieces of text
1087
.I between
1088
matches of the pattern;
1089
if
1090
.CW x
1091
selects,
1092
.CW y
1093
rejects.
1094
Here is the final command:
1095
.P1
1096
.WC ",y/\e\en/ x/[a-zA-Z_][a-zA-Z_0-9]*/ g/n/ v/../ c/num/
1097
.P2
1098
The
1099
.CW y/\e\en/
1100
(with backslash doubled to make it a literal character)
1101
removes the two-character sequence
1102
.CW \en
1103
from consideration, so the rest of the command will not touch it.
1104
There is more we could do here; for example, another
1105
.CW y
1106
could be prefixed to protect comments in the code.
1107
I won't elaborate the example any further, but you should have
1108
an idea of the way in which the looping and conditional commands
1109
in
1110
.CW sam
1111
may be composed to do interesting things.
1112
.SH
1113
Grouping
1114
.PP
1115
There is another way to arrange commands.
1116
By enclosing them in brace brackets
1117
.CW {} ,
1118
commands may be applied in parallel.
1119
This example uses the
1120
.CW =
1121
command, which reports the line and character numbers of dot,
1122
together with
1123
.CW p ,
1124
to report on appearances of
1125
.CW Emacs
1126
in our original file:
1127
.P1
1128
.WC ,p
1129
This manual is organized in a rather haphazard manner.  The first
1130
several sections were written hastily in an attempt to provide a
1131
general introduction to the commands in Emacs and to try to show
1132
the method in the madness that is the Emacs command structure.
1133
.ft CI
1134
,x/Emacs/{
1135
	=
1136
	+-p
1137
}
1138
.ft
1139
3; #171,#176
1140
general introduction to the commands in Emacs and to try to show
1141
4; #234,#239
1142
the method in the madness that is the Emacs command structure.
1143
.P2
1144
(The number before the semicolon is the line number;
1145
the numbers beginning with
1146
.CW #
1147
are character numbers.)
1148
As a more interesting example, consider changing all occurrences of
1149
.CW Emacs
1150
to
1151
.CW vi
1152
and vice versa.  We can type
1153
.P1
1154
.ft CI
1155
,x/Emacs|vi/{
1156
	g/Emacs/ c/vi/
1157
	g/vi/ c/Emacs/
1158
}
1159
.ft
1160
.P2
1161
or even
1162
.P1
1163
.ft CI
1164
,x/[a-zA-Z]+/{
1165
	g/Emacs/ v/....../ c/vi/
1166
	g/vi/ v/.../ c/Emacs/
1167
}
1168
.ft
1169
.P2
1170
to make sure we don't change strings embedded in words.
1171
.SH
1172
Multiple Changes
1173
.PP
1174
You might wonder why, once
1175
.CW Emacs
1176
has been changed to
1177
.CW vi
1178
in the above example,
1179
the second command in the braces doesn't put it back again.
1180
The reason is that the commands are run in parallel:
1181
within any top-level
1182
.CW sam
1183
command, all changes to the file refer to the state of the file
1184
before any of the changes in that command are made.
1185
After all the changes have been determined, they are all applied
1186
simultaneously.
1187
.PP
1188
This means, as mentioned, that commands within a compound
1189
command see the state of the file before any of the changes apply.
1190
This method of evaluation makes some things easier (such as the exchange of
1191
.CW Emacs
1192
and
1193
.CW vi ),
1194
and some things harder.
1195
For instance, it is impossible to use a
1196
.CW p
1197
command to print the changes as they happen,
1198
because they haven't happened when the
1199
.CW p
1200
is executed.
1201
An indirect ramification is that changes must occur in forward
1202
order through the file,
1203
and must not overlap.
1204
.SH
1205
Unix
1206
.PP
1207
.CW sam
1208
has a few commands to connect to Unix processes.
1209
The simplest is
1210
.CW ! ,
1211
which runs the command with input and output connected to the terminal.
1212
.P1
1213
.WC !date
1214
Wed May 28 23:25:21 EDT 1986
1215
!
1216
.P2
1217
(When downloaded, the input is connected to
1218
.CW /dev/null
1219
and only the first few lines of output are printed;
1220
any overflow is stored in
1221
.CW $HOME/sam.err .)
1222
The final
1223
.CW !
1224
is a prompt to indicate when the command completes.
1225
.PP
1226
Slightly more interesting is
1227
.CW > ,
1228
which provides the current text as standard input to the Unix command:
1229
.P1
1230
.WC "1,2 >wc
1231
      2       22      131
1232
!
1233
.P2
1234
The complement of
1235
.CW >
1236
is, naturally,
1237
.CW < :
1238
it replaces the current text with the standard output of the Unix command:
1239
.P1
1240
.WC "1 <date
1241
!
1242
.WC 1p
1243
Wed May 28 23:26:44 EDT 1986
1244
.P2
1245
The last command is
1246
.CW | ,
1247
which is a combination of
1248
.CW <
1249
and
1250
.CW > :
1251
the current text is provided as standard input to the Unix command,
1252
and the Unix command's standard output is collected and used to
1253
replace the original text.
1254
For example,
1255
.P1
1256
.WC ",| sort
1257
.P2
1258
runs
1259
.CW sort (1)
1260
on the file, sorting the lines of the text lexicographically.
1261
Note that
1262
.CW < ,
1263
.CW >
1264
and
1265
.CW |
1266
are
1267
.CW sam
1268
commands, not Unix shell operators.
1269
.PP
1270
The next example converts all appearances of
1271
.CW Emacs
1272
to upper case using
1273
.CW tr (1):
1274
.P1
1275
.WC ",x/Emacs/ | tr a-z A-Z
1276
.P2
1277
.CW tr
1278
is run once for each occurrence of
1279
.CW Emacs .
1280
Of course, you could do this example more efficiently with a simple
1281
.CW c
1282
command, but here's a trickier one:
1283
given a Unix mail box as input,
1284
convert all the
1285
.CW Subject
1286
headers to distinct fortunes:
1287
.P1
1288
.WC ",x/^Subject:.*\en/ x/[^:]*\en/ < /usr/games/fortune
1289
.P2
1290
(The regular expression
1291
.CW [^:]
1292
refers to any character
1293
.I except
1294
.CW :
1295
and newline; the negation operator
1296
.CW ^
1297
excludes newline from the list of characters.) 
1298
Again,
1299
.CW /usr/games/fortune
1300
is run once for each
1301
.CW Subject
1302
line, so each
1303
.CW Subject
1304
line is changed to a different fortune.
1305
.SH
1306
A few other text commands
1307
.PP
1308
For completeness, I should mention three other commands that
1309
manipulate text.  The
1310
.CW m
1311
command moves the current text to after the text specified by the
1312
(obligatory) address after the command.
1313
Thus
1314
.P1
1315
.WC "/Emacs/+- m 0
1316
.P2
1317
moves the next line containing
1318
.CW Emacs
1319
to the beginning of the file.
1320
Similarly,
1321
.CW t
1322
(another historic character) copies the text:
1323
.P1
1324
.WC "/Emacs/+- t 0
1325
.P2
1326
would make, at the beginning of the file, a copy of the next line
1327
containing
1328
.CW Emacs .
1329
.PP
1330
The third command is more interesting: it makes substitutions.
1331
Its syntax is
1332
\f(CWs/\f2pattern\f(CW/\f2replacement\f(CW/\f1.
1333
Within the current text, it finds the first occurrence of
1334
the pattern and replaces it by the replacement text,
1335
leaving dot set to the entire address of the substitution.
1336
.P1
1337
.WC 1p
1338
This manual is organized in a rather haphazard manner.  The first
1339
.WC s/haphazard/thoughtless/
1340
.WC p
1341
This manual is organized in a rather thoughtless manner.  The first
1342
.P2
1343
Occurrences of the character
1344
.CW &
1345
in the replacement text stand for the text matching the pattern.
1346
.P1
1347
.WC s/T/"&&&&"/
1348
.WC p
1349
"TTTT"his manual is organized in a rather thoughtless manner.  The first
1350
.P2
1351
There are two variants.  The first is that a number may be specified
1352
after the
1353
.CW s ,
1354
to indicate which occurrence of the pattern to substitute; the default
1355
is the first.
1356
.P1
1357
.WC s2/is/was/
1358
.WC p
1359
"TTTT"his manual was organized in a rather thoughtless manner.  The first
1360
.P2
1361
The second is that suffixing a
1362
.CW g
1363
(global) causes replacement of all occurrences, not just the first.
1364
.P1
1365
.WC s/[a-zA-Z]/x/g
1366
.WC p
1367
"xxxx"xxx xxxxxx xxx xxxxxxxxx xx x xxxxxx xxxxxxxxxxx xxxxxxx  xxx xxxxx
1368
.P2
1369
Notice that in all these examples
1370
dot is left
1371
set to the entire line.
1372
.PP
1373
[The substitute command is vital to
1374
.CW ed,
1375
because it is the only way to make changes within a line.
1376
It is less valuable in
1377
.CW sam ,
1378
in which the concept of a line is much less important.
1379
For example, many
1380
.CW ed
1381
substitution idioms are handled well by
1382
.CW sam 's
1383
basic commands. Consider the commands
1384
.P1
1385
s/good/bad/
1386
s/good//
1387
s/good/& bye/
1388
.P2
1389
which are equivalent in
1390
.CW sam
1391
to
1392
.P1
1393
/good/c/bad/
1394
/good/d
1395
/good/a/ bye/
1396
.P2
1397
and for which the context search is likely unnecessary because the desired
1398
text is already dot.
1399
Also, beware this
1400
.CW ed
1401
idiom:
1402
.P1
1403
1,$s/good/bad/
1404
.P2
1405
which changes the first
1406
.CW good
1407
on each line; the same command in
1408
.CW sam
1409
will only change the first one in the whole file.
1410
The correct
1411
.CW sam
1412
version is
1413
.P1
1414
,x s/good/bad/
1415
.P2
1416
but what is more likely meant is
1417
.P1
1418
,x/good/ c/bad/
1419
.P2
1420
.CW sam
1421
operates under different rules.]
1422
.SH
1423
Files
1424
.PP
1425
So far, we have only been working with a single file,
1426
but
1427
.CW sam
1428
is a multi-file editor.
1429
Only one file may be edited at a time, but
1430
it is easy to change which file is the `current' file for editing.
1431
To see how to do this, we need a
1432
.CW sam
1433
with a few files;
1434
the easiest way to do this is to start it
1435
with a list of Unix file names to edit.
1436
.P1
1437
$ \f(CIecho *.ms\f(CW
1438
conquest.ms death.ms emacs.ms famine.ms slaughter.ms
1439
$ \f(CIsam -d *.ms\f(CW
1440
 -. conquest.ms
1441
.P2
1442
(I'm sorry the Horsemen don't appear in liturgical order.)
1443
The line printed by
1444
.CW sam
1445
is an indication that the Unix file
1446
.CW conquest.ms
1447
has been read, and is now the current file.
1448
.CW sam
1449
does not read the Unix file until
1450
the associated
1451
.CW sam
1452
file becomes current.
1453
.PP
1454
The
1455
.CW n
1456
command prints the names of all the files:
1457
.P1
1458
.WC n
1459
 -. conquest.ms
1460
 -  death.ms
1461
 -  emacs.ms
1462
 -  famine.ms
1463
 -  slaughter.ms
1464
.P2
1465
This list is also available in the menu on mouse button 3.
1466
The command
1467
.CW f
1468
tells the name of just the current file:
1469
.P1
1470
.WC f
1471
 -. conquest.ms
1472
.P2
1473
The characters to the left of the file name encode helpful information about
1474
the file.
1475
The minus sign becomes a plus sign if the file has a window open, and an
1476
asterisk if more than one is open.
1477
The period (another meaning of dot) identifies the current file.
1478
The leading blank changes to an apostrophe if the file is different
1479
from the contents of the associated Unix file, as far as
1480
.CW sam
1481
knows.
1482
This becomes evident if we make a change.
1483
.P1
1484
.WC 1d
1485
.WC f
1486
\&'-. conquest.ms
1487
.P2
1488
If the file is restored by an undo command, the apostrophe disappears.
1489
.P1
1490
.WC u
1491
.WC f
1492
 -. conquest.ms
1493
.P2
1494
The file name may be changed by providing a new name with the
1495
.CW f
1496
command:
1497
.P1
1498
.CW "f pestilence.ms
1499
\&'-. pestilence.ms
1500
.P2
1501
.WC f
1502
prints the new status of the file,
1503
that is, it changes the name if one is provided, and prints the
1504
name regardless.
1505
A file name change may also be undone.
1506
.P1
1507
.WC u
1508
.WC f
1509
 -. conquest.ms
1510
.P2
1511
.PP
1512
When
1513
.CW sam
1514
is downloaded, the current file may be changed simply by selecting
1515
the desired file from the menu (selecting the same file subsequently
1516
cycles through the windows opened on the file).
1517
Otherwise, the
1518
.CW b
1519
command can be used to choose the desired file:\(dg
1520
.FS
1521
\(dg A bug prevents the
1522
.CW b
1523
command from working when downloaded.
1524
Because the menu is more convenient anyway, and
1525
because the method
1526
of choosing files from the command language is slated to change,
1527
the bug hasn't been fixed.
1528
.FE
1529
.P1
1530
.WC "b emacs.ms
1531
 -. emacs.ms
1532
.P2
1533
Again,
1534
.CW sam
1535
prints the name (actually, executes an implicit
1536
.CW f
1537
command) because the Unix file
1538
.CW emacs.ms
1539
is being read for the first time.
1540
It is an error to ask for a file
1541
.CW sam
1542
doesn't know about, but the
1543
.CW B
1544
command will prime
1545
.CW sam 's
1546
menu with a new file, and make it current.
1547
.P1
1548
.WC "b flood.pic
1549
?no such file `flood.pic'
1550
.WC "B flood.pic
1551
 -. flood.pic
1552
.WC n
1553
 -  conquest.ms
1554
 -  death.ms
1555
 -  emacs.ms
1556
 -  famine.ms
1557
 -. flood.pic
1558
 -  slaughter.ms
1559
.P2
1560
Both
1561
.CW b
1562
and
1563
.CW B
1564
will accept a list of file names.
1565
.CW b
1566
simply takes the first file in the list, but
1567
.CW B
1568
loads them all.
1569
The list may be typed on one line \(em
1570
.P1
1571
.WC "B devil.tex satan.tex 666.tex emacs.tex
1572
.P2
1573
\(em or generated by a Unix command \(em
1574
.P1
1575
.WC "B <echo *.tex
1576
.P2
1577
The latter form requires a Unix command;
1578
.CW sam
1579
does not understand the shell file name metacharacters, so
1580
.CW "B *.tex
1581
attempts to load a single file named
1582
.CW *.tex .
1583
(The
1584
.CW <
1585
form is of course derived from
1586
.CW sam 's
1587
.CW <
1588
command.)
1589
.CW echo
1590
is not the only useful command to run subservient to
1591
.CW B ;
1592
for example,
1593
.P1
1594
.WC "B <grep -l Emacs *
1595
.P2
1596
will load only those files containing the string
1597
.CW Emacs .
1598
Finally, a special case: a
1599
.CW B
1600
with no arguments creates an empty, nameless file within
1601
.CW sam .
1602
.PP
1603
The complement of
1604
.CW B
1605
is
1606
.CW D :
1607
.P1
1608
.WC "D devil.tex satan.tex 666.tex emacs.tex
1609
.P2
1610
eradicates the files from
1611
.CW sam 's
1612
memory (not from the Unix machine's disc).
1613
.CW D
1614
without any file names removes the current file from
1615
.CW sam .
1616
.PP
1617
There are three other commands that relate the current file
1618
to Unix files.
1619
The
1620
.CW w
1621
command writes the file to disc;
1622
without arguments, it writes the entire file to the Unix file associated
1623
with the current file in
1624
.CW sam
1625
(it is the only command whose default address is not dot).
1626
Of course, you can specify an address to be written,
1627
and a different file name, with the obvious syntax:
1628
.P1
1629
.WC "1,2w /tmp/revelations
1630
/tmp/revelations: #44
1631
.P2
1632
.CW sam
1633
responds with the file name and the number of characters written to the file.
1634
The
1635
.CW write
1636
command on the button 3 menu is identical in function to an unadorned
1637
.CW w
1638
command.
1639
.PP
1640
The other two commands,
1641
.CW e
1642
and
1643
.CW r ,
1644
read data from Unix files.
1645
The
1646
.CW e
1647
command clears out the current file,
1648
reads the data from the named file (or uses the current file's old name if
1649
none is explicitly provided), and sets the file name.
1650
It's much like a
1651
.CW B
1652
command, but puts the information in the current file instead of a new one.
1653
.CW e
1654
without any file name is therefore an easy way to refresh
1655
.CW sam 's
1656
copy of a Unix file.
1657
[Unlike in
1658
.CW ed ,
1659
.CW e
1660
doesn't complain if the file is modified.  The principle is not
1661
to protect against things that can be undone if wrong.]
1662
Since its job is to replace the whole text,
1663
.CW e
1664
never takes an address.
1665
.PP
1666
The
1667
.CW r
1668
command is like
1669
.CW e ,
1670
but it doesn't clear the file:
1671
the text in the Unix file replaces dot, or the specified text if an
1672
address is given.
1673
.P1
1674
.WC "r emacs.ms
1675
.P2
1676
has essentially the effect of
1677
.P1
1678
.WC "<cat emacs.ms
1679
.P2
1680
The commands
1681
.CW r
1682
and
1683
.CW w
1684
will set the name of the file if the current file has no name already defined;
1685
.CW e
1686
sets the name even if the file already has one.
1687
.PP
1688
There is a command, analogous to
1689
.CW x ,
1690
that iterates over files instead of pieces of text:
1691
.CW X
1692
(capital
1693
.CW x ).
1694
The syntax is easy; it's just like that of
1695
.CW x
1696
\(em \f(CWX/\f2pattern\f(CW/\f2command\f1.
1697
(The complementary command is
1698
.CW Y ,
1699
analogous to
1700
.CW y .)
1701
The effect is to run the command in each file whose menu entry
1702
(that is, whose line printed by an
1703
.CW f
1704
command) matches the pattern.
1705
For example, since an apostrophe identifies modified files,
1706
.P1
1707
.WC "X/'/ w
1708
.P2
1709
writes the changed files out to disc.
1710
Here is a longer example: find all uses of a particular variable
1711
in the C source files:
1712
.P1
1713
.WC "X/\e.c$/ ,x/variable/+-p
1714
.P2
1715
We can use an
1716
.CW f
1717
command to identify which file the variable appears in:
1718
.P1
1719
.ft CI
1720
X/\e.c$/ ,g/variable/ {
1721
	f
1722
	,x/variable/+-{
1723
		=
1724
		p
1725
	}
1726
}
1727
.ft
1728
.P2
1729
Here, the
1730
.CW g
1731
command guarantees that only the names of files containing the variable
1732
will be printed (but beware that
1733
.CW sam
1734
may confuse matters by printing the names of files it reads in during
1735
the command).
1736
The
1737
.CW =
1738
command shows where in the file the variable appears, and the
1739
.CW p
1740
command prints the line.
1741
.PP
1742
The
1743
.CW D
1744
command is handy as the target of an
1745
.CW X .
1746
This example deletes from the menu all C files that do not contain
1747
a particular variable:
1748
.P1
1749
.WC "X/\e.c$/ ,v/variable/ D
1750
.P2
1751
If no pattern is provided for the
1752
.CW X ,
1753
the command (which defaults to
1754
.CW f )
1755
is run in all files, so
1756
.P1
1757
.WC "X D
1758
.P2
1759
cleans
1760
.CW sam
1761
up for a fresh start.
1762
.PP
1763
But rather than working any further, let's stop now:
1764
.P1
1765
.WC q
1766
$
1767
.P2
1768
.fi
1769
.PP
1770
Some of the file manipulating commands can be undone:
1771
undoing a
1772
.CW f ,
1773
.CW e ,
1774
or
1775
.CW r
1776
restores the previous state of the file,
1777
but
1778
.CW w ,
1779
.CW B
1780
and
1781
.CW D
1782
are irrevocable.
1783
And, of course, so is
1784
.CW q .