|
|
|
|
18.4 The vi Editor
Text editors are still used for many system administration tasks
as well as for programming. In the world of Unix, vi stands out as an
editor that offers comfortable editing functions and is more ergonomic
than many editors with mouse support.
18.4.1 Operating Modes
NOTE: Display of Keys
In the following, find several commands that you can enter in vi
by just pressing keys. These appear in uppercase as on a keyboard. If you
need to
enter a key in uppercase, this is stated explicitly by showing a key
combination including the key.
Basically, vi makes use of three operating modes:
insert mode, command mode,
and extended mode. The keys have different
functions depending on the mode. On start-up, vi is normally set to
the command mode. The first thing to learn is
how to switch between the modes:
- Command Mode to Insert Mode
-
There are many possibilities, including A
for append, I for insert, or O
for a new line under the current line.
- Insert Mode to Command Mode
-
Press Esc to exit the
insert mode.
vi cannot be terminated in
insert mode, so it is important to get used
to pressing Esc.
- Command Mode to Extended Mode
-
The extended mode of vi can be
activated by entering a colon (:). The
extended or ex mode is
similar to an independent line-oriented editor that can be used
for various simple and more complex tasks.
- Extended Mode to Command Mode
-
After executing a command in extended
mode, the editor automatically returns to
command mode. If you decide not to execute
any command in extended mode, delete the
colon with . The editor returns to
command mode.
It is not possible to switch directly from
insert mode to extended
mode without first switching to command mode.
vi, like other editors, has its own procedure for terminating
the program. You cannot terminate vi while in
insert mode. First, exit
insert mode by pressing Esc.
Subsequently, you have two options:
-
Exit without saving: To terminate the editor
without saving the changes, enter
:
Q
!
in command mode. The exclamation
mark (!) causes vi to ignore any changes.
-
Save and exit: There are several possibilities
to save your changes and terminate the editor. In
command mode, use
Z
Z
. To exit the program saving all changes using the
extended mode, enter
:
W
Q
. In extended mode,
w stands for write and q for
quit.
18.4.2 vi in Action
vi can be used as a normal editor. In
insert mode, enter text and delete text with the
and keys.
Use the arrow keys to move the cursor.
However, these control keys often cause problems, because there
are many terminal types that use special key codes. This is where the
command mode comes into play. Press
Esc to switch from insert mode
to command mode. In command
mode, move the cursor with H, J,
K, and L. The keys have the
following functions:
-
H
-
Move one character to the left
-
J
-
Move one line down
-
K
-
Move one line up
-
L
-
Move one character to the right
The commands in command mode allow diverse
variations. To execute a command several times, simply enter the
number of repetitions before entering the actual command. For
example, enter 5
L to move the cursor five characters to the right.
A selection of important commands is shown in Table 18-2 This list is far from complete. More
complete lists are available in the documentation found in Section 18.4.3,
For More Information
Table 18-2 Simple Commands of the vi Editor
Esc
|
Change to command mode |
I
|
Change to insert mode (characters appear at the current
cursor position) |
A
|
Change to insert mode (characters are inserted after the
current cursor position) |
A
|
Change to insert mode (characters are added at the end of
the line) |
R
|
Change to replace mode (overwrite the old text) |
R
|
Replace the character under the cursor |
O
|
Change to insert mode (a new line is inserted after the
current one) |
O
|
Change to insert mode (a new line is inserted before the
current one) |
X
|
Delete the current character |
D
D
|
Delete the current line |
D
W
|
Delete up to the end of the current word |
C
W
|
Change to insert mode (the rest of the current word is
overwritten by the next entries you make) |
U
|
Undo the last command |
Ctrl
R
|
Redo the change that was undone |
J
|
Join the following line with the current one |
.
|
Repeat the last command |
18.4.3 For More Information
vi supports a wide range of commands. It enables the use of
macros, shortcuts, named buffers, and many other useful features. A
detailed description of the various options would exceed the scope of
this manual. SUSE® Linux Enterprise comes with vim (vi improved), an
improved version of vi. There are numerous information sources for
this application:
|
|
|