📄 ae2
字号:
.NHSPECIAL CHARACTERS.PPThe editor.UL edis the primary interface to the systemfor many people, soit is worthwhile to knowhow to get the most out of.UL edfor the least effort..PPThe next few sections will discussshortcutsand labor-saving devices.Not all of these will be instantly usefulto any one person, of course,but a few will be,and the others should give you ideas to storeaway for future use.And as always,until you try these things,they will remain theoretical knowledge,not something you have confidence in..SHThe List command `l'.PP.UL edprovides two commands for printing the contents of the linesyou're editing.Most people are familiar with.UL p ,in combinations like.P11,$p.P2to print all the lines you're editing,or.P1s/abc/def/p.P2to change `abc'to`def'on the current line.Less familiar is the.ullistcommand.UL l(the letter `\fIl\|\fR'),which gives slightly more information than.UL p .In particular,.UL lmakes visible characters that are normally invisible,such as tabs and backspaces.If you list a line that contains some of these,.UL lwill print each tab as.UL \z\(mi>and each backspace as.UL \z\(mi< .This makes it much easier to correct the sort of typing mistakethat inserts extra spaces adjacent to tabs,or inserts a backspace followed by a space..PPThe.UL lcommandalso `folds' long lines for printing _any line that exceeds 72 characters is printed on multiple lines;each printed line except the last is terminated by a backslash .UL \*e ,so you can tell it was folded.This is useful for printing long lines on short terminals..PPOccasionally the.UL lcommand will print in a line a string of numbers preceded by a backslash,such as \*e07 or \*e16.These combinations are used to make visible characters that normally don't print,like form feed or vertical tab or bell.Each such combination is a single character.When you see such characters, be wary _they may have surprising meanings when printed on some terminals.Often their presence means that your finger slipped while you were typing;you almost never want them..SHThe Substitute Command `s'.PPMost of the next few sections will be taken up with a discussionof thesubstitutecommand.UL s .Since this is the command for changing the contents of individuallines,it probably has the most complexity of any .UL ed command,and the most potential for effective use..PPAs the simplest place to begin,recall the meaning of a trailing.UL gafter a substitute command.With.P1s/this/that/.P2and.P1s/this/that/g.P2thefirstone replaces the.ulfirst`this' on the linewith `that'.If there is more than one `this' on the line,the second formwith the trailing.UL gchanges.ulallof them..PPEither form of the.UL scommand can be followed by.UL por.UL lto `print' or `list' (as described in the previous section)the contents of the line:.P1s/this/that/ps/this/that/ls/this/that/gps/this/that/gl.P2are all legal, and mean slightly different things.Make sure you know what the differences are..PPOf course, any.UL scommand can be preceded by one or two `line numbers'to specify that the substitution is to take placeon a group of lines. Thus.P11,$s/mispell/misspell/.P2changes the .ulfirstoccurrence of`mispell' to `misspell' on every line of the file.But.P11,$s/mispell/misspell/g.P2changes .uleveryoccurrence in every line(and this is more likely to be what you wanted in thisparticular case)..PPYou should also notice that if you add a.UL por.UL lto the end of any of these substitute commands,only the last line that got changed will be printed,not all the lines.We will talk later about how to print all the linesthat were modified..SHThe Undo Command `u'.PPOccasionally you will make a substitution in a line,only to realize too late that it was a ghastly mistake.The `undo' command.UL ulets you `undo' the last substitution:the last line that was substituted can be restored toits previous state by typing the command.P1u.P2.SHThe Metacharacter `\*.'.PPAs you have undoubtedly noticedwhen you use.UL ed ,certain characters have unexpected meaningswhen they occur in the left side of a substitute command,or in a search for a particular line.In the next several sections, we will talk aboutthese special characters,which are often called `metacharacters'..PPThe first one is the period `\*.'.On the left side of a substitute command,or in a search with `/.../',`\*.' stands for.ulanysingle character.Thus the search.P1/x\*.y/.P2finds any line where `x' and `y' occur separated bya single character, as in.P1x+yx\-yx\*Byx\*.y.P2and so on.(We will use \*B to stand for a space whenever we need tomake it visible.).PPSince `\*.' matches a single character,that gives you a way to deal with funny charactersprinted by.UL l .Suppose you have a line that, when printed with the.UL lcommand, appears as.P1 .... th\*e07is .....P2and you want to get rid of the \*e07(which represents the bell character, by the way)..PPThe most obvious solution is to try.P1s/\*e07//.P2but this will fail. (Try it.)The brute force solution, which most people would now take,is to re-type the entire line.This is guaranteed, and is actually quite a reasonable tacticif the line in question isn't too big,but for a very long line,re-typing is a bore.This is where the metacharacter `\*.' comes in handy.Since `\*e07' really represents a single character,if we say.P1s/th\*.is/this/.P2the job is done.The `\*.' matches the mysterious character between the `h' and the `i',.ulwhatever it is..PPBear in mind that since `\*.' matches any single character,the command.P1s/\*./,/.P2converts the first character on a line into a `,',which very often is not what you intended..PPAs is true of many characters in.UL ed ,the `\*.' has several meanings, dependingon its context.This line shows all three:.P1\&\*.s/\*./\*./.P2The first `\*.' is a line number,the number ofthe line we are editing,which is called `line dot'.(We will discuss line dot more in Section 3.)The second `\*.' is a metacharacterthat matches any single character on that line.The third `\*.' is the only one that really isan honest literal period.On the.ulrightside of a substitution, `\*.'is not special.If you apply this command to the line.P1Now is the time\*..P2the result willbe.P1\&\*.ow is the time\*..P2which is probably not what you intended..SHThe Backslash `\*e'.PPSince a period means `any character',the question naturally arises of what to dowhen you really want a period.For example, how do you convert the line.P1Now is the time\*..P2into.P1Now is the time?.P2The backslash `\*e' does the job.A backslash turns off any special meaning that the next charactermight have; in particular,`\*e\*.' converts the `\*.' from a `match anything'into a period, soyou can use it to replacethe period in.P1Now is the time\*..P2like this:.P1s/\*e\*./?/.P2The pair of characters `\*e\*.' is considered by.UL edto be a single real period..PPThe backslash can also be used when searching for linesthat contain a special character.Suppose you are looking for a line that contains.P1\&\*.PP.P2The search.P1/\*.PP/.P2isn't adequate, for it will finda line like.P1THE APPLICATION OF ....P2because the `\*.' matches the letter `A'.But if you say.P1/\*e\*.PP/.P2you will find only lines that contain `\*.PP'..PPThe backslash can also be used to turn off special meanings forcharacters other than `\*.'.For example, consider finding a line that contains a backslash.The search.P1/\*e/.P2won't work,because the `\*e' isn't a literal `\*e', but instead means that the second `/'no longer \%delimits the search.But by preceding a backslash with another one,you can search for a literal backslash.Thus.P1/\*e\*e/.P2does work.Similarly, you can search for a forward slash `/' with.P1/\*e//.P2The backslash turns off the meaning of the immediately following `/' so thatit doesn't terminate the /.../ construction prematurely..PPAs an exercise, before reading further, find two substitute commands each of which willconvert the line.P1\*ex\*e\*.\*ey.P2into the line.P1\*ex\*ey.P2.PPHere are several solutions;verify that each works as advertised..P1s/\*e\*e\*e\*.//s/x\*.\*./x/s/\*.\*.y/y/.P2.PPA couple of miscellaneous notes aboutbackslashes and special characters.First, you can use any character to delimit the piecesof an.UL scommand: there is nothing sacred about slashes.(But you must use slashes for context searching.)For instance, in a line that contains a lot of slashes already, like.P1//exec //sys.fort.go // etc....P2you could use a colon as the delimiter _to delete all the slashes, type.P1s:/::g.P2.PPSecond, if # and @ are your character erase and line kill characters,you have to type \*e# and \*e@;this is true whether you're talking to.UL edor any other program..PPWhen you are adding text with.UL aor.UL ior.UL c ,backslash is not special, and you should only put inone backslash for each one you really want..SHThe Dollar Sign `$'.PPThe next metacharacter, the `$', stands for `the end of the line'.As its most obvious use, suppose you have the line.P1Now is the.P2and you wish to add the word `time' to the end.Use the $ like this:.P1s/$/\*Btime/.P2to get.P1Now is the time.P2Notice that a space is needed before `time' inthe substitute command,or you will get.P1Now is thetime.P2.PPAs another example, replace the second comma inthe following line with a period without altering the first:.P1Now is the time, for all good men,.P2The command needed is.P1s/,$/\*./.P2The $ sign here provides context to make specific which comma we mean.Without it, of course, the.UL scommand would operate on the first comma to produce.P1Now is the time\*. for all good men,.P2.PPAs another example, to convert.P1Now is the time\*..P2into.P1Now is the time?.P2as we did earlier, we can use.P1s/\*.$/?/.P2.PPLike `\*.', the `$'has multiple meanings depending on context.In the line.P1$s/$/$/.P2the first `$' refers to thelast line of the file,the second refers to the end of that line,and the third is a literal dollar sign,to be added to that line..SHThe Circumflex `^'.PPThe circumflex (or hat or caret)`^' stands for the beginning of the line.For example, suppose you are looking for a line that begins
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -