By Hugo Toledo, Jonathan Gennick
Price: $39.95 USD
£28.50 GBP
Cover | Table of Contents | Colophon
termcap
library. In the early 1980s, System V introduced a
binary terminal information database and terminfo library. The two libraries were largely
functionally equivalent. In order to tell vi which terminal you had, you had to set the
TERM environment variable. This was typically done in a shell startup file, such as
.profile or .login.TERM for
you. (You can use vi from a PC non-GUI
console too, of course. This is very useful when doing system recovery
work in single-user mode. There aren’t too many people left who would want
to work this way on a regular basis, though.) For day-to-day use, it is
likely that you will want to use a GUI version of vi, such as Vim or one of the other clones. On a
Microsoft Windows or Mac OS X system, this will probably be the default.
However, when you run 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 practice in the current directory, you would
enter::e!ENTER:q!ENTER:e or :q command causes vi to override this prohibition, performing the
operation even though the buffer has been modified.File exists
Filefile 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
junkfilei. 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 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, pressESC. 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.
(If you have disabled vi compatibility,
Vim allows you to backspace 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 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, 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
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. shows the vi commands you use to make the edits marked in
.
you can scroll the page, move the cursor, deleteines, and insert characters.
| Keystrokes | Results |
|---|---|
2k
|
ou can scroll the page, move the cursor, delete lines, and insert characters. Move the cursor up two lines with the
k command, to the line where you want
to make the insertion. |
iWith a
|
With ayou can scroll the page, move the cursor, delete lines, and insert characters. Press
i to enter insert
mode and begin inserting text. |
screen
editor
ESC
|
With a screen editoryou can scroll the page, move the cursor, delete lines, and insert characters. |
itext to be inserted ESC
a command. Here are some other insert commands
for inserting text at different positions relative to the cursor:A
I
o (lowercase
letter “o”)O (uppercase
letter “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 cSPACE, 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 insert commands just listed
(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 J to join the two lines.ith a screen editor you can scroll the page, move the cursor
| Keystrokes | Results |
|---|---|
J
|
ith a screen editor you can scroll the page, move the cursor J joins the line the
cursor is on with the line below. |
.
|
ith a screen editor you can scroll the page, move the cursor 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 here, 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), 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 lowercase, 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. 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. Tables and list some
other basic commands. summarizes the
rest of the commands described in this chapter. | Text object | Change | Delete | Copy |
|---|---|---|---|
| One word |
cw
|
dw
|
yw
|
|
Two words, not counting
| 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
|
| 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 | 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!
|
| Editing action | Command |
|---|---|
| Insert text at current position |
^F
^B
^D
^F
^B
^D
^U
^ symbol represents the CTRL key. So ^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.z command.z
ENTER
z.
w, W, b or
B). In addition, you can use these
commands:e
E
(
)
{
}
[[
]]
?, ., or !.
vi locates the end of a sentence when
the punctuation is followed by at least two spaces or when it appears as
the last nonblank 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..IP, .PP,
.LP, or .QP) from the troff MS macro package. Similarly, a section is
defined as text up to the next default section macro (.NH, .SH,
.H 1, or .HU). The macros that are recognized as
paragraph or section separators can be customized with the :set command, as described in .3) moves ahead three sentences. Also
remember that you can edit using movement commands: d) deletes to the end of the current sentence,
2y} copies (yanks) two paragraphs
ahead./ (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
|
With a screen editor you can scroll the page, move the cursor, delete lines, insert characters, and more, while seeing the results of yourdits as you make them. Search for the pattern edits. Press
ENTER to enter. The cursor moves
directly to that pattern.
|
/scr
|
With acreen editor you can scroll the page, move the cursor, delete lines, insert characters, and more, while seeing the results of your edits as you make them. |
:set nu
option described in . In vi, you can also display the current line number
on the bottom of the screen."practice" line 3 of 6 --50%--
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 (| 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
ENTER
|
|
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—the 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
| ENTER |
|
Move to first character of next line
|
+
|
|
Move to first character of previous
line
|
-
|
|
Move to first nonblank character of current
line
|
^
|
|
Move to column 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 , 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,
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
|
c, d, and y, as
well as how to combine them with movements and numbers (such as 2cw or 4dd).
In , 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,
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)
c, d, or
$vifile
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
|
With a screen editor you can scroll the page, move the cursor, delete lines, and insert characters, while seeing th |