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.
left, one space.
down, one line.
up, one line.
right, one space.
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.
l to move
forward or backward in the file from the current cursor position.
When you have gone as far as possible in one direction, you hear a beep
and the cursor stops.
For example, once you're at the beginning or end of a line,
you cannot use
l to wrap around to the previous or
next line; you have to use
Similarly, you cannot move the cursor past a tilde (~) representing a line
without text, nor can you move the cursor above the first line of
You can precede movement commands with numbers.
shows how the command
the cursor four spaces to the right, just as if you had typed
four times (
The 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.
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 (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
(its abbreviation is
wm). You can set a wrapmargin at 10 characters
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
you should break
lines with carriage returns to keep the lines of manageable length.
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
nu in command mode.
This operation is described in Chapter 7, Advanced
1 With a screen editor you can scroll the page, 2 move the cursor, delete lines, insert characters, and more, while seeing the results of your edits as you make them. 3 Screen editors are very popular.
$, 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.
w command moves the cursor forward one word at a
time, counting symbols and punctuation as equivalent to words.
The line below shows cursor movement by
cursor, delete lines, insert characters,
You can also move by word, not counting symbols and punctuation, using the
(You can think of this as a "large" or "capital" Word.)
Cursor movement using
W looks like this:
cursor, delete lines, insert characters,
To move backward by word, use the
B allows you to move backward by word, not counting
As mentioned previously, movement commands take numeric
arguments; so, with either the
b commands you
can multiply the movement with numbers.
2w moves forward
5B moves back five words, not counting punctuation.
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.