<<

. 16
( 69 .)



>>

commands to change to Input mode.
word or phrase you are seeking, the vi editor automatically
moves the cursor to the next occurrence of that text. If you
The Command-mode keystrokes in the vi editor are usually
type a question mark (?) instead of a forward slash, the
a single letter, and, unlike the Pico editor commands, they
search occurs backwards in the file from your current
are typed without holding down the Control key. However,
position.
the keystroke commands are case sensitive, which means
that a is a different command than A. You need to use the
Shift key, of course, to create the uppercase letters.


MOVEMENT WITHIN VI




¤ Press Shift + G to move to ‹ Press Shift + 6 to move to › Press Shift + 4 to move to
MOVING IN VI
the end of the file. the beginning of a line. the end of the line.
„ With vi in Command
mode, press Shift + H to
move to the start of the file.




66
5
WORK WITH TEXT EDITORS



The commands in the table below allow you to move
quickly through a file in vi, repositioning the cursor and
scrolling the screen when necessary.

KEYSTROKE EFFECT
Move to the first line of the file.
H
Move to the last line of the file.
G
Move one character to the left.
h
Move down one line.
j
Move up one line.
k
Move one character to the right.
l
Move to the beginning of the current line.
^
Move to the end of the current line.
$
Move ahead one word.
w
Find text from the cursor forward.
/text
Find text from the cursor backward.
?text




– The vi editor moves the
¤ Type a word or phrase
FINDING TEXT IN VI
and press Return. cursor to that word or phrase.
„ Type /.




67
UNIX FOR MAC




ENTER TEXT IN VI
and that is to press the Esc (Escape) key, located on the

Y
ou can enter text into a document by switching to
upper-left corner of most keyboards. If you are not sure
Input mode in vi. When you start the vi editor, it
which mode you are in, you can press the Esc key to return
switches to Command mode by default, allowing you
to Command mode. Pressing the Esc key while in
to move through the file or enter keystroke commands. If
Command mode does nothing, so it is safe to press this key
you try to enter text while in Command mode, you can
several times to make sure.
produce some very strange results, because many
characters correspond to keystroke commands in
Many useful command-mode keystrokes appear in the table
Command mode. It is therefore very important to know
on the facing page. The most important keystrokes are i,
which mode you are in before you enter text.
which inserts text at the current cursor location, and o,
which opens a new line below the current one and allows
The easiest way to determine your current mode is to
you to enter text directly. When you edit the text in your
enable the option showmode. You can do this by typing :set
file, vi does not save your changes until you give the
showmode while in Command mode. Your current mode
command to do so.
appears in the lower right corner.

You can use a number of single keystrokes to switch to
Input mode from Command mode. However, there is only
one way to switch to Command mode from Input mode,


ENTER TEXT IN VI




– The vi editor switches to
¤ Type the letter i. › Press Esc.
INSERT TEXT
Input mode.
„ Position the cursor where – The vi editor switches to
‹ Type the text you want to
you want to enter text. Command mode.
insert.


68
5
WORK WITH TEXT EDITORS


The keystroke commands that you use to switch to Insert
mode appear in the table below. There are other modes that
function like Insert mode, allowing you to type text into the
file. As with Insert mode, you can leave these modes by
pressing the Esc key to switch to Command mode.

KEYSTROKE MODE ENTERED FUNCTION
Insert Append text at the end of the current line.
A
Insert Append text after the current character.
a
Insert Insert text at the beginning of the current line.
I
Insert Insert text at the current location.
i
Insert Insert a new line before the current line.
O
Insert Insert a new line after the current line.
o
Replace Replace existing text with the text you type.
R
Replace Replace a single character with the text you type.
r
Change Substitute the current line with one you type.
S
Change Substitute a single character with the text you type.
s




– The vi editor switches to
¤ Type the letter o. ‹ Type the text you want to
OPEN A NEW LINE OF TEXT
insert. Command mode.
„ Position the cursor to the – The vi editor switches to
› Press Esc.
line above where you want to Input mode and opens a new
insert a new line. blank line.


69
UNIX FOR MAC




DELETE TEXT IN VI
You can delete larger sections of text by typing the letter d

Y
ou can delete text from a file using keystroke
and applying a movement command. For example, to delete
commands. If you press the Delete key while in
from the current location to the beginning of the line, you
Command mode, this generates the symbols, ^?,
can type d^, because the ^ keystroke moves the cursor to
which do not correspond to any command known to vi, and
the beginning of the line. To delete the current line, you can
an error message appears. In Command mode, the Delete
type dd. If you want to delete everything from the current
key has no function. You can use other keystroke
position to the end of the current line, you can type either
commands to delete text.
D or d$. A list of deletion commands appears in the table
on the facing page. When you edit the text in your file, vi
To delete content from your document, you must first move
does not save your changes until you give the command to
to the location of the unwanted text using the arrow keys or
do so.
a movement command. Deletion commands use the
current location of the cursor as the starting point of the
deletion.

You can delete the character underneath the cursor by
typing a lowercase x. If there is more text on the line, it
shifts to the left to fill the space formerly occupied by the
deleted character. A capital X deletes the character to the
left of the cursor.


DELETE TEXT IN VI




– The vi editor deletes the
¤ Type the letter x.
DELETE CHARACTERS
character.
„ Position the cursor over
the character you want to
delete.


70
5
WORK WITH TEXT EDITORS




You can use these commands to remove text from the file you
are editing. If you make a mistake, you can type the keystroke
command u, which undoes the most recent change. You can
use this command to undo insertions as well as deletions.

KEYSTROKES EFFECT
Delete from current position to end of line.
D
Delete current line.
dd
Delete from current position to end of file.
dG
Delete current word, from current position to end of word.
dw
Delete from current position to beginning of line.
d^
Delete from current position to end of line.
d$
Delete from current position to next occurrence of text.
d/text
Undo last command that changed the file.
u
Delete character to left of cursor.
X
Delete character under cursor.
x




– The vi editor deletes the
¤ Type dd.

<<

. 16
( 69 .)



>>