The vi Editor

Operating the vi editor takes some practice. For many, it is the preferred editor, partly because it is available on any UNIX-like operating system and is included in default Linux installations. Also, if nothing else works, vi will. The short instructions that follow should enable you to edit various configuration files and other types of files with vi.

vi provides three operating modes. In command mode keys are interpreted as command elements. Insert mode interprets all keys as text entries and last line mode is used for more complex commands, which are entered in the last line.

The most important commands in command mode are:

Table 24.1. Simple Commands of the vi Editor

ESCChanges to last line mode.
iChanges to insert mode (characters appear at the current cursor position).
aChanges to insert mode (characters appear after the current cursor position).
AChanges to insert mode (characters are added at the end of the line).
RChanges to command mode (overwrites the old text).
rChanges to insert mode and overwrites each character.
sChanges to insert mode (the character where the cursor is positioned is replaced by the next entry you make).
CChanges to insert mode (the rest of the line is replaced by the new text).
oChanges to insert mode (a new line is inserted following the current one).
OChanges to insert mode (a new line is inserted preceding the current one).
xDeletes the current character.
ddDeletes the current line.
dwDeletes up to the end of the current word.
cwChanges to insert mode (the rest of the current word is overwritten by the next entries you make).
uUndoes the last command.
JJoins the following line with the current one.
.Repeats the last command.
:Changes to last line mode.

Each command can be preceded by a number specifying on how many objects the following command should operate. Delete three words at once by entering 3dw. The command 10x deletes ten characters after the cursor position and 20dd deletes twenty lines.

The most important commands in last line mode are:

Table 24.2. Complex Commands of the vi Editor

:q!exits vi without saving any changes
:w filenamesaves as filename
:xsaves the modified file and exits the editor
:e filenameedits (loads) filename
:uundoes the last edit command