vi tutorial #2: Moving Through Files Efficiently This lesson lasts 15-20 minutes. The material taught here is used in tutorial #3: Cutting and Pasting. Lines which begin with >>> mark exercises you should try. When you want to exit this tutorial type ZZ . WORDS ----- There are many ways to move from one word to another. Consider these: w Move to the beginning of the next WORD e Move to the END of the next word b Move BACK to the beginning to the previous word For w, e, and b, a word is delimited by any non-alphanumeric character. The capitalized versions, W, E, and B, also move from word to word. The difference is that for W, E, and B, a word is delimited by any blank space. >>> Try out w, b, e, on the lines provided below. >>> Next practice using B, W, b, E on the lines provided below. EX-PER-IMENT on these lines;test moving back &forth. EX-PER-IMENT on these lines;test moving back &forth. ON THE LINE ----------- You can move immediately to any point on the current line. $ Move to the end of the line ^ Move to the first non-white character on the line 0 Move to the first column on the line (column zero) #| Move to an exact column on the line (column #) e.g. 5| 12| >>> Experiment with $ and ^ on the line provided below. Notice >>> that ^ moves to the first non-white character, not the beginning. This is a PRACTICE LINE. There is white space at the front. END 0 (zero) will always take you to the far left edge of the screen. #| (number vertical-bar) is for moving to an explicit column on a line. Just type any number 1-80 and press | . For example: 5| 20| 30| Note that you can't move beyond the last column on a line. FINDING CHARACTERS ------------------ Often you want to move to a specific letter or character on a line. f char FIND the next occurrence of char on the line t char Move 'TIL the next occurrence of char on the line F char FIND the previous occurrence of char on the line T char Move 'TIL the previous occurrence of char on the line ; Repeat the last f, t, F, or T , Reverse the last f, t, F, or T f and F land on the character. t and T land next to the character. f and t move forward, while F and T move backward. If the specified character is not on the line, vi will beep. >>> Move to the beginning of the line below, and try out these commands: >>> fe fE ; ; , , t@ TP te tE , ; , ; "PRACTICE line?" "Each and Every?" "Find thE char@cter and move to it.END MATCHING -------- vi has a handy way to determine if (), {}, and [] pairs match up. % Show matching () or {} or [] >>> On the practice lines below, move your cursor over a (,),{,},[, or ]. >>> Then type % . [TRY THIS. ((Whether) the pairs match up is the question.) [One] pair is incomplete]. Can you tell {which one? ]} END WINDOW POSITIONS ---------------- You can move the cursor to the top, middle, or bottom of the vi window. H Move to the HIGHEST position in the window M Move to the MIDDLE position in the window L Move to the LOWEST position in the window >>> Try out these commands: type H then M and L and then M again. MARKING LOCATIONS ----------------- You can mark positions in the file and return to them. m char MARK this location and name it char ' char (quote character) return to line named char '' (quote quote) return from last movement char can be any lower case letter, a-z. A mark persists until you: 1) use the same char to mark another location or 2) delete the marked line >>> Move to this line and type ma to mark it a >>> Move to this line and type mb to mark it b >>> Move to this line and type mz to mark it z >>> Type 'a to return to line a >>> Type 'b to return to line b >>> Type 'z to return to line z Certain commands can move you large distances. These commands cause your last position to be remembered in the special mark named ' (quote). To move to this special mark, just type '' (quote quote). >>> Try this: 'b to return to line b, and then '' to return here. GO TO A LINE ------------ G GO to the last line in the file #G GO to line #. (e.g., 3G , 5G , 124G ) Read these directions carefully: >>> Type 1G to go to the top of the file, and then '' (quote quote) >>> to return here. >>> Now try G to go to the end of the file, and then '' to return here. BLOCKS OF TEXT -------------- It is often convenient to move through files jumping from one block of text to the next. To do this use braces and parentheses: { (left brace) Move to the beginning of a paragraph } (right brace) Move to the end of a paragraph ( (left paren) Move to the beginning of a sentence ) (right paren) Move to the beginning of the next sentence >>> Experiment with } and { on the two paragraphs provided below. >>> Note that paragraphs are separated by a blank line. EXPERIMENT on this first paragraph. The quick brown fox jumped over the seven lazy dogs. The fox must have been very large to jump over seven dogs! EXPERIMENT on this second paragraph. The quick brown dog jumped over the seven lazy foxes. The dog didn't have to be nearly as large, since foxes aren't too big. >>> Try out ) and ( on the two paragraphs provided above. >>> Notice that sentences are separated by two blank spaces. C programmers find it useful to move by sections, since sections may be delimited by a left brace in the first column. By placing the opening brace of a C subroutine in the first column, you can move to the top of the next subroutine, using [[ and ]] . [[ Move to the beginning of a section ]] Move to the end of a section Note that if vi does not find a left brace at the far left, it will move to the top or bottom of the file. >>> Now try ]] then ]] and [[ on the subroutines provided below: main() { helloworld(); } helloworld() { printf( "Hello world\n" ); } SEARCHING --------- This enables you to jump to the next occurrence of a string in a file. To initially find the string use: /string Find string looking forward ?string Find string looking backward To find additional occurrences of the string type: n Repeat last / or ? command N Reverse last / or ? command vi may search past the bottom of the file and then start again at the top. (Or, vi may search past the top and then start again at the bottom.) >>> You are going to search for a string, find the next three >>> occurrences. Then flip directions and find the string until you >>> return to this location. To do this: >>> Type /the then press RETURN. >>> Type n three times. >>> Type N until you return to this location. SUMMARY ------- w Move to the beginning of the next WORD e Move to the END of the next word b Move BACK to the beginning to the previous word $ Move to the end of the line ^ Move to the first non-white character on the line 0 Move to the first column on the line (column zero) #| Move to an exact column on the line (column #) e.g. 5| 12| f char FIND the next occurrence of char on the line t char Move 'TIL the next occurrence of char on the line F char FIND the previous occurrence of char on the line T char Move 'TIL the previous occurrence of char on the line ; Repeat the last f, t, F, or T , Reverse the last f, t, F, or T % Show matching () or {} or [] H Move to the HIGHEST position in the window M Move to the MIDDLE position in the window L Move to the LOWEST position in the window m char MARK this location and name it char ' char (quote character) return to line named char '' (quote quote) return from last movement G GO to the last line in the file #G GO to line #. (e.g., 3G , 5G , 175G ) { (left brace) Move to the beginning of a paragraph } (right brace) Move to the end of a paragraph ( (left paren) Move to the beginning of a sentence ) (right paren) Move to the beginning of the next sentence [[ Move to the beginning of a section ]] Move to the end of a section /string Find string looking forward ?string Find string looking backward n Repeat last / or ? command N Reverse last / or ? command You should now be able to move around files very efficiently. These commands are especially useful if you are using vi over a slow modem. Practice the material in this lesson for a few days and then take either the third vi tutorial to learn how to copy, cut, and paste, or the forth vi tutorial to learn additional insertion techniques. Copyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.