new meaning or initiates some instruction.
There are several ways to tell vi that you want to begin insert mode. One of the
I
most common is to press i. The i doesnt appear on the screen, but after you press
it, whatever you type will appear on the screen and will be entered into the buffer. The
cursor marks the current insertion point.* To tell vi that you want to stop inserting text,
press ESC . Pressing ESC moves the cursor back one space (so that it is on the last
character you typed) and returns vi to command mode.
For example, suppose you have opened a new file and want to insert the word intro-
duction. If you type the keystrokes iintroduction, what appears on the screen is:
introduction
When you open a new file, vi starts in command mode and interprets the first keystroke
(i) as the insert command. All keystrokes made after the insert command are considered
text until you press ESC . If you need to correct a mistake while in insert mode, back-
space and type over the error. Depending on the type of terminal you are using, back-
spacing may erase what youve previously typed or may just back up over it. In either
case, whatever you back up over will be deleted. Note that you cant use the backspace
key to back up beyond the point where you entered insert mode. (If you have disabled
vi compatibility, Vim allows you to backspace beyond the point where you entered
insert mode.)
vi has an option that lets you define a right margin and provides a carriage return
automatically when you reach it. For right now, while you are inserting text, press
ENTER to break the lines.
Sometimes you dont know whether you are in insert mode or command mode. When-
ever vi does not respond as you expect, press ESC once or twice to check which mode
you are in. When you hear the beep, you are in command mode.
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.
* Some versions show that youre in input mode in the status line.
14 | Chapter 2:Simple Editing
There are vi commands to move the cursor:
Up, down, left, or rightone 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.
O
b
2k
$
With a screen editor you can scroll the
page, move the cursor, delete lines,
and more, while seeing the results of
your edits as you make them.
2h
j
2w
Figure 2-1. Sample movement commands
Single Movements
The keys h, j, k, and l, right under your fingertips, will move the cursor:
h
Left, one space
j
Down, one line
k
Up, one line
l
Right, one space
You can also use the cursor arrow keys ( , , , ), + and - to go up and down, or
the ENTER
and BACKSPACE keys, but they are out of the way. At first, it may seem
awkward to use letter keys instead of arrows for cursor movement. After a short while,
though, youll find it is one of the things youll like best about viyou can move around
without ever taking your fingers off the center of the keyboard.
Moving the Cursor | 15
Before you move the cursor, press ESC to make sure that you are in command mode.
Use h, j, k, and 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 youre at the beginning or end of a line, you cannot
use h or l to wrap around to the previous or next line; you have to use j or k. 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 text.
Numeric Arguments
You can precede movement commands with numbers. Figure 2-2 shows how the com-
mand 4l moves the cursor four spaces to the right, just as if you had typed l four times
(llll).
4l
With a screen editor you can scroll the
Figure 2-2. Multiplying commands by numbers
The ability to multiply commands gives you more options and power for each com-
mand you learn. Keep this in mind as you are introduced to additional commands.
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 ENTER key in insert