?? ae3
字號:
.NHLINE ADDRESSING IN THE EDITOR.PPThe next general area we will discuss is that ofline addressing in.UL ed ,that is, how you specify what lines are to beaffected by editing commands.We have already used constructions like .P11,$s/x/y/.P2to specify a change on all lines.And most users are long since familiar withusing a single newline (or return) to print the next line,and with.P1/thing/.P2to find a line that contains `thing'.Less familiar, surprisingly enough, is theuse of.P1?thing?.P2to scan.ulbackwardsfor the previous occurrence of `thing'.This is especially handy when you realize that the thingyou want to operate on is back up the page fromwhere you are currently editing..PPThe slash and question mark are the only characters you canuse to delimit a context search, though you can useessentially any character in a substitute command..SHAddress Arithmetic.PPThe next step is to combine the line numberslike `\*.', `$', `/.../' and `?...?'with `+' and `\-'.Thus.P1$-1.P2is a command to print the next to last line ofthe current file (that is, one line before line `$').For example, to recall how far you got in a previous editing session,.P1$-5,$p.P2prints the last six lines.(Be sure you understand why it's six, not five.)If there aren't six, of course, you'll get an error message..PPAs another example,.P1\&\*.-3,\*.+3p.P2prints from three lines before where you are now(at line dot)to three lines after,thus giving you a bit of context.By the way, the `+' can be omitted:.P1\&\*.-3,\*.3p.P2is absolutely identical in meaning..PPAnother area in which you can save typing effortin specifying lines is to use `\-' and `+' as line numbersby themselves..P1-.P2by itself is a command to move back up one line in the file.In fact, you can string several minus signs together to moveback up that many lines:.P1---.P2moves up three lines, as does `\-3'.Thus.P1-3,+3p.P2is also identical to the examples above..PPSince `\-' is shorter than `\*.\-1',constructions like.P1-,\*.s/bad/good/.P2are useful. This changes `bad' to `good' on the previous line andon the current line..PP`+' and `\-' can be used in combination with searches using `/.../' and `?...?',and with `$'.The search.P1/thing/--.P2finds the line containing `thing', and positions youtwo lines before it..SHRepeated Searches.PPSuppose you ask for the search.P1/horrible thing/.P2and when the line is printed you discover that itisn't the horrible thing that you wanted,so it is necessary to repeat the search again.You don't have to re-type the search,for the construction.P1//.P2is a shorthand for `the previous thing that was searched for',whatever it was.This can be repeated as many times as necessary.You can also go backwards:.P1??.P2searches for the same thing,but in the reverse direction..PPNot only can you repeat the search, but you canuse `//' as the left side of a substitute command,to mean`the most recent pattern'..P1/horrible thing/.ft I .... ed prints line with `horrible thing' ....ft Rs//good/p.P2To go backwards and change a line, say.P1??s//good/.P2Of course, you can still use the `&' on the right hand side of a substitute to stand forwhatever got matched:.P1//s//&\*B&/p.P2finds the next occurrence of whatever you searched for last,replaces it by two copies of itself,then prints the line just to verify that it worked..SHDefault Line Numbers and the Value of Dot.PPOne of the most effective ways to speed up your editingis always to know what lines will be affectedby a command if you don't specify the lines it is to act on,and on what line you will be positioned (i.e., the value of dot) when a command finishes.If you can edit without specifying unnecessaryline numbers, you can save a lot of typing..PPAs the most obvious example, if you issue a search commandlike.P1/thing/.P2you are left pointing at the next line that contains `thing'.Then no address is required with commands like.UL sto make a substitution on that line,or.UL pto print it,or.UL lto list it,or.UL dto delete it,or.UL ato append text after it,or.UL cto change it,or.UL ito insert text before it..PPWhat happens if there was no `thing'?Then you are left right where you were _dot is unchanged.This is also true if you were sittingon the only `thing' when you issued the command.The same rules hold for searches that use`?...?'; the only difference is the directionin which you search..PPThe delete command.UL d leaves dot pointingat the line that followed the last deleted line.When line `$' gets deleted,however,dot points at the.ulnewline `$'..PPThe line-changing commands.UL a ,.UL cand.UL iby default all affect the current line _if you give no line number with them,.UL aappends text after the current line,.UL cchanges the current line,and.UL iinserts text before the current line..PP.UL a ,.UL c ,and.UL ibehave identically in one respect _when you stop appending, changing or inserting,dot points at the last line entered.This is exactly what you want for typing and editing on the fly.For example, you can say.P1.ta 1.5ia ... text ... ... botch ... (minor error)\&\*.s/botch/correct/ (fix botched line)a ... more text ....P2without specifying any line number for the substitute command or forthe second append command.Or you can say.P1 2.ta 1.5ia ... text ... ... horrible botch ... (major error)\&\*.c (replace entire line) ... fixed up line ....P2.PPYou should experiment to determine what happens if you add.ulnolines with.UL a ,.UL cor.UL i ..PPThe.UL rcommand will read a file into the text being edited,either at the end if you give no address,or after the specified line if you do.In either case, dot points at the last line read in.Remember that you can even say.UL 0rto read a file in at the beginning of the text.(You can also say.UL 0aor.UL 1ito start adding text at the beginning.).PPThe.UL wcommand writes out the entire file.If you precede the command by one line number,that line is written,while if you precede it by two line numbers,that range of lines is written.The .UL wcommand does.ulnotchange dot:the current line remains the same,regardless of what lines are written.This is true even if you say something like.P1/^\*e\*.AB/,/^\*e\*.AE/w abstract.P2which involves a context search..PPSince the.UL wcommand is so easy to use,you should save what you are editing regularlyas you go alongjust in case the system crashes, or in case you do something foolish,like clobbering what you're editing..PPThe least intuitive behavior, in a sense, is that of the.UL scommand.The rule is simple _you are left sitting on the last line that got changed.If there were no changes, then dot is unchanged..PPTo illustrate,suppose that there are three lines in the buffer, and you are sitting onthe middle one:.P1x1x2x3.P2Then the command.P1\&-,+s/x/y/p.P2prints the third line, which is the last one changed.But if the three lines had been.P1x1y2y3.P2and the same command had been issued whiledot pointedat the second line, then the resultwould be to change and print only the first line,and that is where dot would be set..SHSemicolon `;'.PPSearches with `/.../' and `?...?' startat the current line and moveforward or backward respectivelyuntil they either find the pattern or get back to the current line.Sometimes this is not what is wanted.Suppose, for example, that the buffer contains lines like this:.P1 \*. \*. \*. ab \*. \*. \*. bc \*. \*..P2Starting at line 1, one would expect that the command.P1/a/,/b/p.P2prints all the lines from the `ab' to the `bc' inclusive.Actually this is not what happens..ulBothsearches(for `a' and for `b')start from the same point, and thus they both find the linethat contains `ab'.The result is to print a single line.Worse, if there had been a line with a `b' in itbefore the `ab' line, then the print commandwould be in error, since the second line numberwould be less than the first, and it is illegal totry to print lines in reverse order..PPThis is because the comma separatorfor line numbers doesn't set dot as each address is processed;each search starts from the same place.In .UL ed ,the semicolon `;' can be used just like comma,with the single difference that use of a semicolonforces dot to be set at that pointas the line numbers are being evaluated.In effect, the semicolon `moves' dot.Thus in our example above, the command.P1/a/;/b/p.P2prints the range of lines from `ab' to `bc',because after the `a' is found, dot is set to that line,and then `b' is searched for, starting beyond that line..PPThis property is most often useful in a very simple situation.Suppose you want to find the .ulsecondoccurrence of `thing'.You could say.P1/thing///.P2but this prints the first occurrence as well as the second,and is a nuisance when you know very well that it is onlythe second one you're interested in.The solution is to say.P1/thing/;//.P2This says to find the first occurrence of `thing', set dot to that line, then find the secondand print only that..PPClosely related is searching for the second previousoccurrence of something, as in.P1?something?;??.P2Printing the third or fourth or ...in either direction is left as an exercise..PPFinally, bear in mind that if you want to find the first occurrence ofsomething in a file, starting at an arbitrary place within the file,it is not sufficient to say.P11;/thing/.P2because this fails if `thing' occurs on line 1.But it is possible to say.P10;/thing/.P2(one of the few places where 0 is a legal line number),for this starts the search at line 1..SHInterrupting the Editor.PPAs a final note on what dot gets set to,you should be aware that if you hit the interrupt or deleteor rubout or break keywhile.UL edis doing a command, things are put back together again and your stateis restored as much as possible to what it was before the commandbegan.Naturally, some changes are irrevocable _if you are reading or writing a file or making substitutions or deleting lines, these will be stoppedin some clean but unpredictable state in the middle(which is why it is not usually wise to stop them).Dot may or may not be changed..PPPrinting is more clear cut.Dot is not changed until the printing is done.Thus if you print until you see an interesting line,then hit delete, you are.ulnotsitting on that line or even near it.Dot is left where it was when the.UL pcommand was started.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -