By Linda Lamb, Arnold Robbins
Price: $29.95 USD
£20.95 GBP
Cover | Table of Contents | Colophon


vi is the UNIX command that invokes the vi editor for
an existing file or for a brand new file.
The syntax for the vi command is:$ vi [filename]
$ is the UNIX prompt.
If the filename is omitted,
vi will open an unnamed buffer.
You can assign the name when you write the buffer into a file.
For right now, though,
let's stick to naming the file on the command line.vi command.
For example, if you want to open a new
file called :e! RETURN
:q! RETURN
:e or :q command causes
vi to override this prohibition, performing the operation even
though the buffer has been modified.File exists
File file exists - use w!
[Existing file]
File is read only:w!
file to overwrite the existing file, or type
:w
newfile to save the edited version in a new file.:w
newfile to write out the buffer into a new file.
If you have write permission for the directory, you can use
mv to replace the original version with your copy of it.
If you don't have write permission for the directory,
type :w
pathname/file to
write out the buffer to a directory in which you do have write
permission (such as your home directory, or /tmp).:!rm
junkfile to delete
a (large) unneeded file and
free some space.
(Starting an
There are several ways to tell vi that you want to begin insert mode.
One of the most common is to press i.
The i doesn't 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
There are several ways to tell vi that you want to begin insert mode.
One of the most common is to press i.
The i doesn't 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.iintroduction,
what appears on the screen is:introduction
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, backspace and
type over the error. Depending on the type of terminal you are using,
backspacing may erase what you've previously typed or may just back
up over it.
In either case, whatever you back up over will be
deleted. Note that you
can't use the backspace key to back up beyond the point where you
entered insert mode.h, j, k, and l, right under your
fingertips, will move the cursor:h
j
k
l
+ and - to go up and down,
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.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 you're 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 i for insert (which you've
already seen); a for append; c for change; and
d for delete. To move or copy text, you use a pair 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.
| Keystrokes | Results |
|---|---|
2k
|
Move the cursor up two lines with the
k command,
to the line where you want to make the insertion. |
iWith a
|
Press
i to enter insert mode and begin inserting text. |
screen editor
ESC
|
Finish inserting text, and press
ESC
to end the insert and return to command mode.
|
itext to be inserted ESC
a command.
There are other insert commands for inserting text at different
positions relative to the cursor:A
I
o
O
s
S
R
A (append) and I (insert) save you from having to
move your cursor to the end or beginning of the line before
invoking insert mode.
(The A command saves one keystroke over $a.
Although one keystroke might not seem like much of a saving,
the more adept—and impatient—an editor you become, the more
keystrokes you will want to omit.)o and O (open) save you from having to
insert a carriage return. You can type these commands from
anywhere within the line.s and S (substitute) allow you to delete a character
or a whole line
and replace the deletion with any amount of new text.
s is the equivalent of the two-stroke command c
SPACE
and S is the same as cc. One of the best uses
for s is to change one character to several characters.R ("large" replace) is useful when you
want to start changing text, but you don't know exactly how much. For
example, instead of guessing whether to say 3cw or
4cw, just type R and then enter your replacement text.o and O, the above insert commands
(plus i and a) take numeric prefixes.
With numeric prefixes, you might use the commands
i, I, a, and A to
insert a row of underlines or alternating characters.
For example, typing 50i*
ESC
inserts 50 asterisks, and typing
Sometimes while editing a file you will end up with a series of
short lines that are difficult to scan.
When you want to merge two lines into one, position the cursor
anywhere on the first line, and press J to join the two lines.
| Keystrokes | Results |
|---|---|
J
|
J joins the line the cursor is on with the line below. |
.
|
Repeat the last command (
J) with the .
to join the next line with the current line. |
J joins that number
of consecutive lines.
In the example above, you could have joined three lines by using
the command 3J.J command when you mean j.I,
A, J, etc.) are different from lowercase commands
(i, a, j), so all your commands are being
interpreted not as lowercase but as uppercase commands.
Press the
CAPS LOCK
key again to return to lowercase,
press
ESC
to ensure that you are in command mode,
then type either U to restore the
last line changed or u to undo the last command.
You'll probably also have to do some additional editing to fully
restore the garbled part of your file.c, d, and y with various text objects.
The last two rows show additional commands for editing.
Table 2.2 and Table 2.3
list some other basic commands. Table 2.4
summarizes the rest of the commands described in this chapter.
| Text Object | Change | Delete | Copy |
|---|---|---|---|
| 1 word |
cw
|
dw
|
yw
|
|
2 words, not counting punctuation
| 2cW or c2W
| 2dW or d2W
| 2yW or y2W
|
| 3 words back | 3cb or c3b
| 3db or d3b
| 3yb or y3b
|
| 1 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
|
| 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
|
| Backward by word | b or B
|
| To end of line |
$
|
| To beginning of line |
0
|
| Operations |
|---|

There are vi commands to scroll forward and backward through
the file by full and half screens:^F
^B
^D
^U

There are vi commands to scroll forward and backward through
the file by full and half screens:^F
^B
^D
^U
^ symbol represents the
CTRL
key. ^F means to hold down the
CTRL
key and press the f key simultaneously.)^E) and down one line
(^Y).
However, these two commands do not send the cursor to the beginning
of the line.
The cursor remains at the same point in the line as when the command
was issued.
If you want to scroll the screen up or down, but you want the cursor
to remain on the line where you left it, use the z command.
z
RETURN
|
Move current line to top of screen and scroll.
|
z.
|
Move current line to center of screen and scroll.
|
Another way that
you can think of moving through a vi
file is by text blocks—words, sentences, paragraphs,
or sections.w, W, b or
B). In addition, you can use these commands:e
E
(
)
{
}
[[
]]
?
.
!.
vi locates the end of a sentence when the
punctuation is followed by
at least two spaces or when it appears as the last non-blank character on a
line.
If you have left only a single space following a period, or if
the sentence ends with a quotation mark, vi
won't recognize the sentence.:set command, as described in Chapter 7.3) moves ahead three sentences.
Also remember that you can edit using movement
commands:
One of the most useful ways to move around in a large file quickly is by
searching for text, or more properly, a pattern of characters.
Sometimes a search can be performed to find a misspelled word or
to find each occurrence of a variable in a program./ (slash).
When you enter a slash, it appears on the bottom line of the
screen; you then type in the pattern that you
want to find: /
pattern.| Keystrokes | Results |
|---|---|
/edits
|
Search for the pattern edits. Press
RETURN
to enter.
The cursor moves directly to that pattern.
|
/scr
|
Search for the pattern scr.
Press RETURN to enter.
Note that there is no space after scr.
|
:set
nu option described in Chapter 7.
In vi, you can also display the current line number on the bottom
of the screen."practice" line 3 of 6 --50%--
You can use line numbers to move the cursor through a file.
The G (go to) command uses a line number as a numeric argument
and moves directly to that line.
For instance, 44G moves the cursor to
the beginning of line 44. G without a line
number moves the cursor to the last line of the file.` `) returns you to your
original position (the position where you issued the last G
command), unless you have done some edits in the meantime.
If you have made an edit, and then
moved the cursor using some command other than G,
` ` will
return the cursor to the site of your last edit.
If you have issued a
search command (/ or ?),
` ` will
return the cursor to its position when you started the search.
A pair of apostrophes
(| Movement | Command |
|---|---|
|
Scroll forward one screen.
|
^F
|
|
Scroll backward one screen.
|
^B
|
|
Scroll forward half screen.
|
^D
|
|
Scroll backward half screen.
|
^U
|
|
Scroll forward one line.
|
^E
|
|
Scroll backward one line.
|
^Y
|
|
Move current line to top of screen and scroll.
|
z
RETURN
|
|
Move current line to center of screen and scroll.
|
z.
|
|
Move current line to bottom of screen and scroll.
|
z-
|
|
Redraw the screen.
|
^L
|
|
Move to home—top line of screen.
|
H
|
|
Move to middle line of screen.
|
M
|
|
Move to bottom line of screen.
|
L
|
|
Move to first character of next line.
| RETURN |
|
Move to first character of next line.
|
+
|
|
Move to first character of previous line.
|
-
|
|
Move to first non-blank character of current line.
|
^
|
|
Move to column n of current line.
| n |
i, a, c, d, and y.
This chapter expands on what you already know about editing.
It covers:c, d, and y,
as well as how to combine them with movements and numbers (such as
2cw or 4dd).
In Chapter 3,
you added many more
movement commands to your repertoire.
Although the fact that you can combine edit commands with
movement is not a new concept to you,
Table 4.1
gives you a feel for the many editing options you now have.
| Change | Delete | Copy | from Cursor to ... |
|---|---|---|---|
cH
|
dH
|
yH
| top of screen |
cL
|
dL
|
yL
| bottom of screen |
c+
|
d+
|
y+
| next line |
c5|
|
d5|
|
y5|
| column 5 of current line |
2c)
|
2d)
|
2y)
| second sentence following |
c{
|
d{
|
y{
| previous paragraph |
c/
pattern
|
d/
pattern
|
y/
pattern
| pattern |
cn
|
dn
|
yn
| next pattern |
c, d, and y,
as well as how to combine them with movements and numbers (such as
2cw or 4dd).
In Chapter 3,
you added many more
movement commands to your repertoire.
Although the fact that you can combine edit commands with
movement is not a new concept to you,
Table 4.1
gives you a feel for the many editing options you now have.
| Change | Delete | Copy | from Cursor to ... |
|---|---|---|---|
cH
|
dH
|
yH
| top of screen |
cL
|
dL
|
yL
| bottom of screen |
c+
|
d+
|
y+
| next line |
c5|
|
d5|
|
y5|
| column 5 of current line |
2c)
|
2d)
|
2y)
| second sentence following |
c{
|
d{
|
y{
| previous paragraph |
c/
pattern
|
d/
pattern
|
y/
pattern
| pattern |
cn
|
dn
|
yn
| next pattern |
cG
|
dG
|
yG
| end of file |
c13G
|
d13G
|
y13G
| line number 13 |
(number)(command)(text object)
$ vi file
vi command that can be
helpful.
You can open a file directly to a specific line number or pattern.
You can also open a file in read-only mode.
Another option recovers all changes to a file that you were
editing when the system crashed.$ vi +n file$ vi + file$ vi +/pattern file| Keystrokes | Results |
|---|---|
vi +/Screen practice
|
Give the
vi command with the option
+/
pattern
to go directly to the line containing Screen. |
+/"you make"
+/you\ make
+/
pattern is helpful
if you have to leave an editing session in the
middle. You can mark your place by inserting a pattern
such as ZZZ or HERE. Then when you return to the
file, all you have to remember is d or x) or yank (y) is saved in a buffer (a
place in stored memory).
You can access the contents
of that buffer and put the saved text
back in your file with the put command
(p or P).p or P command
immediately after you've made the deletion.)" (double quote),
identify the buffered text by number, then give the put command.
To recover your second-to-last deletion from buffer 2:"2p
"
n
p over
and over again.
If you use the repeat command (.)
with p after u,
it automatically increments the buffer number. As a
result, you can search through the numbered buffers
as follows:"1pu.u.u etc.
u, the restored text is removed; when
you type a dot (.), the contents of the next buffer is restored to
your file.
Keep typing