Command Mode: Moving the Cursor


While vim is in Command mode, you can position the cursor over any character on the screen. You can also display a different portion of the Work buffer on the screen. By manipulating the screen and cursor position, you can place the cursor on any character in the Work buffer.

You can move the cursor forward or backward through the text. As illustrated in Figure 6-8, forward means toward the right and bottom of the screen and the end of the file. Backward means toward the left and top of the screen and the beginning of the file. When you use a command that moves the cursor forward past the end (right) of a line, the cursor generally moves to the beginning (left) of the next line. When you move it backward past the beginning of a line, the cursor moves to the end of the previous line.

Figure 6-8. Forward and backward


Long lines

Sometimes a line in the Work buffer may be too long to appear as a single line on the screen. In such a case vim wraps the current line onto the next line (unless you set the nowrap option [page 182]).

You can move the cursor through the text by any Unit of Measure (that is, character, word, line, sentence, paragraph, or screen). If you precede a cursor-movement command with a number, called a Repeat Factor, the cursor moves that number of units through the text. Refer to pages 188 and 191 for more precise definitions of these terms.

Moving the Cursor by Characters

l/h

The SPACE bar moves the cursor forward, one character at a time, toward the right side of the screen. The l (lowercase "l") key and the RIGHT ARROW key (Figure 6-9) do the same thing. The command 7 SPACE or 7l moves the cursor seven characters to the right. These keys cannot move the cursor past the end of the current line to the beginning of the next line. The h and LEFT ARROW keys are similar to the l key but work in the opposite direction.

Figure 6-9. Moving the cursor by characters


Moving the Cursor to a Specific Character

f/F

You can move the cursor to the next occurrence of a specified character on the current line by using the Find command. For example, the following command moves the cursor from its current position to the next occurrence of the character a, if one appears on the same line:

fa


You can also find the previous occurrence by using a capital F. The following command moves the cursor to the position of the closest previous a in the current line:

Fa


A semicolon (;) repeats the last Find command.

Moving the Cursor by Words

w/W

The w (word) key moves the cursor forward to the first letter of the next word (Figure 6-10). Groups of punctuation count as words. This command goes to the next line if the next word is located there. The command 15w moves the cursor to the first character of the fifteenth subsequent word.

Figure 6-10. Moving the cursor by words


The W key is similar to the w key but moves the cursor by blank-delimited words, including punctuation, as it skips forward. (Refer to "Blank-Delimited Word" on page 188.)

b/B

The b (back) key moves the cursor backward to the first letter of the previous word. The B key moves the cursor backward by blank-delimited words. Similarly the e key moves the cursor to the end of the next word; E moves it to the end of the next blank-delimited word.

Moving the Cursor by Lines

j/k

The RETURN key moves the cursor to the beginning of the next line; the j and DOWN ARROW keys move it down one line to the character just below the current character (Figure 6-11). If no character is immediately below the current character, the cursor moves to the end of the next line. The cursor will not move past the last line of text in the work buffer.

Figure 6-11. Moving the cursor by lines


The k and UP ARROW keys are similar to the j key but work in the opposite direction. The minus () key is similar to the RETURN key but works in the opposite direction.

Moving the Cursor by Sentences and Paragraphs

)/( }/{

The ) and } keys move the cursor forward to the beginning of the next sentence or the next paragraph, respectively (Figure 6-12). The ( and { keys move the cursor backward to the beginning of the current sentence or paragraph. You can find more information on sentences and paragraphs starting on page 189.

Figure 6-12. Moving the cursor by sentences, paragraphs, H, M, and L


Moving the Cursor Within the Screen

H/M/L

The H (home) key positions the cursor at the left end of the top line of the screen. The M (middle) key moves the cursor to the middle line, and the L (lower) key moves it to the bottom line (Figure 6-12).

Viewing Different Parts of the Work Buffer

The screen displays a portion of the text that is in the Work buffer. You can display the text preceding or following the text on the screen by scrolling the display. You can also display a portion of the Work buffer based on a line number.

CONTROL-D CONTROL-U

Press CONTROL-D to scroll the screen down (forward) through the file so that vim displays half a screen of new text. Use CONTROL-U to scroll the screen up (backward) by the same amount. If you precede either of these commands with a number, vim will scroll that number of lines each time you use CONTROL-D or CONTROL-U for the rest of the session (unless you again change the number of lines to scroll). See page 183 for a discussion of the scroll parameter.

CONTROL-F CONTROL-B

The CONTROL-F (forward) and CONTROL-B (backward) keys display almost a whole screen of new text, leaving a couple of lines from the previous screen for continuity. On many keyboards you can use the PAGE DOWN and PAGE UP keys in place of CONTROL-F and CONTROL-B.

Line numbers (G)

When you enter a line number followed by G (goto), vim positions the cursor on that line in the Work buffer. If you press G without a number, vim positions the cursor on the last line in the Work buffer. Line numbers are implicit; your file does not need to have actual line numbers for this command to work. Refer to "Line numbers" on page 182 if you want vim to display line numbers.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net