mode.) If you type 200 characters before pressing ENTER , 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 in Chapter 1, 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 doesnt affect lines that youve already typed. Well talk more about
setting options in Chapter 7. (This one really couldnt wait!)
If you do not use vis automatic wrapmargin option, you should break lines with carriage
returns to keep the lines of manageable length.
Vim, with nocompatible set, allows you to space past the end of the line to the next one with l or the space
bar.
16 | Chapter 2:Simple Editing
Two useful commands that involve movement within a line are:
0
0 (digit zero)
Move to beginning of line.
$
Move to end of line.
In the following example, 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.)
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 number of logical lines (3) does not correspond to the number of visible lines
$ (5) 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.
Movement by Text Blocks
You can also move the cursor by blocks of text: words, sentences, paragraphs,
w etc. The w command moves the cursor forward one word at a time, counting
symbols and punctuation as equivalent to words. The following line shows cursor
movement by w:
cursor, delete lines, insert characters,
You can also move by word, not counting symbols and punctuation, using the W com-
mand. (You can think of this as a large
or capital W ord.)
Cursor movement using W looks like this:
cursor, delete lines, insert characters,
To move backward by word, use the b command. Capital B allows you to move back-
ward by word, not counting punctuation.
As mentioned previously, movement commands take numeric arguments; so, with ei-
ther the w or b commands you can multiply the movement with numbers. 2w moves
forward two words; 5B moves back five words, not counting punctuation.
To move to a specific line, you can use the G command. Plain G goes to the end of the
file, 1G goes to the top of the file, and 42G goes to line 42. This is described in more detail
later in the section The G (Go To) Command on page 43.
Moving the Cursor | 17
With a editor you can scrooll the page,
move the cursor, delete lines, nisret
characters, and more, while results of
your edits as you make tham.
Since they allow you to make changes
as you read through a file, much as
you would edit a printed copy,
screen editors are very popular.
Figure 2-3. Proofreading edits
Well discuss movement by sentences and by paragraphs in Chapter 3. For now,
practice using the cursor movement commands that you know, combining them with
numeric multipliers.
Simple Edits
When you enter text in your file, it is rarely perfect. You find typos or want to improve
on a phrase; sometimes your program has a bug. Once you enter text, you have to be
able to change it, delete it, move it, or copy it. Figure 2-3 shows the kinds of edits you
might want to make to a file. The edits are indicated by proofreading marks.
In vi you can perform any of these edits with a few basic keystrokes: i for insert (which
youve already seen); a for append; c for change; and d for delete. To move or copy text,
you use pairs of commands. You move text with a d for delete, then a p for put;
you copy text with a y for yank, then a p for put. Each type of edit is described in
this section. Figure 2-4 shows the vi commands you use to make the edits marked in
Figure 2-3.
Inserting New Text
You have already seen the insert command used to enter text into a new file. You also
use the insert command while editing existing text to add missing characters, words,
and sentences. In the file practice, suppose you have the sentence:
you can scroll
the page, move the cursor, delete
lines, and insert characters.
18 | Chapter 2:Simple Editing
i screen ESC
x
i seeing the ESC
With a editor you can scrooll the page, cw
move the cursor, delete lines, nisret
insert
characters, and more while results of
ESC
rs
your edits as you make tham.
re
P
Since they allow you to make changes
as you read through a file, much as
you would edit a printed copy,
r.
dd
screen editors are very popular.
x
rS
Figure 2-4. Edits with vi commands