Annotation of 43BSDReno/share/doc/usd/12.edtut/e2, revision 1.1.1.1

1.1       root        1: .\"    @(#)e2  6.1 (Berkeley) 5/22/86
                      2: .\"
                      3: .SH
                      4: Writing text out as a file \- the Write command ``w''
                      5: .PP
                      6: It's likely that you'll want to save your text for later use.
                      7: To write out the contents of the buffer onto a file,
                      8: use the
                      9: .ul
                     10: write
                     11: command
                     12: .P1
                     13: w
                     14: .P2
                     15: followed by the filename you want to write on.
                     16: This will copy the buffer's contents
                     17: onto the specified file
                     18: (destroying any previous information on the file).
                     19: To save
                     20: the text on a file named
                     21: .UL junk ,
                     22: for example, type
                     23: .P1
                     24: w junk
                     25: .P2
                     26: Leave a space between
                     27: .UL w
                     28: and the file name.
                     29: .ul
                     30: Ed
                     31: will respond by printing
                     32: the number of characters it wrote out.
                     33: In this case,
                     34: .ul
                     35: ed
                     36: would respond with
                     37: .P1
                     38: 68
                     39: .P2
                     40: (Remember that blanks and the return character at the end of each
                     41: line are included in the character count.)
                     42: Writing a file just makes a copy of the text \- the
                     43: buffer's contents are not disturbed, so you can go on adding
                     44: lines to it.
                     45: This is an important point.
                     46: .ul
                     47: Ed
                     48: at all times works on a copy
                     49: of a file, not the file itself.
                     50: No change in the contents
                     51: of a file takes place until you give a
                     52: .UL w
                     53: command.
                     54: (Writing out the text onto a file from time to time as it is being
                     55: created is a good idea, since if the system crashes or if you make some horrible mistake, you will lose
                     56: all the text in the buffer but any text that was written onto
                     57: a file is relatively safe.)
                     58: .SH
                     59: Leaving ed \- the Quit command ``q''
                     60: .PP
                     61: To terminate a session with
                     62: .IT ed ,
                     63: save the text you're working on
                     64: by writing it onto a file using the
                     65: .UL w
                     66: command,
                     67: and then type the command
                     68: .P1
                     69: q
                     70: .P2
                     71: which
                     72: stands for
                     73: .IT quit .
                     74: The system will respond with
                     75: the prompt character
                     76: .UL $ "" (
                     77: or
                     78: .UL % ).
                     79: At
                     80: this point your buffer vanishes, with all its text,
                     81: which is why you want to write it out before quitting.\(dg
                     82: .FS
                     83: \(dg Actually,
                     84: .IT ed
                     85: will print 
                     86: .UL ?
                     87: if you try to quit without writing.
                     88: At that point, write if you want;
                     89: if not, another
                     90: .UL q
                     91: will get you out regardless.
                     92: .FE
                     93: .SH
                     94: Exercise 1:
                     95: .PP
                     96: Enter
                     97: .ul
                     98: ed
                     99: and
                    100: create some text using
                    101: .P1
                    102: a
                    103: \&. . . text . . .
                    104: \&\fB.\fR
                    105: .P2
                    106: Write it out using
                    107: .UL w .
                    108: Then leave
                    109: .ul
                    110: ed
                    111: with the
                    112: .UL q
                    113: command, and print the file,
                    114: to see that everything worked.
                    115: (To print a file, say
                    116: .P1
                    117: pr filename
                    118: .P2
                    119: or
                    120: .P1
                    121: cat filename
                    122: .P2
                    123: in response to
                    124: the prompt character.
                    125: Try both.)
                    126: .SH
                    127: Reading text from a file \- the Edit command ``e''
                    128: .PP
                    129: A common way to get text into the buffer is to read it
                    130: from a file in the file system.
                    131: This is what you do to edit text
                    132: that you saved with the 
                    133: .UL w
                    134: command in a previous session.
                    135: The
                    136: .ul
                    137: edit
                    138: command
                    139: .UL e
                    140: fetches the entire contents of a file into the buffer.
                    141: So if you had saved the three lines
                    142: ``Now is the time'', etc.,
                    143: with a
                    144: .UL w
                    145: command in an earlier session,
                    146: the 
                    147: .ul
                    148: ed
                    149: command
                    150: .P1
                    151: e junk
                    152: .P2
                    153: would fetch the entire contents of the file
                    154: .UL junk
                    155: into the buffer, and respond
                    156: .P1
                    157: 68
                    158: .P2
                    159: which is the number of characters in
                    160: .UL junk .
                    161: .ul
                    162: If anything was already in the buffer, it is deleted first.
                    163: .PP
                    164: If you use the
                    165: .UL e
                    166: command to read a file into the buffer,
                    167: then you need not use a file name after a subsequent
                    168: .UL w
                    169: command;
                    170: .ul
                    171: ed
                    172: remembers the last file name used in an
                    173: .UL e
                    174: command,
                    175: and
                    176: .UL w
                    177: will write on this file.
                    178: Thus a good way to operate is
                    179: .P1
                    180: ed
                    181: e file
                    182: [editing session]
                    183: w
                    184: q
                    185: .P2
                    186: This way, you can simply say
                    187: .UL w
                    188: from time to time,
                    189: and be secure in the knowledge that
                    190: if you got the file name right at the beginning,
                    191: you are writing into the proper file each time.
                    192: .PP
                    193: You can find out at any time what file name
                    194: .ul
                    195: ed
                    196: is remembering by typing the 
                    197: .ul
                    198: file
                    199: command
                    200: .UL f .
                    201: In this example,
                    202: if you typed
                    203: .P1
                    204: f
                    205: .P2
                    206: .ul
                    207: ed
                    208: would reply
                    209: .P1
                    210: junk
                    211: .P2
                    212: .SH
                    213: Reading text from a file \- the Read command ``r''
                    214: .PP
                    215: Sometimes you want to read a file into the buffer
                    216: without destroying anything that is already there.
                    217: This is done by the
                    218: .ul
                    219: read
                    220: command
                    221: .UL r .
                    222: The command
                    223: .P1
                    224: r junk
                    225: .P2
                    226: will read the file
                    227: .UL junk
                    228: into the buffer;
                    229: it adds it
                    230: to the end of whatever is already in the buffer.
                    231: So if you do a read after
                    232: an edit:
                    233: .P1
                    234: e junk
                    235: r junk
                    236: .P2
                    237: the buffer will contain
                    238: .ul
                    239: two
                    240: copies of the text (six lines).
                    241: .P1
                    242: Now is the time
                    243: for all good men
                    244: to come to the aid of their party.
                    245: Now is the time
                    246: for all good men
                    247: to come to the aid of their party.
                    248: .P2
                    249: Like the
                    250: .UL w
                    251: and
                    252: .UL e
                    253: commands,
                    254: .UL r
                    255: prints
                    256: the
                    257: number of characters read in, after the reading operation is complete.
                    258: .PP
                    259: Generally speaking,
                    260: .UL r
                    261: is much less used than
                    262: .UL e .
                    263: .SH
                    264: Exercise 2:
                    265: .PP
                    266: Experiment with the
                    267: .UL e
                    268: command \-
                    269: try reading and printing various files.
                    270: You may get an error
                    271: .UL ?name ,
                    272: where
                    273: .UL name
                    274: is the name of a file;
                    275: this means that the file doesn't exist,
                    276: typically because you spelled the file name wrong,
                    277: or perhaps that you are not allowed to read or write it.
                    278: Try alternately reading and appending to see that they work
                    279: similarly.
                    280: Verify that
                    281: .P1
                    282: ed filename
                    283: .P2
                    284: is exactly equivalent to
                    285: .P1
                    286: ed
                    287: e filename
                    288: .P2
                    289: What does
                    290: .P1
                    291: f filename
                    292: .P2
                    293: do?
                    294: .SH
                    295: Printing the contents of the buffer \- the Print command ``p''
                    296: .PP
                    297: To
                    298: .ul
                    299: print
                    300: or list the contents of the buffer (or parts
                    301: of it) on the terminal, use the print command
                    302: .P1
                    303: p
                    304: .P2
                    305: The way this is done is as follows.
                    306: Specify the lines where
                    307: you want printing to begin and where you want it to end,
                    308: separated by a comma, and
                    309: followed by the letter
                    310: .UL p .
                    311: Thus to print the first two lines of the buffer, for
                    312: example, (that is, lines 1 through 2) say
                    313: .P1
                    314: 1,2p   (starting line=1, ending line=2 p)
                    315: .P2
                    316: .ul
                    317: Ed
                    318: will respond with
                    319: .P1
                    320: Now is the time
                    321: for all good men
                    322: .P2
                    323: .PP
                    324: Suppose you want to print
                    325: .ul
                    326: all
                    327: the lines in the buffer.
                    328: You could use
                    329: .UL 1,3p
                    330: as above if you knew there were exactly
                    331: 3 lines in the buffer.
                    332: But in general, you don't
                    333: know how many there are, so what do you use for the ending
                    334: line number?
                    335: .ul
                    336: Ed
                    337: provides a shorthand symbol for ``line number of
                    338: last line in buffer'' \- the dollar sign
                    339: .UL $ .
                    340: Use it this
                    341: way:
                    342: .P1
                    343: 1,$p
                    344: .P2
                    345: This will print
                    346: .ul
                    347: all
                    348: the lines in the buffer (line 1 to last line.)
                    349: If you want to stop the printing before it is finished,
                    350: push the
                    351: .UC DEL
                    352: or Delete key;
                    353: .ul
                    354: ed
                    355: will type
                    356: .P1
                    357: ?
                    358: .P2
                    359: and wait for the next command.
                    360: .PP
                    361: To print the
                    362: .ul
                    363: last
                    364: line of the buffer, you could use
                    365: .P1
                    366: $,$p
                    367: .P2
                    368: but
                    369: .ul
                    370: ed
                    371: lets you abbreviate this to
                    372: .P1
                    373: $p
                    374: .P2
                    375: You can print any single line by typing the line
                    376: number followed by a
                    377: .UL p .
                    378: Thus
                    379: .P1
                    380: 1p
                    381: .P2
                    382: produces the response
                    383: .P1
                    384: Now is the time
                    385: .P2
                    386: which is the first line of the buffer.
                    387: .PP
                    388: In fact,
                    389: .ul
                    390: ed
                    391: lets you abbreviate even further:
                    392: you can print any single line by typing
                    393: .ul
                    394: just
                    395: the line number \- no need to type the letter
                    396: .UL p .
                    397: So if you say
                    398: .P1
                    399: $
                    400: .P2
                    401: .ul
                    402: ed
                    403: will print the last line of the buffer.
                    404: .PP
                    405: You can also use
                    406: .UL $
                    407: in combinations like
                    408: .P1
                    409: $\-1,$p
                    410: .P2
                    411: which prints the last two lines of the buffer.
                    412: This helps when you want to see how far you got in typing.
                    413: .SH
                    414: Exercise 3:
                    415: .PP
                    416: As before, create some text using the
                    417: .UL a
                    418: command and
                    419: experiment with the
                    420: .UL p
                    421: command.
                    422: You will find, for example,
                    423: that you can't print line 0 or a line beyond
                    424: the end of the buffer, and that attempts
                    425: to print a buffer in reverse order by saying
                    426: .P1
                    427: 3,1p
                    428: .P2
                    429: don't work.

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.