?? l6.2
字號:
#print Misc. Useful Features - The Outer Editor I You may remember that we said at the outset that vi is built on top ofanother, line-oriented, editor, called 'ex'. Whenever you use a coloncommand (e.g. :q, :w, :set), you are really accessing this outer editor.One of the most useful commands available in the outer editor is thesubstitution command. You can find out all about 'ex' by leaving learnwith 'bye' (when you see the percent sign) and then typing 'learn editor'.The substitution command is for those times when you want to make a changeseveral times in the file without having to move to each one in turn.Suppose we wanted to change all occurances of 'masticate' to 'chew'. We'duse this command: :1,$s/masticate/chew/gWhat does it mean? Well, ':' is just the command to move the cursor to thebottom of the screen on the command line. '1,$' means "do this on every lineof the file from line 1 to line $ (i.e. the end)". 's/masticate/chew/' means"if there's an instance of masticate on this line, change it to chew". Thefinal 'g' stands for global, meaning "do this as many times as possible on thisline". Otherwise, the s command only changes the first occurance on the line. In place of the '1,$' we could have used any two line numbers or thecharacter '.', which stands for the current line. ":.,.+5s/mast/pole/g" wouldchange all masts to poles in the next six lines. Type 'ready' to try out this handy command.#user#create Substitute You can learn all about the substitution command when you try'learn editor', but remember to leave your present session with 'bye'.Once you get the hang of it (it's rather crude at first in comparisonto vi), if you want to skip to the lessons on this command, type'again 30.1a' from within learn, and do the lessons as they are presented.To get a real understanding of substitutions you will need to do this atsome time, but you may want to get a glimpse of them here. The format of a substitute command is as follows: :<address>s/<pattern>/<replacement>/<options>where <address> is either of the form <line1>,<line2> or g/<pattern>/ In the first case, <line1> and <line2> can be any of 1. A line number 2. '$', meaning the last line in the file 3. '.', meaning the current line or 4. A sum of these, like .+5 The meaning of this type of address is all lines between <line1> and <line2>. In the second case, vi uses all lines of the file containing the text <pattern>. <pattern> is the text to look for <replacement> is the text to replace <pattern> withand <options> is any number of the following g, which makes the change happen to all occurances of <pattern> on the line c, which makes vi confirm each change before doing it or p, which makes vi print out each changed line as it goesExamples: :1,$s/masticate/chew/g For all lines in the file, changes every occurance of 'masticate' on each line to 'chew'. :1,$s/masticate/chew/ Same as the previous one, but only changes the first 'masticate' on each line. :g/fundamental/s/fundamental/basic/cp Finds every line in which the word 'fundamental' occurs and changes it to 'basic' after first asking for confirmation. In this mode, vi will print the line in which the pattern occurs, underline the pattern, and wait. If you type 'y', the change will be made. Any other character causes the change not to be done. Because of the 'p' at the end of the command above, vi will print out the line each time after it makes a substitution.Play around with this command for a while and then try this exercise:-----------------------------------------------------------------------------EXERCISE Compose and execute a command which will change every instance of the word 'rogue' in this file to the word 'rouge'. Make sure you even get the instance in these instructions. Then exit, saving your changes.-----------------------------------------------------------------------------#create Empty#cat /usr/lib/learn/vi/longtext >> Substitutevi Substitutegrep rogue Substitute > Answer#cmp Answer Empty#succeedYou should be proud of yourself for learning this complicated command evenwith very muddled explanations... Sorry...#failTry it again. The kind of command you're looking for looks like this: :1,$s/rogue/rouge/gIf you forgot the 'g' on the end, it won't work, since vi will only changethe first instance of 'rogue' on each line and there are lines with twoinstances.When the learn program types a percent sign, type 'ready' to try it again.#next6.3 10
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -