home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the vi EditorSearch this book

2.2. Moving the Cursor

You may spend only a small amount of time in an editing session adding new text in insert mode; much of the time you will be making edits to existing text.

In command mode you can position the cursor anywhere in the file. Since you begin all basic edits (changing, deleting, and copying text) by placing the cursor at the text that you want to change, you want to be able to move the cursor to that place as quickly as possible.

There are vi commands to move the cursor:

  • Up, down, left, or right—one character at a time

  • Forward or backward by blocks of text such as words, sentences, or paragraphs

  • Forward or backward through a file, one screen at a time

In Figure 2.1, an underscore marks the present cursor position. Circles show movement of the cursor from its current position to the position that would result from various vi commands.

Figure 2.1

Figure 2.1. Sample movement commands

2.2.3. Movement Within a Line

When you saved the file practice, vi displayed a message telling you how many lines are in that file. A line is not necessarily the same length as the visible line (often limited to 80 characters) that appears on the screen. A line is any text entered between newlines. (A newline character is inserted into the file when you press the RETURN key in insert mode.) If you type 200 characters before pressing RETURN, vi regards all 200 characters as a single line (even though those 200 characters visibly take up several lines on the screen).

As we mentioned, vi has an option that allows you to set a distance from the right margin at which vi will automatically insert a newline character. This option is wrapmargin (its abbreviation is wm). You can set a wrapmargin at 10 characters:

:set wm=10

This command doesn't affect lines that you've already typed. We'll talk more about setting options in Chapter 7. (This one really couldn't wait!)

If you do not use vi's automatic wrapmargin option, you should break lines with carriage returns to keep the lines of manageable length.

Figure Two useful commands that involve movement within a line are:

0
Move to beginning of line.

$
Move to end of line.

In the example below, line numbers are displayed. (Line numbers can be displayed in vi by using the number option, which is enabled by typing :set nu in command mode. This operation is described in Chapter 7.)

Figure 2.2.3

Figure The number of logical lines (3) does not correspond to the number of visible lines (6) that you see on the screen. If the cursor were positioned on the d in the word delete, and you entered $, the cursor would move to the period following the word them. If you entered 0, the cursor would move back to the letter m in the word move, at the beginning of line two.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.