📄 vi.beginner
字号:
Section 13:Up until this point, the tutorial has always tried to make sure that the firstline of each screen has on it the section number and a list of the commandscovered in that section. This will no longer be strictly maintained. If youwant the section line at the top of the screen, you now know enough commands todo it easily: do {^M} or {-} until the cursor is on the section line andthen {z^M}. Also, from this point on, it may not be the case that a {^F} willput you at the beginning of the next section. Therefore, be aware of where youare in the file as we look at other commands. You may have to find your wayback to a particular section without any help from the tutorial. If you do notfeel comfortable with this, then it is suggested that you practice moving fromsection 1 to section 13, back and forth, using {^M}, {-}, {^F}, and {^B}commands for a while.Also make liberal use of the mark command {m}: if, for example, you make ahabit of using {mz} to mark your current location in the file, then you willalways be able to return to that location with {'z} if the editor doessomething strange and you have no idea where you are or what happened.And finally, the proscription against experimentation is hereby lifted: playwith the editor. Feel free to try out variations on the commands and movearound in the file. By this time you should be able to recover from any grosserrors.Section 14: {^E} {^Y} {^D} {^U}Let us now look at a few other commands for moving around in the file, andmoving the file around on the screen. Note that the commands we have alreadylooked at are sufficient: you really don't need any more commands for lookingin a file. The following commands are not absolutely necessary. However,they can make editing more convenient, and you should take note of theirexistence. But it would be perfectly valid to decide to ignore them on thisfirst pass: you can learn them later when you see a need for them, if you everdo.First, let's clear up some potentially confusing language. In at least oneplace in the official document ('An Introduction to Display Editing with Vi'by William Joy, and Mark Horton, September 1980), the expression "to scrolldown text" means that the cursor is moved down in your file. However, notethat this may result in the text on the screen moving UP. This use of theword 'scroll' refers to the action of the cursor within the file. However,another legitimate use of the word refers to the action of the text on thescreen. That is, if the lines on your screen move up toward the top of thescreen, this would be 'scrolling the screen up'. If the lines move downtoward the bottom of the screen, this would be refered to as scrolling down.I have tried to maintain the following jargon: 'scrolling' refers to what thetext does on the screen, not to what the cursor does within the file. For thelatter I will refer to the cursor 'moving', or to 'moving the cursor'. Irealize that this is not necessarily consistent with Joy and Horton, but theywere wrong.{^E} scrolls the whole screen up one line, keeping the cursor on the same line,if possible. However, if the cursor line is the first line on the screen, thenthe cursor is moved to the next line in the file. Try typing {^E}.{^Y} scrolls the screen down one line, keeping the cursor on the same line, ifpossible. However, if the cursor line is the last line on the screen, then thecursor is moved to the previous line in the file. Try it.{^D} moves the cursor down into the file, scrolling the screen up.{^U} moves the cursor up into the file, also scrolling the screen if theterminal you are on has the reverse scroll capability. Otherwise thescreen is repainted.Note that {^E} and {^Y} move the cursor on the screen while trying to keep thecursor at the same place in the file (if possible: however, the cursor cannever move off screen), while {^D} and {^U} keep the cursor at the same placeon the screen while moving the cursor within the file.Section 15: {/ .. /^M}Another way to position yourself in the file is by giving the editor a stringto search for. Type the following: {/Here 1/^M} and the cursor should end upright ...........................here ^. Now type {/Section 15:/^M} and thecursor will end up over/on .....................here ^. Now type {//^M} andobserve that the cursor is now over the capital S five lines above this line.Typing {//^M} several more times will bounce the cursor back and forth betweenthe two occurrences of the string. In other words, when you type a stringbetween the two slashes, it is searched for. Typing the slashes with nothingbetween them acts as if you had typed the previous string again.Observe that the string you type between the two slashes is entered on thebottom line of the screen. Now type {/Search for x /^M} except replace the 'x'in the string with some other character, say 'b'. The message "Pattern notfound" should appear on the bottom of the screen. If you hadn't replaced the'x', then you would have found the string. Try it.Section 16: {? .. ?^M} {n} (search strings: ^ $)When you surround the sought-for string with slashes as in {/Search/}, thefile is searched beginning from your current position in the file. If thestring is not found by the end of the file, searching is restarted at thebeginning of the file. However, if you do want the search to find thePREVIOUS rather than the NEXT occurrence of the string, surround the stringwith question marks instead of slash marks.Below are several occurrences of the same string. Here 2 Here 2 Here 2 Here 2 Here 2.Observe the effect of the following search commands (try them in thesequence shown):{/Here 2/^M} {//^M} {??^M}{/^Here 2/^M} {//^M} {??^M}{/Here 2$/^M} {//^M} {??^M}The first command looks for the next occurrence of the string 'Here 2'.However the second line of commands looks for an occurrence of 'Here 2' thatis at the beginning of the line. When the up-arrow is the first character ofa search string it stands for the beginning of the line. When the dollar-signis the last character of the search string it stands for the end of the line.Therefore, the third line of commands searches for the string only when it isat the end of the line. Since there is only one place the string begins aline, and only one place the string ends the line, subsequent {//^M} and{??^M} will find those same strings over and over.The {n} command will find the next occurrence of the / or ? searchstring. Try {/Here 2/^M} followed by several {n} and observe theeffect. Then try {??^M} followed by several {n}. The {n} commandremembers the direction of the last search. It is just a way to save afew keystrokes.Section 17: \ and magic-characters in search stringsNow type {/Here 3$/^M}. You might expect the cursor to end upright......^ here. However, you will get "Pattern not found" at the bottom ofthe screen. Remember that the dollar-sign stands for the end of the line.Somehow, you must tell vi that you do not want the end of the line, but adollar-sign. In other words, you must take away the special meaning that thedollar-sign has for the search mechanism. You do this (for any specialcharacter, including the up-arrow ^) by putting a back-slash ('\', not '/') infront of the character.Now try {/Here 3\$/^M} and you should end up nine lines above this one. Try{//^M} and note that it returns you to the same place, and not to the firstline of this paragraph: the back-slash character is not part of the searchstring and will not be found. To find the string in the first line of thisparagraph, type {/Here 3\\\$/^M}. There are three back-slashes: the first takesaway the special meaning from the second, and the third takes away the specialmeaning from the dollar-sign.Following is a list of the characters that have special meanings in searchstrings. If you wish to find a string containing one of these characters, youwill have to be precede the character with a backslash. These characters arecalled magic characters because of the fun and games you can have with themand they can have with you, if you aren't aware of what they do. ^ - (up-arrow) beginning of a line $ - (dollar-sign) end of a line . - (period) matches any character \ - (backslant) the escape character itself [ - (square bracket) for finding patterns (see section #SEARCH) ] - (square bracket) ditto * - (asterisk) dittoWithout trying to explain it here, note that {:set nomagic^M} turns off thespecial meanings of all but the ^ up-arrow, $ dollar-sign, and backslashcharacters.Section 18: {: (colon commands)} {ZZ}In this section we will discuss getting into and out of the editor in moredetail. If you are editing a file and wish to save the results the commandsequence {:w^M} writes the current contents of the file out to disk, using thefile name you used when you invoked the editor. That is, if you are at thecommand level in Unix, and you invoke vi with {vi foo} where foo is the nameof the file you wish to edit, then foo is the name of the file used by the{:w^M} command.If you are done, the write and quit commands can be combined into a singlecommand {:wq^M}. An even simpler way is the command {ZZ} (two capital Z's).If, for some reason, you wish to exit without saving any changes you have made,{:q!^M} does the trick. If you have not made any changes, the exclamationpoint is not necessary: {:q^M}. Vi is pretty good about not letting youget out without warning you that you haven't saved your file.We have mentioned before that you are currently in the vi editor, editing afile. If you wish to start the tutorial over from the very beginning, youcould {ZZ}, and then type {vi.tut beginner} in response to the Unix prompt.This will create a fresh copy of this file for you, which might be necessary if you accidentally destroyed the copy you were working with. Just do a search for the last section you were in: e.g. {/Section 18:/^Mz^M}.Section 19: {H} {M} {L}Here are a few more commands that will move you around on the screen. Again,they are not absolutely necessary, but they can make screen positioning easier:{H} - puts the cursor at the top of the screen (the 'home' position){M} - puts the cursor in the middle of the screen{L} - puts the cursor at the bottom of the screen.Try typing {HML} and watch the cursor.Try typing {5HM5L} and note that 5H puts you five lines from the top of thescreen, and 5L puts you five lines from the bottom of the screen.Section 20: {w} {b} {0} {W} {B} {e} {E} {'} {`}Up to this point we have concentrated on positioning in the file, andpositioning on the screen. Now let's look at positioning in a line. Put thecursor at the beginning of the following line and type {z^M}:This is a test line: your cursor should initially be at its beginning.The test line should now be at the top of your screen. Type {w} several times.Note that it moves you forward to the beginning of the next word. Now type{b} (back to the beginning of the word) several times till you are at thebeginning of the line. (If you accidentally type too many {b}, type {w} untilyou are on the beginning of the line again.) Type {wwwww} (five w's) and notethat the cursor is now on the colon in the sentence. The lower-case w commandmoves you forward one word, paying attention to certain characters such ascolon and period as delimiters and counting them as words themselves. Nowtype {0} (zero, not o 'oh'): this moves you to the beginning of the currentline. Now type {5w} and notice that this has the effect of repeating {w} fivetimes and that you are now back on the colon. Type {0} (zero) again. Toignore the delimiters and to move to the beginning of the next word using onlyblanks, tabs and carriage-returns (these are called white-space characters) todelimit the words, use the {W} command: upper-case W. {B} takes you back aword using white-space characters as word delimiters.Note that the commands {wbWB} do not stop at the beginning or end of a line:they will continue to the next word on the next line in the direction specified(a blank line counts as a word).If you are interested in the END of the word, and not the BEGINNING, then usethe {e} and {E} commands. These commands only move forward and there are nocorresponding 'reverse search' commands for the end of a word.Also, we have been using the {'} command to move the cursor to a position thatwe have previously marked with the {m} command. However, position the cursorin the middle of a line (any line, just pick one) and type {mk}, marking thatposition with the letter k. Now type a few returns {^M} and type {'k}.Observe that the cursor is now at the beginning of the line that you marked.Now try {`k}: note that this is the reverse apostrophe, or back-quote, or graveaccent, or whatever you want to call it. Also note that it moves you to thecharacter that was marked, not just to the line that was marked.In addition, the {``} command works just like the {''} command except that youare taken to the exact character, not just to the line. (I'm still notsure which exact character, just as I'm still not sure which line.)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -