2.2 Moving the CursorYou 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:
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: Sample movement commands2.2.1 Single Movements
The keys
You can also use the cursor arrow keys (, , , ) or the [RETURN] and [BACKSPACE] keys, but they are out of the way, and the arrow keys are not supported by all terminals. At first, it may seem awkward to use letter keys instead of arrows for cursor movement. After a short while, though, you'll find it is one of the things you'll like best about vi - you can move around without ever taking your fingers off the center of the keyboard.
Before you move the cursor, press
[ESC]
to make sure that you are in command mode.
Use 2.2.2 Numeric Arguments
You can precede movement commands with numbers.
Figure 2.2
shows how the command Figure 2.2: Multiplying commands by numbersThe ability to multiply commands gives you more options and power for each command you learn. Keep it in mind as you are introduced to additional commands. 2.2.3 Movement Within a LineWhen 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 (limited usually 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
This command doesn't affect lines that you've already typed. We'll talk more about setting options in Chapter 7, Advanced Editing . (This one really couldn't wait!)
If you do not use
vi
's automatic [0] Two useful commands that involve movement within a line are:
In the example below, line numbers are displayed. (Line numbers can be displayed in
vi
by using the
$
, 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.
2.2.4 Movement by Text Blocks[w] You can also move the cursor by blocks of text: words, sentences, paragraphs, etc.
The
c ursor , d elete l ines , i nsert c haracters ,
You can also move by word, not counting symbols and punctuation, using the
c ursor, d elete l ines, i nsert c haracters,
To move backward by word, use the
As mentioned previously, movement commands take numeric
arguments; so, with either the We'll discuss movement by sentences and by paragraphs in Chapter 3, Moving Around in a Hurry . For now, practice using the cursor movement commands that you know, combining them with numeric multipliers. |
|