📄 e4
字号:
.H1Exercise 5:.H2.PGExperiment with the substitute command.See what happens if yousubstitute for some word on a line with several occurrences of that word.For example, do this:.X1athe other side of the coin.li\fB.\fRs/the/on the/p.X2You will get.X1on the other side of the coin.X2A substitute command changes only the first occurrence of the first string.You can change all occurrences by adding a ``g'' (for ``global'')to the ``s'' command, like this:.X1s/ . . . / . . . /gp.X2Try other characters instead of slashes to delimit the two setsof characters in the ``s'' command \(mi anything should workexcept blanks or tabs..PG(If you get funny results using any of the characters.X1^ \*. $ [ * \\.X2read the section on ``Special Characters''.).H1Context searching \(mi ``/ . . . /''.H2.PGWith the substitute command mastered, we can move on toanother highly important idea of.uled\(mi context searching..PGSuppose we have our original three line text in the buffer:.X1Now is the timefor all good mento come to the aid of their party..X2Suppose we want to find the line that contains ``their'' sowe can change it to ``the''.Now with only three lines in the buffer, it's pretty easyto keep track of what line the word ``their'' is on.But if the buffer contained several hundred lines,and we'd been making changes, deleting and rearranging lines,and so on, we would no longer really know what this linenumber would be.Context searching is simply a method of specifying the desired line,regardless of what its number is,by specifying some context on it..PGThe way we say ``search for a linethat contains this particular string of characters''is to type.X1/\fIstring of characters we want to find\fP/.X2For example,the.uledline.X1/their/.X2is a context search whichis sufficient to find the desired line \(miit will locate the next occurrence ofthe characters between slashes (``their'').It also sets dot to that lineand prints the line for verification:.X1to come to the aid of their party..X2``Next occurrence'' means that.uledstarts looking for the string at line ``\*.+1'',searches to the end of the buffer,then continues at line 1 and searches to line dot.(That is, the search ``wraps around'' from ``$'' to 1.) It scans all the lines in the buffer until it either finds the desired lineor gets back to dot again.If the given string of characters can't be found in any line,.uledtypes the error message.X1?.X2Otherwise it prints the line it found..PGWe can do both the search for the desired line.ulandasubstitution all at once, like this:.X1/their/s/their/the/p.X2which will yield.X1to come to the aid of the party..X2There were three parts to that last command:context search for the desired line, make the substitution, print the line..PGThe expression ``/their/'' is a context search expression.In their simplest form,all context search expressions are like this \(mia string of characters surrounded by slashes.Context searches are interchangeable with line numbers,so they can be used by themselves to find and print a desired line,or as line numbers for some other command, like ``s''.We used them both ways in the examples above..PGSuppose the buffer contains the three familiar lines.X1Now is the timefor all good mento come to the aid of their party..X2Then the.uledline numbers.X1/Now/+1/good//party/\(mi1.X2are all context search expressions, and they all referto the same line (line 2).To make a change in line 2,we could say.X1/Now/+1s/good/bad/.X2or.X1/good/s/good/bad/.X2or.X1/party/\(mi1s/good/bad/.X2The choice is dictated only by convenience.We could print all three lines by, for instance.X1/Now/,/party/p.X2or.X1/Now/,/Now/+2p.X2or by any number of similar combinations.The first one of these might be better if we don'tknow how many lines are involved.(Of course, if there were only three lines in the buffer,we'd use.X11,$p.X2but not if there were several hundred.).PGThe basic rule is: a context search expression is.ulthe same asa line number, so it can be used wherever a line number is needed..H1Exercise 6:.H2.PGExperiment with context searching.Try a body of text withseveral occurrencesof the same string of characters, and scan through it usingthe same context search..PGTry using context searches as line numbers for thesubstitute, print and delete commands.(They can also be usedwith ``r'', ``w'', and ``a''.).PGTry context searching using ``?text?'' instead of ``/text/''.This scans lines in the buffer in reverse orderrather than normal.This issometimes useful if you go too far while looking for somestring of characters \(mi it's an easy way to back up..PG(If you get funny results with any of the characters.X1^ \*. $ [ * \\.X2read the section on ``Special Characters''.).PG.ulEdprovides a shorthand for repeating a context searchfor the same string.For example,the.uledline number.X1/string/.X2will find the next occurrence of ``string''.It often happens that this is not the desired line,so the search must be repeated.This can be done by typing merely.X1//.X2This shorthand stands for ``the most recently usedcontext search expression.''It canalso be used as the first string of the substitutecommand, as in.X1/string1/s//string2/.X2which will find the next occurrence of ``string1''and replace it by ``string2''.This can save a lot of typing.Similarly.X1??.X2means ``scan backwards for the same expression.''
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -