way its supposed to.
Make sure youre not typing the J command when you mean j.
You may have hit the CAPS LOCK key without noticing it. vi is case-sensitive;
that is, uppercase commands (I, A, J, etc.) are different from lowercase commands
(i, a, j), and if you hit this key, all your commands are interpreted not as lowercase
but as uppercase commands. Press the CAPS LOCK key again to return to low-
ercase, press ESC to ensure that you are in command mode, and then type either
U to restore the last line changed or u to undo the last command. Youll probably
also have to do some additional editing to fully restore the garbled part of your file.
Review of Basic vi Commands
Table 2-1 presents a few of the commands you can perform by combining the
commands c, d, and y with various text objects. The last two rows show additional
commands for editing. Tables 2-2 and 2-3 list some other basic commands. Ta-
ble 2-4 summarizes the rest of the commands described in this chapter.
Table 2-1. Edit commands
Text object
Change
Delete
Copy
One word
cw
dw
yw
Two words, not counting punctuation
2cW or c2W
2dW or d2W
2yW or y2W
Three words back
3cb or c3b
3db or d3b
3yb or y3b
One line
cc
dd
yy or Y
To end of line
c$ or C
d$ or D
y$
To beginning of line
c0
d0
y0
Single character
r
x or X
yl or yh
Five characters
5s
5x
5yl
Table 2-2. Movement
Movement
Commands
, , ,
h, j, k, l
To first character of next line
+
To first character of previous line
-
To end of word
e or E
Forward by word
w or W
32 | Chapter 2:Simple Editing
Movement
Commands
Backward by word
b or B
To end of line
$
To beginning of line
0
Table 2-3. Other operations
Operations
Commands
Place text from buffer
P or p
Start vi, open file if specified
vi file
Save edits, quit file
ZZ
No saving of edits, quit file
:q!
Table 2-4. Text creation and manipulation commands
Editing action
Command
Insert text at current position
i
Insert text at beginning of line
I
Append text at current position
a
Append text at beginning of line
A
Open new line below cursor for new text
o
Open new line above cursor for new text
O
Delete line and substitute text
S
Overstrike existing characters with new text
R
Join current and next line
J
Toggle case
~
Repeat last action
.
Undo last change
u
Restore line to original state
U
You can get by in vi using only the commands listed in these tables. However, in order
to harness the real power of vi (and increase your own productivity), you will need
more tools. The following chapters describe those tools.
Review of Basic vi Commands | 33
CHAPTER 3
Moving Around in a Hurry
You will not use vi just to create new files. Youll spend a lot of your time in vi editing
existing files. You rarely want to simply open to the first line in the file and move through
it line by line; you want to get to a specific place in a file and start working.
All edits start with you moving the cursor to where you want to begin the edit (or, with
ex line editor commands, by identifying the line numbers to be edited). This chapter
shows you how to think about movement in a variety of ways (by screens, by text, by
patterns, or by line numbers). There are many ways to move in vi, since editing speed
depends on getting to your destination with only a few keystrokes.
This chapter covers:
Movement by screens
Movement by text blocks
Movement by searches for patterns
Movement by line number
Movement by Screens
When you read a book, you think of places in the book in terms of pages: the page
where you stopped reading or the page number in an index. You dont have this con-
venience when youre editing files. Some files take up only a few lines, and you can see
the whole file at once. But many files have hundreds (or thousands!) of lines.
You can think of a file as text on a long roll of paper. The screen is a window of (usually)
24 lines of text on that long roll.
In insert mode, as you fill up the screen with text, you will end up typing on the bottom
line of the screen. When you reach the end and press ENTER , the top line rolls out of
sight, and a blank line appears on the bottom of the screen for new text. This is called
scrolling .
35
In command mode, you can move through a file to see any text in it by scrolling the
screen ahead or back. And, since cursor movements can be multiplied by numeric
prefixes, you can move quickly to anywhere in your file.
Scrolling the Screen
There are vi commands to scroll forward and backward through the file
CTRL
F by full and half screens:
^F
Scroll forward one screen.
^B
Scroll backward one screen.
^D
Scroll forward half screen (down).
^U
Scroll backward half screen (up).
(In this list of commands, the ^ symbol represents the CTRL key. So ^F means to hold
down the CTRL key and press the f key simultaneously.)
There are also commands to scroll the screen up one line (^E) and down one line (^Y).
However, these two commands do not send the cursor to the beginning of the line. The