📄 e2
字号:
.H1Writing text out as a file \(mi the Write command ``w''.H2.PGIt's likely that we'll want to save our text for later use.To write out the contents of the buffer onto a file,we use the.ulwritecommand.X1w.X2followed by the filename we want to write on.This will copy the buffer's contentsonto the specified file(destroying any previous information on the file).To savethe text on a file named ``junk'', for example, type.X1w junk.X2Leave a space between ``w'' and the file name..ulEdwill respond by printingthe number of characters it wrote out.In our case,.uledwould respond with.X168.X2(Remember that blanks and the newline character at the end of eachline are included in the character count.)Writing a file just makes a copy of the text \(mi thebuffer's contents are not disturbed, so we can go on addinglines to it.This is an important point..ulEdat all times works on a copyof a file, not the file itself.No change in the contentsof a file takes place until you give a ``w'' command.(Writing out the text onto a file from time to time as it is beingcreated is a good idea, since if the system crashes or if you make some horrible mistake, you will loseall the text in the buffer but any text that was written ontoa file is relatively safe.).H1Leaving ed \(mi the Quit command ``q''.H2.PGTo terminate a session with.uled,save the text you're working onby writing it onto a file using the ``w'' command,and then type the command.X1q.X2whichstands for.ulquit.The system will respond with ``%''.Atthis point your buffer vanishes, with all its text,which is why you want to write it out before quitting..H1Exercise 1:.H2.PGEnter.uledandcreate some text using.X1a.li. . . text . . ..li\fB.\fR.X2Write it out using ``w''.Then leave.uledwith the ``q'' command, and print the file,to see that everything worked.(To print a file, say.X1pr filename.X2or.X1cat filename.X2in response to ``%''.Try both.).H1Reading text from a file \(mi the Edit command ``e''.H2.PGA common way to get text into the buffer is to read itfrom a file in the file system.This is what you do to edit textthat you saved with the ``w''command in a previous session.The.uleditcommand ``e''fetches the entire contents of a file into the buffer.So if we had saved the three lines``Now is the time'', etc.,with a ``w'' command in an earlier session,the .uledcommand.X1e junk.X2would fetch the entire contents of the file ``junk''into the buffer, and respond.X168.X2which is the number of characters in ``junk''..ulIf anything was already in the buffer, it is deleted first..PGIf we use the ``e'' command to read a file into the buffer,then we need not use a file name after a subsequent ``w'' command;.uledremembers the last file name used in an ``e'' command,and ``w'' will write on this file.Thus a common way to operate is.X1ede file[editing session]wq.X2.PGYou can find out at any time what file name.uledis remembering by typing the .ulfilecommand ``f''.In our case,if we typed.X1f.X2.uledwould reply.X1junk.X2.H1Reading text from a file \(mi the Read command ``r''.H2.PGSometimes we want to read a file into the bufferwithout destroying anything that is already there.This is done by the.ulreadcommand ``r''.The command.X1r junk.X2will read the file ``junk'' into the buffer;it adds itto the end of whatever is already in the buffer.So if we do a read afteran edit:.X1e junkr junk.X2the buffer will contain.ultwocopies of the text (six lines)..X1Now is the timefor all good mento come to the aid of their party.Now is the timefor all good mento come to the aid of their party..X2Like the ``w'' and ``e'' commands, ``r'' prints thenumber of characters read in, after the reading operation is complete..PGGenerally speaking, ``r'' is much less used than ``e''..H1Exercise 2:.H2.PGExperiment with the ``e'' command \(mitry reading and printing various files.You may get an error ``?'',typically because you spelled the file name wrong.Try alternately reading and appending to see that they worksimilarly.Verify that.X1ed filename.X2is exactly equivalent to.X1ede filename.X2What does.X1f filename.X2do?.H1Printing the contents of the buffer \(mi the Print command ``p''.H2.PGTo.ulprintor list the contents of the buffer (or partsof it) on the terminal, we use the print command.X1p.X2The way this is done is as follows.We specify the lines wherewe want printing to begin and where we want it to end,separated by a comma, andfollowed by the letter ``p''.Thus to print the first two lines of the buffer, forexample, (that is, lines 1 through 2) we say.X11,2p (starting line=1, ending line=2 p).X2.ulEdwill respond with.X1Now is the timefor all good men.X2.PGSuppose we want to print.ulallthe lines in the buffer.We could use ``1,3p'' as above if we knew there were exactly3 lines in the buffer.But in general, we don'tknow how many there are, so what do we use for the endingline number?.ulEdprovides a shorthand symbol for ``line number oflast line in buffer'' \(mi the dollar sign ``$''.Use it thisway:.X11,$p.X2This will print.ulallthe lines in the buffer (line 1 to last line.)If you want to stop the printing before it is finished,push the DEL or Delete key;.uledwill type.X1?.X2and wait for the next command..PGTo print the.ullastline of the buffer, we could use.X1$,$p.X2but.uledlets us abbreviate this to.X1$p.X2We can print any single line by typing the linenumber followed by a ``p''.Thus.X11p.X2produces the response.X1Now is the time.X2which is the first line of the buffer..PGIn fact,.uledlets us abbreviate even further:we can print any single line by typing.uljustthe line number \(mi no need to type the letter ``p''.So if we say.X1$.X2.uledwill print the last line of the buffer for us..PGWe can also use ``$'' in combinations like.X1$\(mi1,$p.X2which prints the last two lines of the buffer.This helps when we want to see how far we got in typing..H1Exercise 3:.H2.PG.H2As before, create some text using the append command andexperiment with the ``p'' command.You will find, for example,that you can't print line 0 or a line beyondthe end of the buffer, and that attemptsto print a buffer in reverse order by saying.X13,1p.X2don't work.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -