📄 vi.beginner
字号:
Section 1: {^F} {ZZ}To get out of this tutorial, type: ZZ (two capital Z's).Learning a new computer system implies learning a new text editor. Thesetutorial lessons were created by Dain Samples to help you come to grips withUC Berkeley's screen oriented editor called vi (for VIsual). This tutorialuses the vi editor itself as the means of presentation. For best use of this tutorial, read all of a screen before performing any ofthe indicated actions. This tutorial (or, at least, the first half of it) hasbeen designed to systematically present the vi commands IF THE INSTRUCTIONSARE FOLLOWED! If you are too adventuresome, you may find yourself lost. Ifyou ever find yourself stuck, remember the first line of this section.OK, now find the control key on your keyboard; it usually has CTL or CTRLwritten on its upper surface. Your first assignment is to hold the controlkey down while you press the 'F' key on your keyboard. Please do so now.Section 2: {^F} {^B}Many of vi's commands use the control key and some other key in combination,as with the control and the 'F' key above. This is abbreviated CTL-F, or ^F.As you have probably guessed by now, ^F (CTL-F) moves you forward a fixednumber of lines in the file. Throughout the remainder of the tutorial whenyou are ready to advance to the next section of text, hit ^F.The opposite command is ^B. Just for fun, you might want to try a ^B to seethe previous section again. Be sure to do a ^F to return you here.Determine what the cursor looks like on your screen. Whatever it is (a box,an underscore, blinking, flashing, inverse, etc.) it should now be positionedin the upper left-hand corner of your screen under or on the S of Section.Become familiar with your cursor: to use vi correctly it is important toalways know where the cursor is.Did you notice that when you do a ^F the cursor is left at the top of thescreen, and a ^B leaves the cursor near the bottom of the screen? Try the twocommands ^B^F again. And now do another ^F to see the next section.Section 3: {^F} {^B}You now have two basic commands for examining a file, both forwards (^F) andbackwards (^B).Note that these are vi text editing commands: they are not commands for thetutorial. Indeed, this tutorial is nothing but a text file which you are nowediting. Everything you do and learn in this tutorial will be applicable toediting text files.Therefore, when you are editing a file and are ready to see more of the text,entering ^F will get you to the next section of the file. Entering ^B willshow you the previous section.Time for you to do another ^F.Section 4: {^F} {^B} {^M} (return key)We will adopt the notation of putting commands in curly braces so we can writethem unambiguously. For example, if you are to type the command sequence"control B control F" (as we asked you to do above) it would appear as {^B^F}.This allows clear delineation of the command strings from the text. Rememberthat the curly braces are NOT part of the command string you are to type. DoNOT type the curly braces. Sometimes, the command string in the curly braces will be rather long, and maybe such that the first couple of characters of the command will erase fromthe screen the string you are trying to read and type. It is suggested thatyou write down the longer commands BEFORE you type them so you won't forgetthem once they disappear.Now locate the return key on your keyboard: it is usually marked 'RETURN',indicate hitting the return key. In fact, the control-M key sequence isexactly the same as if you hit the return key, and vice versa.Now type {^F}.Section 5: {:q!} {ZZ} {^M} (return key)Recognize that this tutorial is nothing more than a text file that youare editing. This means that if you do something wrong, it is possiblefor you to destroy the information in this file. Don't worry. If thishappens, type {ZZ} (two capital Z's) or {:q!^M} to leave the tutorial.Restart the tutorial. Once in the tutorial, you can then page forwardwith {^F} until you are back to where you want to be. (There areeasier ways to do this, some of which will be discussed later, but thisis the most straightforward.)You may want to write these commands down in a convenient place for quickreference: {:q!^M} and {ZZ}We will assume that you now know to do a {^F} to advance the fileSection 6: {m} {G} {'} {z}Now that you know how to get around in the file via ^F and ^B let's look atother ways of examining a text file. Sometimes it is necessary, in the midstof editing a file, to examine another part of the file. You are then facedwith the problem of remembering your place in the file, looking at the othertext, and then getting back to your original location. Vi has a 'mark'command, m. Type {mp}. You have just 'marked' your current location in thefile and given it the name 'p'. The command string below will do threethings: position you at the beginning of the file (line 1), then return you tothe location 'p' that you just marked with the 'm' command, and, since thescreen will not look exactly the same as it does right now, the 'z' commandwill reposition the screen. (You may want to write the string down beforetyping it: once you type {1G} it will no longer be on the screen.)So now type {1G'pz^M} - a one followed by a capital G, followed by the quotemark, followed by a lower case 'p', then a lower case 'z', then a return(which is the same as a ^M). The {1G} moves you to line 1, i.e. the beginningof the file. The {'p} moves you to the location you marked with {mp}. The{z^M} command will repaint the screen putting the cursor at the top of thescreen. (Now {^F}.)Section 7: {m} {G} {'} {z}Let's look at some variations on those commands. If you wanted to look atline 22 in the file and return to this location you could type {mp22G'p}. Doso now, observing that {22G} puts your cursor at the beginning of section 2 inthe middle of the screen.Also note that, without the {z^M} command, the line with 'Section 7' on it isnow in the MIDDLE of the screen, and not at the top. Our cursor is on thecorrect line (where we did the {mp} command) but the line is not where wemight like it to be on the screen. That is the function of the {z^M} command.(Remember, ^M is the same as the 'return' key on your keyboard.) Type {z^M}now and observe the effect.As you can see, the 'Section 7' line is now at the top of the screen with thecursor happily under the capital S. If you would like the cursor line (i.e.the line which the cursor is on) in the middle of the screen again, you wouldtype {z.}. If you wanted the cursor line to be at the BOTTOM of the screen,type {z-}. Try typing {z-z.z^M} and watch what happens.{^F}Section 8: {z} {m} {'}Note that the z command does not change the position of our cursor in the fileitself, it simply moves the cursor around on the screen by moving the contentsof the file around on the screen. The cursor stays on the same line of thefile when using the z command.This brings up an important point. There are two questions that the users ofvi continually need to know the answer to: "Where am I in the file?" and"Where am I on the screen?" The cursor on your terminal shows the answer toboth questions. Some commands will move you around in the file, usuallychanging the location of the cursor on the screen as well. Other commandsmove the cursor around on the screen without changing your location in thefile.Now type {ma}. Your location in the file has been given the name 'a'. If youtype {'p'a} you will see the previous location we marked in section 7, andthen will be returned to the current location. (You will want to do a {z^M}to repaint the screen afterwards.) Try it. {^F}Section 9: {m} {''}Now we can move about in our file pretty freely. By using the {m} command wecan give the current cursor position a lower-case-character name, like 'p','a', 'e', 'm', or 'b'. Using the {G} command preceded by a line number we canlook at any line in the file we like. Using the single quote command {'}followed by a character used in an {m} command, we can return to any locationin the file we have marked.However, try {m3}, or {mM}. You should hear a beep, or bell. Only lower-caseletters are acceptable to the {m} and {'} commands: numbers, upper-caseletters, and special characters are not acceptable.If you type the {'} command with a character that is lower-case alphabetic butthat has not been used in an {m} command, or for which the 'marked' text hasbeen deleted, you will also get a beep. Try {'i}. You should get a beepbecause the command {mi} has never been issued. (Unless you've beenexperimenting.)The command {''} attempts to return you to the location at which you lastmodified some part of your file. However, my experience has been that it isdifficult to predict exactly where you will end up. Section 10: {^M} {-}Now do {ma}, marking your position at the top of the screen. Now hit {^M} (orreturn) until the cursor is right ... * <- here, over/under the asterisk. Nowtype {mb'a'b} and watch the cursor move from the asterisk to the top of thescreen and back again.The {^M} command moves the cursor to the beginning of the next line. Now type{^M} until the cursor is right ...* <- here. The command to move the cursor to the beginning of theprevious line is {-}. Practice moving the cursor around on the screen by using{^M} and {-}. BE CAREFUL to not move the cursor OFF the screen just yet. Ifyou do, type {'az^M}.Now we can move to any line within the screen. Practice moving around in thefile using the {^F}, {^B}, {-}, {^M}, {z}, and {'} commands. When you arefairly confident that you can get to where you need to be in the file, andposition the cursor on the screen where you want it type {'az^M^F} (which, ofcourse, moves you back to the beginning of this section, repositions thecursor at the top of the screen, and advances you to the next section).Section 11: scrolling: {^M}The cursor should now be on the S of 'Section 11', and this should be on thefirst line of the screen. If it is not, do {^M} or {-} as appropriate to putthe cursor on the section line, and type {z^M}.Type {mc} to mark your place.Now type {^M} until the cursor is on the last line of this screen. Now do onemore {^M} and observe the result. This is called scrolling. When youattempted to move to a line not displayed on the screen, the line at the top ofthe screen was 'scrolled off', and a line at the bottom of the screen was'scrolled on'. The top line with 'Section 11' should no longer be visible.Now type {'cz^M} to reset the screen and type {^F} for the next section.Section 12: {-} {z}The {-} command moves the cursor to the previous line in the file. Now type{-}, which attempts to move the cursor to the previous line in this file.However, that line is not on the screen. The resulting action will depend onyour terminal. (Do a {^Mz^M} to reposition the file). On intelligentterminals (e.g. VT100s, Z19s, Concept 100s), a top line is 'scrolled on' andthe bottom line is 'scrolled off'. Other terminals, however, may not havethis 'reverse scrolling' feature. They will simply repaint the screen withthe cursor line in the middle of the screen. On such terminals it isnecessary to type {z^M} to get the cursor line back to the top of the screen.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -