vi Basics

The vi editor has two modes of operation: command or insert. In command mode, vi allows you to navigate around your file and enter commands. To enter new text, put vi into insert mode. In command mode, the keyboard keys are interpreted as vi commands instead of text. The convenience of being able to manipulate the editor without moving your hands from the keyboard is considered one of vi’s strengths.

Commands are brief, case-sensitive combinations of one or more letters. For example, to switch from command to insert mode, press the “i” key. To terminate insert mode, press the Escape key (Esc), which puts you back in command mode.

Almost any command can be prefixed with a number to repeat the command that number of times. For example, r will replace the character at the current cursor position. To replace exactly 10 characters, use 10r. Commonly used vi commands are listed in Table 6-12.

Table 6-12. vi commands

Key command

Description

h or left arrow

Move left one character.

j or down arrow

Move down one line.

k or up arrow

Move up one line.

l or right arrow

Move right one character.

H

Move to the top of the screen.

L

Move to the bottom of the screen.

G

Move to the end of the file.

w

Move forward one word.

b

Move backward one word.

0 (zero)

Move to the beginning of the current line.

^

Move to the first nonwhitespace character on the current line.

$

Move to the end of the current line.

Ctrl-B

Move up (back) one screen.

Ctrl-F

Move down (forward) one screen.

i

Insert at the current cursor position.

I

Insert ...

Get LPI Linux Certification in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.