VI Tutorial - Lesson 6 - Positioning the Cursor

Congratulations, you have succeeded in getting the text you wanted to appear on the terminal screen. Now that you can see the target text, you can proceed to move in closer "for the kill". This will probably require a combination of major and minor cursor moves. Cursor movement commands are issued in command mode.

6.1 Major Screen Movements

Sometimes the cursor can be a substantial distance from the next editing position on the screen. Large vertical jumps may be made with the use of the mnemonic commands: "H" for high or home, "M" for middle, or "L" for low or last screen line. When you type "H" "M" or "L" the cursor will immediately reposition to the first character space on the highest, middle, or lowest screen line.

After using these three commands for a while, you may decide to become more precise in moving the cursor by adding a number to the command. For example, "3H" would move the cursor to the third line from the top of the screen. The command "3L" would likewise move the cursor to the third line from the bottom of the screen. As you may guess, the middle is always the middle and you can not fancy up the "M" command.

Figure 6.1

6.2 Minor Screen Movements

The most common means used to move the cursor is by use of "arrow" keys. Pressing an arrow key "\(ua" "\(da" "->" or "<-" will move the cursor one space in the direction indicated. If the terminal you are using does not have arrow keys, you can use the "direction" keys "h" (left), "j" (down), "k" (up), and "l" (right) to move the cursor one space on the screen. Pressing direction keys in command mode will move the cursor as indicated in the figure to the right. Watch out, many people mix up the lower-case "l" with the lower-case "i" or the numeral "1".

The "RETURN" key is similar to the "j" key in that it moves the cursor down one line. However, the "RETURN" key always positions the cursor at the beginning of the next line down; whereas, the "j" key moves the cursor straight down from its present position, which may be the middle of a line. Moving several spaces may be accomplished by repeatedly pressing the "RETURN", direction or arrow key; such as, "k" "k" "k" to move upward 3 lines. You can also precede any of these keys with a number and achieve the same results, "3k".

Similarly, you can use the "SPACE BAR" to move to the right one character. When using this combined with a certain number of characters ("25sp") you can search for the ith character in the file as opposed to the ith character in a the line. ("25l" will find the 25th character in that line whereas "25sp" will disregard the end line characters and continue searching for the 25th character)

Most speed typists prefer to move the cursor with the direction keys rather than arrow keys because they do not have to remove their fingers from the center of the keyboard.

6.3 Moving within the Line

Once you have located the cursor on the correct line, it may be necessary to fine tune the placement of the cursor still further. You already possess the ability to move right and left by way of arrow or direction keys: "l" and "->" (right), or "h" and "<-" (left); and for a while this will probably be all you need to know. However, after you have worked with the VI editor for a while, it would be to your advantage to add the following line movement "scopes" to your command arsenal. Scope refers to the amount of text unit encompassed by a command key.

 

Scope      Text Unit Encompassed________________________________     0     beginning of line (zero)     $     end of lineW    w     word rightB    b     word leftE    e     end of word right

The figure on the next page provides an illustration of how the cursor would move using the scope keys. When you experiment with these keys on a terminal screen, you will notice that the "0" and "$" keys will jump to the beginning or end of the line the cursor is on, but will move no further. The "w", "b", "e" keys, on the other hand, will wraparound to the line below if moving right, or to the line above if moving left. The mini terminal screens on the next page show the path taken when a scope key is pressed repeatedly causing the cursor to reposition again and again. It is important to remember that the cursor position serves as a reference point for all scopes. Notice that when moving with the "b" or "e" key, if the cursor is sitting in the middle of a word, the remainder of that word is counted as a scope unit. Additionally, the lower-case scopes treat punctuation marks as a scope unit. In other words, a comma or period will be treated like a word in itself.

As with direction and arrow keys, the jumping power of "w", "b", or "e" can be multiplied by preceding the key with a number. At the bottom of the next page are three sentences illustrating the effect of instructing the scope key to jump four units. In each example, the cursor is originally sitting on the "r" of the word "green". The arrow tip points to the destination location.

VI offers many variations for getting an editing job done. One of vi's "more-than-one-way-to-skin-a-cat" options, is the capitalization of the "W" "B" "E" keys. The action of the upper-case scope keys is the same as the lower-case scope keys, except the upper-case scope keys DO NOT see punctuation marks as a scope unit; therefore, DO NOT stop for punctuation marks. Use the form that works best for you...most people do not try to remember both variations.

The power of scopes is greatly increased when combined with operators. This concept is discussed further in the Correcting Text section.

0: moves to the beginning of line

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After 0 Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

$: moves to end of line

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After $ Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

w: moves forward 1 word

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After w Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After Additional w Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

b: moves backward 1 word

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After b Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After Additional b Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

e: moves to end of word

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After e Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

After Additional e Command

Purdue won the Rose Bowlin 1967, beating USC by ascore of 14 to 13.

 


previous | MENU | next