1Section 1: {^F} {ZZ} 2 3To get out of this tutorial, type: ZZ (two capital Z's). 4 5Learning a new computer system implies learning a new text editor. These 6tutorial lessons were created by Dain Samples to help you come to grips with 7UC Berkeley's screen oriented editor called vi (for VIsual). This tutorial 8uses the vi editor itself as the means of presentation. 9 10For best use of this tutorial, read all of a screen before performing any of 11the indicated actions. This tutorial (or, at least, the first half of it) has 12been designed to systematically present the vi commands IF THE INSTRUCTIONS 13ARE FOLLOWED! If you are too adventuresome, you may find yourself lost. If 14you ever find yourself stuck, remember the first line of this section. 15 16OK, now find the control key on your keyboard; it usually has CTL or CTRL 17written on its upper surface. Your first assignment is to hold the control 18key down while you press the 'F' key on your keyboard. Please do so now. 19 20 21 22Section 2: {^F} {^B} 23Many of vi's commands use the control key and some other key in combination, 24as with the control and the 'F' key above. This is abbreviated CTL-F, or ^F. 25 26As you have probably guessed by now, ^F (CTL-F) moves you forward a fixed 27number of lines in the file. Throughout the remainder of the tutorial when 28you are ready to advance to the next section of text, hit ^F. 29 30The opposite command is ^B. Just for fun, you might want to try a ^B to see 31the previous section again. Be sure to do a ^F to return you here. 32 33Determine what the cursor looks like on your screen. Whatever it is (a box, 34an underscore, blinking, flashing, inverse, etc.) it should now be positioned 35in the upper left-hand corner of your screen under or on the S of Section. 36Become familiar with your cursor: to use vi correctly it is important to 37always know where the cursor is. 38 39Did you notice that when you do a ^F the cursor is left at the top of the 40screen, and a ^B leaves the cursor near the bottom of the screen? Try the two 41commands ^B^F again. And now do another ^F to see the next section. 42 43Section 3: {^F} {^B} 44You now have two basic commands for examining a file, both forwards (^F) and 45backwards (^B). 46 47Note that these are vi text editing commands: they are not commands for the 48tutorial. Indeed, this tutorial is nothing but a text file which you are now 49editing. Everything you do and learn in this tutorial will be applicable to 50editing text files. 51 52Therefore, when you are editing a file and are ready to see more of the text, 53entering ^F will get you to the next section of the file. Entering ^B will 54show you the previous section. 55 56Time for you to do another ^F. 57 58 59 60 61 62 63 64Section 4: {^F} {^B} {^M} (return key) 65We will adopt the notation of putting commands in curly braces so we can write 66them unambiguously. For example, if you are to type the command sequence 67"control B control F" (as we asked you to do above) it would appear as {^B^F}. 68This allows clear delineation of the command strings from the text. Remember 69that the curly braces are NOT part of the command string you are to type. Do 70NOT type the curly braces. 71 72Sometimes, the command string in the curly braces will be rather long, and may 73be such that the first couple of characters of the command will erase from 74the screen the string you are trying to read and type. It is suggested that 75you write down the longer commands BEFORE you type them so you won't forget 76them once they disappear. 77 78Now locate the return key on your keyboard: it is usually marked 'RETURN', 79indicate hitting the return key. In fact, the control-M key sequence is 80exactly the same as if you hit the return key, and vice versa. 81 82Now type {^F}. 83 84 85Section 5: {:q!} {ZZ} {^M} (return key) 86Recognize that this tutorial is nothing more than a text file that you 87are editing. This means that if you do something wrong, it is possible 88for you to destroy the information in this file. Don't worry. If this 89happens, type {ZZ} (two capital Z's) or {:q!^M} to leave the tutorial. 90Restart the tutorial. Once in the tutorial, you can then page forward 91with {^F} until you are back to where you want to be. (There are 92easier ways to do this, some of which will be discussed later, but this 93is the most straightforward.) 94 95You may want to write these commands down in a convenient place for quick 96reference: {:q!^M} and {ZZ} 97 98We will assume that you now know to do a {^F} to advance the file 99 100 101 102 103 104 105 106Section 6: {m} {G} {'} {z} 107Now that you know how to get around in the file via ^F and ^B let's look at 108other ways of examining a text file. Sometimes it is necessary, in the midst 109of editing a file, to examine another part of the file. You are then faced 110with the problem of remembering your place in the file, looking at the other 111text, and then getting back to your original location. Vi has a 'mark' 112command, m. Type {mp}. You have just 'marked' your current location in the 113file and given it the name 'p'. The command string below will do three 114things: position you at the beginning of the file (line 1), then return you to 115the location 'p' that you just marked with the 'm' command, and, since the 116screen will not look exactly the same as it does right now, the 'z' command 117will reposition the screen. (You may want to write the string down before 118typing it: once you type {1G} it will no longer be on the screen.) 119 120So now type {1G'pz^M} - a one followed by a capital G, followed by the quote 121mark, followed by a lower case 'p', then a lower case 'z', then a return 122(which is the same as a ^M). The {1G} moves you to line 1, i.e. the beginning 123of the file. The {'p} moves you to the location you marked with {mp}. The 124{z^M} command will repaint the screen putting the cursor at the top of the 125screen. (Now {^F}.) 126 127Section 7: {m} {G} {'} {z} 128Let's look at some variations on those commands. If you wanted to look at 129line 22 in the file and return to this location you could type {mp22G'p}. Do 130so now, observing that {22G} puts your cursor at the beginning of section 2 in 131the middle of the screen. 132 133Also note that, without the {z^M} command, the line with 'Section 7' on it is 134now in the MIDDLE of the screen, and not at the top. Our cursor is on the 135correct line (where we did the {mp} command) but the line is not where we 136might like it to be on the screen. That is the function of the {z^M} command. 137(Remember, ^M is the same as the 'return' key on your keyboard.) Type {z^M} 138now and observe the effect. 139 140As you can see, the 'Section 7' line is now at the top of the screen with the 141cursor happily under the capital S. If you would like the cursor line (i.e. 142the line which the cursor is on) in the middle of the screen again, you would 143type {z.}. If you wanted the cursor line to be at the BOTTOM of the screen, 144type {z-}. Try typing {z-z.z^M} and watch what happens. 145 146{^F} 147 148Section 8: {z} {m} {'} 149 150Note that the z command does not change the position of our cursor in the file 151itself, it simply moves the cursor around on the screen by moving the contents 152of the file around on the screen. The cursor stays on the same line of the 153file when using the z command. 154 155This brings up an important point. There are two questions that the users of 156vi continually need to know the answer to: "Where am I in the file?" and 157"Where am I on the screen?" The cursor on your terminal shows the answer to 158both questions. Some commands will move you around in the file, usually 159changing the location of the cursor on the screen as well. Other commands 160move the cursor around on the screen without changing your location in the 161file. 162 163Now type {ma}. Your location in the file has been given the name 'a'. If you 164type {'p'a} you will see the previous location we marked in section 7, and 165then will be returned to the current location. (You will want to do a {z^M} 166to repaint the screen afterwards.) Try it. 167{^F} 168 169Section 9: {m} {''} 170Now we can move about in our file pretty freely. By using the {m} command we 171can give the current cursor position a lower-case-character name, like 'p', 172'a', 'e', 'm', or 'b'. Using the {G} command preceded by a line number we can 173look at any line in the file we like. Using the single quote command {'} 174followed by a character used in an {m} command, we can return to any location 175in the file we have marked. 176 177Try {m3}, {mM}, or {m$}. Not only lower-case letters are acceptable to the 178{m} and {'} commands: numbers, upper-case letters, and special characters are 179also acceptable. 180 181If you type the {'} command with a character that that has not been used in an 182{m} command, or for which the 'marked' text has been deleted, you will get a 183beep. Try {'i}. You should get a beep because the command {mi} has never 184been issued. (Unless you've been experimenting.) 185 186The command {''} attempts to return you to the location at which you last 187modified some part of your file. However, my experience has been that it is 188difficult to predict exactly where you will end up. 189 190Section 10: {^M} {-} 191Now do {ma}, marking your position at the top of the screen. Now hit {^M} (or 192return) until the cursor is right ... 193* <- here, over/under the asterisk. Now 194type {mb'a'b} and watch the cursor move from the asterisk to the top of the 195screen and back again. 196 197The {^M} command moves the cursor to the beginning of the next line. Now type 198{^M} until the cursor is right ... 199* <- here. The command to move the cursor to the beginning of the 200previous line is {-}. Practice moving the cursor around on the screen by using 201{^M} and {-}. BE CAREFUL to not move the cursor OFF the screen just yet. If 202you do, type {'az^M}. 203 204Now we can move to any line within the screen. Practice moving around in the 205file using the {^F}, {^B}, {-}, {^M}, {z}, and {'} commands. When you are 206fairly confident that you can get to where you need to be in the file, and 207position the cursor on the screen where you want it type {'az^M^F} (which, of 208course, moves you back to the beginning of this section, repositions the 209cursor at the top of the screen, and advances you to the next section). 210 211Section 11: scrolling: {^M} 212The cursor should now be on the S of 'Section 11', and this should be on the 213first line of the screen. If it is not, do {^M} or {-} as appropriate to put 214the cursor on the section line, and type {z^M}. 215 216Type {mc} to mark your place. 217 218Now type {^M} until the cursor is on the last line of this screen. Now do one 219more {^M} and observe the result. This is called scrolling. When you 220attempted to move to a line not displayed on the screen, the line at the top of 221the screen was 'scrolled off', and a line at the bottom of the screen was 222'scrolled on'. The top line with 'Section 11' should no longer be visible. 223 224Now type {'cz^M} to reset the screen and type {^F} for the next section. 225 226 227 228 229 230 231 232Section 12: {-} {z} 233 234The {-} command moves the cursor to the previous line in the file. Now type 235{-}, which attempts to move the cursor to the previous line in this file. 236However, that line is not on the screen. The resulting action will depend on 237your terminal. (Do a {^Mz^M} to reposition the file). On intelligent 238terminals (e.g. VT100s, xterm, most modern terminals), a top line is 'scrolled 239on' and the bottom line is 'scrolled off'. Some very old terminals, however, 240may not have this 'reverse scrolling' feature. They will simply repaint the 241screen with the cursor line in the middle of the screen. On such terminals it 242is necessary to type {z^M} to get the cursor line back to the top of the 243screen. 244 245 246 247 248 249 250 251 252 253Section 13: 254Up until this point, the tutorial has always tried to make sure that the first 255line of each screen has on it the section number and a list of the commands 256covered in that section. This will no longer be strictly maintained. If you 257want the section line at the top of the screen, you now know enough commands to 258do it easily: do {^M} or {-} until the cursor is on the section line and 259then {z^M}. Also, from this point on, it may not be the case that a {^F} will 260put you at the beginning of the next section. Therefore, be aware of where you 261are in the file as we look at other commands. You may have to find your way 262back to a particular section without any help from the tutorial. If you do not 263feel comfortable with this, then it is suggested that you practice moving from 264section 1 to section 13, back and forth, using {^M}, {-}, {^F}, and {^B} 265commands for a while. 266 267Also make liberal use of the mark command {m}: if, for example, you make a 268habit of using {mz} to mark your current location in the file, then you will 269always be able to return to that location with {'z} if the editor does 270something strange and you have no idea where you are or what happened. 271 272And finally, the proscription against experimentation is hereby lifted: play 273with the editor. Feel free to try out variations on the commands and move 274around in the file. By this time you should be able to recover from any gross 275errors. 276 277Section 14: {^E} {^Y} {^D} {^U} 278Let us now look at a few other commands for moving around in the file, and 279moving the file around on the screen. Note that the commands we have already 280looked at are sufficient: you really don't need any more commands for looking 281in a file. The following commands are not absolutely necessary. However, 282they can make editing more convenient, and you should take note of their 283existence. But it would be perfectly valid to decide to ignore them on this 284first pass: you can learn them later when you see a need for them, if you ever 285do. 286 287First, let's clear up some potentially confusing language. In at least one 288place in the official document ('An Introduction to Display Editing with Vi' 289by William Joy, and Mark Horton, September 1980), the expression "to scroll 290down text" means that the cursor is moved down in your file. However, note 291that this may result in the text on the screen moving UP. This use of the 292word 'scroll' refers to the action of the cursor within the file. However, 293another legitimate use of the word refers to the action of the text on the 294screen. That is, if the lines on your screen move up toward the top of the 295screen, this would be 'scrolling the screen up'. If the lines move down 296toward the bottom of the screen, this would be referred to as scrolling down. 297 298I have tried to maintain the following jargon: 'scrolling' refers to what the 299text does on the screen, not to what the cursor does within the file. For the 300latter I will refer to the cursor 'moving', or to 'moving the cursor'. I 301realize that this is not necessarily consistent with Joy and Horton, but they 302were wrong. 303 304{^E} scrolls the whole screen up one line, keeping the cursor on the same line, 305if possible. However, if the cursor line is the first line on the screen, then 306the cursor is moved to the next line in the file. Try typing {^E}. 307 308{^Y} scrolls the screen down one line, keeping the cursor on the same line, if 309possible. However, if the cursor line is the last line on the screen, then the 310cursor is moved to the previous line in the file. Try it. 311 312{^D} moves the cursor down into the file, scrolling the screen up. 313 314{^U} moves the cursor up into the file, also scrolling the screen if the 315terminal you are on has the reverse scroll capability. Otherwise the 316screen is repainted. 317 318Note that {^E} and {^Y} move the cursor on the screen while trying to keep the 319cursor at the same place in the file (if possible: however, the cursor can 320never move off screen), while {^D} and {^U} keep the cursor at the same place 321on the screen while moving the cursor within the file. 322 323Section 15: {/ .. /^M} 324 325Another way to position yourself in the file is by giving the editor a string 326to search for. Type the following: {/Here 1/^M} and the cursor should end up 327right ...........................here ^. Now type {/Section 15:/^M} and the 328cursor will end up over/on .....................here ^. Now type {//^M} and 329observe that the cursor is now over the capital S five lines above this line. 330Typing {//^M} several more times will bounce the cursor back and forth between 331the two occurrences of the string. In other words, when you type a string 332between the two slashes, it is searched for. Typing the slashes with nothing 333between them acts as if you had typed the previous string again. 334 335Observe that the string you type between the two slashes is entered on the 336bottom line of the screen. Now type {/Search for x /^M} except replace the 'x' 337in the string with some other character, say 'b'. The message "Pattern not 338found" should appear on the bottom of the screen. If you hadn't replaced the 339'x', then you would have found the string. Try it. 340 341Section 16: {? .. ?^M} {n} (search strings: ^ $) 342 343When you surround the sought-for string with slashes as in {/Search/}, the 344file is searched beginning from your current position in the file. If the 345string is not found by the end of the file, searching is restarted at the 346beginning of the file. However, if you do want the search to find the 347PREVIOUS rather than the NEXT occurrence of the string, surround the string 348with question marks instead of slash marks. 349 350Below are several occurrences of the same string. 351Here 2 Here 2 Here 2 352 Here 2 Here 2. 353Observe the effect of the following search commands (try them in the 354sequence shown): 355{/Here 2/^M} {//^M} {??^M} 356{/^Here 2/^M} {//^M} {??^M} 357{/Here 2$/^M} {//^M} {??^M} 358 359The first command looks for the next occurrence of the string 'Here 2'. 360However the second line of commands looks for an occurrence of 'Here 2' that 361is at the beginning of the line. When the caret (circumflex, up-arrow) is the 362first character of a search string it stands for the beginning of the line. 363When the dollar-sign is the last character of the search string it stands for 364the end of the line. Therefore, the third line of commands searches for the 365string only when it is at the end of the line. Since there is only one place 366the string begins a line, and only one place the string ends the line, 367subsequent {//^M} and {??^M} will find those same strings over and over. 368 369The {n} command will find the next occurrence of the / or ? search 370string. Try {/Here 2/^M} followed by several {n} and observe the 371effect. Then try {??^M} followed by several {n}. The {n} command 372remembers the direction of the last search. It is just a way to save a 373few keystrokes. 374 375Section 17: \ and magic-characters in search strings 376 377Now type {/Here 3$/^M}. You might expect the cursor to end up 378right......^ here. However, you will get "Pattern not found" at the bottom of 379the screen. Remember that the dollar-sign stands for the end of the line. 380Somehow, you must tell vi that you do not want the end of the line, but a 381dollar-sign. In other words, you must take away the special meaning that the 382dollar-sign has for the search mechanism. You do this (for any special 383character, including the caret ^) by putting a back-slash ('\', not '/') in 384front of the character. 385 386Now try {/Here 3\$/^M} and you should end up nine lines above this one. Try 387{//^M} and note that it returns you to the same place, and not to the first 388line of this paragraph: the back-slash character is not part of the search 389string and will not be found. To find the string in the first line of this 390paragraph, type {/Here 3\\\$/^M}. There are three back-slashes: the first takes 391away the special meaning from the second, and the third takes away the special 392meaning from the dollar-sign. 393 394Following is a list of the characters that have special meanings in search 395strings. If you wish to find a string containing one of these characters, you 396will have to precede the character with a backslash. These characters are 397called magic characters because of the fun and games you can have with them 398and they can have with you, if you aren't aware of what they do. 399 400 ^ - (caret) beginning of a line 401 $ - (dollar-sign) end of a line 402 . - (period) matches any character 403 \ - (backslash) the escape character itself 404 [ - (square bracket) for finding patterns (see section #SEARCH) 405 ] - (square bracket) ditto 406 * - (asterisk) ditto 407 408Without trying to explain it here, note that {:set nomagic^M} turns off the 409special meanings of all but the ^ caret, $ dollar-sign, and backslash 410characters. 411 412Section 18: {: (colon commands)} {ZZ} 413 414In this section we will discuss getting into and out of the editor in more 415detail. If you are editing a file and wish to save the results the command 416sequence {:w^M} writes the current contents of the file out to disk, using the 417file name you used when you invoked the editor. That is, if you are at the 418command level in Unix, and you invoke vi with {vi foo} where foo is the name 419of the file you wish to edit, then foo is the name of the file used by the 420{:w^M} command. 421 422If you are done, the write and quit commands can be combined into a single 423command {:wq^M}. An even simpler way is the command {ZZ} (two capital Z's). 424 425If, for some reason, you wish to exit without saving any changes you have made, 426{:q!^M} does the trick. If you have not made any changes, the exclamation 427point is not necessary: {:q^M}. Vi is pretty good about not letting you 428get out without warning you that you haven't saved your file. 429 430We have mentioned before that you are currently in the vi editor, editing a 431file. If you wish to start the tutorial over from the very beginning, you 432could {:q!^M}, and then type {vi.tut beginner} or {vi vi.beginner} in response 433to the Unix prompt. This will provide an unmodified copy of this file for you, 434which might be necessary if you accidentally destroyed the copy you were 435working with. Just do a search for the last section you were in: e.g. 436{/Section 18:/^Mz^M}. 437 438Section 19: {H} {M} {L} 439 440Here are a few more commands that will move you around on the screen. Again, 441they are not absolutely necessary, but they can make screen positioning easier: 442 443{H} - puts the cursor at the top of the screen (the 'home' position) 444 445{M} - puts the cursor in the middle of the screen 446 447{L} - puts the cursor at the bottom of the screen. 448 449Try typing {HML} and watch the cursor. 450 451Try typing {5HM5L} and note that 5H puts you five lines from the top of the 452screen, and 5L puts you five lines from the bottom of the screen. 453 454Section 20: {w} {b} {0} {W} {B} {e} {E} {'} {`} 455 456Up to this point we have concentrated on positioning in the file, and 457positioning on the screen. Now let's look at positioning in a line. Put the 458cursor at the beginning of the following line and type {z^M}: 459 460This is a test line: your cursor should initially be at its beginning. 461 462The test line should now be at the top of your screen. Type {w} several times. 463Note that it moves you forward to the beginning of the next word. Now type 464{b} (back to the beginning of the word) several times till you are at the 465beginning of the line. (If you accidentally type too many {b}, type {w} until 466you are on the beginning of the line again.) Type {wwwww} (five w's) and note 467that the cursor is now on the colon in the sentence. The lower-case w command 468moves you forward one word, paying attention to certain characters such as 469colon and period as delimiters and counting them as words themselves. Now 470type {0} (zero, not o 'oh'): this moves you to the beginning of the current 471line. Now type {5w} and notice that this has the effect of repeating {w} five 472times and that you are now back on the colon. Type {0} (zero) again. To 473ignore the delimiters and to move to the beginning of the next word using only 474blanks, tabs and carriage-returns (these are called white-space characters) to 475delimit the words, use the {W} command: upper-case W. {B} takes you back a 476word using white-space characters as word delimiters. 477 478Note that the commands {wbWB} do not stop at the beginning or end of a line: 479they will continue to the next word on the next line in the direction specified 480(a blank line counts as a word). 481 482If you are interested in the END of the word, and not the BEGINNING, then use 483the {e} and {E} commands. These commands only move forward and there are no 484corresponding 'reverse search' commands for the end of a word. 485 486Also, we have been using the {'} command to move the cursor to a position that 487we have previously marked with the {m} command. However, position the cursor 488in the middle of a line (any line, just pick one) and type {mk}, marking that 489position with the letter k. Now type a few returns {^M} and type {'k}. 490Observe that the cursor is now at the beginning of the line that you marked. 491Now try {`k}: note that this is the reverse apostrophe, or back-quote, or grave 492accent, or whatever you want to call it. Also note that it moves you to the 493character that was marked, not just to the line that was marked. 494 495In addition, the {``} command works just like the {''} command except that you 496are taken to the exact character, not just to the line. (I'm still not 497sure which exact character, just as I'm still not sure which line.) 498 499Section 21: {l} {k} {j} {h} 500 501There are several commands to move around on the screen on a character by 502character basis: 503 504l - moves the cursor one character to the RIGHT 505k - moves the cursor UP one line 506j - moves the cursor DOWN one line 507h - moves the cursor one character to the LEFT 508 509Section 22: {i} {a} {I} {A} {o} {O} ^[ (escape key) 510 511For this and following sections you will need to use the ESCAPE key on your 512terminal. It is usually marked ESC. Since the escape key is the same as 513typing {^[} we will use ^[ for the escape key. 514 515Probably the most often used command in an editor is the insert command. Below 516are two lines of text, the first correct, the second incorrect. Position your 517cursor at the beginning of Line 1 and type {z^M}. 518 519Line 1: This is an example of the insert command. 520Line 2: This is an of the insert command. 521 522To make line 2 look like line 1, we are going to insert the characters 523'example ' before the word 'of'. So, now move the cursor so that it is 524positioned on the 'o' of 'of'. (You can do this by typing {^M} to move 525to the beginning of line 2, followed by {6w} or {wwwwww} to position the cursor 526on the word 'of'.) 527 528Now carefully type the following string and observe the effects: 529 {iexample ^[} (remember: ^[ is the escape key)} 530The {i} begins the insert mode, and 'example ' is inserted into the line: 531be sure to notice the blank in 'example '. The ^[ ends insertion mode, 532and the line is updated to include the new string. Line 1 should look exactly 533like Line 2. 534 535Move the cursor to the beginning of Line 3 below and type {z^M}: 536 537Line 3: These lines are examples for the 'a' command. 538Line 4: These line are examples for the ' 539 540We will change line four to look like line three by using the append command. 541We need to append an 's' to the word 'line'. Position the cursor on the 'e' 542of 'line'. You can do this in several ways, one way is the following: 543First, type {/line /^M}. This puts us on the word 'line' in Line 4 544(the blank in the search string is important!). Next, type {e}. The 'e' puts 545us at the end of the word. Now, type {as^[ (^[ is the escape character)}. 546The 'a' puts us in insert mode, AFTER the current character. We appended the 547's', and the escape ^[ ended the insert mode. 548 549The difference between {i} (insert) and {a} (append) is that {i} begins 550inserting text BEFORE the cursor, and {a} begins inserting AFTER the cursor. 551 552Now type {Aa' command.^[}. The cursor is moved to the end of the line and the 553string following {A} is inserted into the text. Line 4 should now look like 554line 3. 555 556Just as {A} moves you to the end of the line to begin inserting, {I} would 557begin inserting at the FRONT of the line. 558 559To begin the insertion of a line after the cursor line, type {o}. To insert a 560line before the cursor line, type {O}. In other words {o123^[} is equivalent 561to {A^M123^[}, and {O123^[} is equivalent to {I123^M^[}. The text after the 562{o} or {O} is ended with an escape ^[. 563 564This paragraph contains information that is terminal dependent: you will just 565have to experiment to discover what your terminal does. Once in the insert 566mode, if you make a mistake in the typing, ^H will delete the previous 567character up to the beginning of the current insertion. ^W will delete the 568previous word, and one of ^U, @, or ^X will delete the current line (up to the 569beginning of the current insertion). You will need to experiment with ^U, @, 570and ^X to determine which works for your terminal. 571 572Section 23: {f} {x} {X} {w} {l} {r} {R} {s} {S} {J} 573 574Position the cursor at the beginning of line 5 and {z^M}: 575 576Line 5: The line as it should be. 577Line 6: The line as it shouldn't be. 578 579To make Line 6 like Line 5, we have to delete the 'n', the apostrophe, and the 580't'. There are several ways to position ourselves at the 'n'. Choose 581whichever one suits your fancy: 582 583{/n't/^M} 584{^M7w6l} or {^M7w6 } (note the space) 585{^M3fn} (finds the 3rd 'n' on the line) 586 587Now {xxx} will delete the three characters, as will {3x}. 588 589Note that {X} deletes the character just BEFORE the cursor, as opposed 590to the character AT the cursor. 591 592Position the cursor at line 7 and {z^M}: 593 594Line 7: The line as it would be. 595Line 8: The line as it could be. 596 597To change line 8 into line 7 we need to change the 'c' in 'could' into a 'w'. 598The 'r' (replace) command was designed for this. Typing {rc} is the same as 599typing {xic^[} (i.e. delete the 'bad' character and insert the correct 600new character). Therefore, assuming that you have positioned the cursor on the 601'c' of 'could', the easiest way to change 'could' into 'would' is {rw}. 602 603If you would like to now change the 'would' into 'should', use the substitute 604command, 's': {ssh^[}. The difference between 'r' and 's' is that 'r' 605(replace) replaces the current character with another character, while 's' 606(substitute) substitutes the current character with a string, ended with an 607escape. 608 609The capital letter version of replace {R} replaces each character by a 610character one at a time until you type an escape, ^[. The 'S' command 611substitutes the whole line. 612 613Position your cursor at the beginning of line 9 and {z^M}. 614 615Line 9: Love is a many splendored thing. 616Line 10: Love is a most splendored thing. 617 618To change line 10 into line 9, position the cursor at the beginning of 'most', 619and type {Rmany^[}. 620 621You may have noticed that, when inserting text, a new line is formed by typing 622{^M}. When changing, replacing, or substituting text you can make a new line 623by typing {^M}. However, neither {x} nor {X} will remove ^M to make two lines 624into one line. To do this, position the cursor on the first of the two lines 625you wish to make into a single line and type {J} (uppercase J for 'Join'). 626 627Section 24: {u} {U} 628 629Finally, before we review, let's look at the undo command. Position 630your cursor on line 11 below and {z^M}. 631 632Line 11: The quick brown fox jumped over the lazy hound dog. 633Line 12: the qwick black dog dumped over the laxy poune fox. 634 635Type the following set of commands, and observe carefully the effect of each 636of the commands: 637 638{/^Line 12:/^M} {ft} {rT} {fw} {ru} {w} {Rbrown fox^[} {w} {rj} 639{fx} {rz} {w} {Rhound dog^[} 640 641Line 12 now matches line 11. Now type {U} - capital 'U'. And line 12 now 642looks like it did before you typed in the command strings. Now type: 643 644{ft} {rT} {fw} {ru} {^M} {^M} 645 646and then type {u}: the cursor jumps back to the line containing the second 647change you made and 'undoes' it. That is, {U} 'undoes' all the changes on the 648line, and {u} 'undoes' only the last change. Type {u} several times and 649observe what happens: {u} can undo a previous {u}! 650 651Caveat: {U} only works as long as the cursor is still on the line. Move the 652cursor off the line and {U} will have no effect, except to possibly beep at 653you. However, {u} will undo the last change, no matter where it occurred. 654 655Section 25: review 656 657At this point, you have all the commands you need in order to make use of vi. 658The remainder of this tutorial will discuss variations on these commands as 659well as introduce new commands that make the job of editing more efficient. 660Here is a brief review of the basic commands we have covered. They are listed 661in the order of increasing complexity and/or decreasing necessity (to say that 662a command is less necessary is not to say that it is less useful!). These 663commands allow you to comfortably edit any text file. There are other 664commands that will make life easier but will require extra time to learn, 665obviously. You may want to consider setting this tutorial aside for several 666weeks and returning to it later after gaining experience with vi and getting 667comfortable with it. The convenience of some of the more exotic commands may 668then be apparent and worth the extra investment of time and effort 669required to master them. 670 671to get into the editor from Unix: {vi filename} 672to exit the editor 673 saving all changes {ZZ} or {:wq^M} 674 throwing away all changes {:q!^M} 675 when no changes have been made {:q^M} 676save a file without exiting the editor {:w^M} 677write the file into another file {:w filename^M} 678insert text 679 before the cursor {i ...text... ^[} 680 at the beginning of the line {I ...text... ^[} 681 after the cursor (append) {a ...text... ^[} 682 at the end of the line {A ...text... ^[} 683 after the current line {o ...text... ^[} 684 before the current line {O ...text... ^[} 685delete the character ... 686 under the cursor {x} 687 to the left of the cursor {X} 688delete n characters {nx} or {nX} (for n a number) 689make two lines into one line (Join) {J} 690find a string in the file ... 691 searching forward {/ ...string... /^M} 692 searching backwards {? ...string... ?^M} 693repeat the last search command {n} 694repeat the last search command in the 695 opposite direction {N} 696find the character c on this line ... 697 searching forward {fc} 698 searching backward {Fc} 699repeat the last 'find character' command {;} 700replace a character with character x {rx} 701substitute a single character with text {s ...text... ^[} 702substitute n characters with text {ns ...text... ^[} 703replace characters one-by-one with text {R ...text... ^[} 704undo all changes to the current line {U} 705undo the last single change {u} 706move forward in the file a "screenful" {^F} 707move back in the file a "screenful" {^B} 708move forward in the file one line {^M} or {+} 709move backward in the file one line {-} 710move to the beginning of the line {0} 711move to the end of the line {$} 712move forward one word {w} 713move forward one word, ignoring punctuation {W} 714move forward to the end of the next word {e} 715to the end of the word, ignoring punctuation{E} 716move backward one word {b} 717move back one word, ignoring punctuation {B} 718return to the last line modified {''} 719scroll a line onto the top of the screen {^Y} 720scroll a line onto the bottom of the screen {^E} 721move "up" in the file a half-screen {^U} 722move "down" in the file a half-screen {^D} 723move the cursor to the top screen line {H} 724move the cursor to the bottom screen line {L} 725move the cursor to the middle line {M} 726move LEFT one character position {h} or {^H} 727move RIGHT one character position {l} or { } 728move UP in the same column {k} or {^P} 729move DOWN in the same column {j} or {^N} 730mark the current position, name it x {mx} 731move to the line marked/named x {'x} 732move to the character position named x {`x} 733move to the beginning of the file {1G} 734move to the end of the file {G} 735move to line 23 in the file {23G} 736repaint the screen with the cursor line 737 at the top of the screen {z^M} 738 in the middle of the screen {z.} 739 at the bottom of the screen {z-} 740 741More information on vi can be found in the file vi.advanced, which you can 742peruse at your leisure. From UNIX, type {vi.tut advanced^M} or 743{vi vi.advanced^M}. 744