Book: LPI Linux Certification in a Nutshell
Section: Chapter 16.  Text-Editing, Processing, and Printing (Topic 1.7)



16.1 Objective 1: Perform Basic File Editing Operations Using vi

When working on multiple systems, the availability of a text editor may be of prime importance. Since an editor is an essential configuration tool for Linux, learning at least the basics of the resident editor is a requirement. For Linux and nearly all other Unix systems, the universally available editor is vi. This Objective covers vi basics.[1]

[1] Some Linux systems come with a newer version of vi, called vim, which is an open source vi clone, with improvements. Additional information on vim can be found online at http://www.vim.org/.

16.1.1 Invoking vi

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.

To start vi, simply provide one or more text files on the command line:

$ vi file1.txt file2.txt

You are presented with a main window showing the contents of file1.txt, or if the specified files don't already exist, a blank screen with tilde (~) characters running the length of the left column (they indicate areas of the screen containing no text, not even blank lines).

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.

16.1.2 Terminating vi

Once you've started vi, the first thing you need to know is how to stop it. When in command mode, you can use any of the key sequences shown in Table 16-1. If you're in insert mode, you must first switch back to command mode in order to exit, by pressing the Esc.

Table 16-1. Common Commands for Exiting vi

Key Command

Description

:n

Next file; when multiple files are specified for editing, this command loads the next file.

:q

Quit without saving changes.

:q!

Quit without saving changes and without confirmation.

:wq

Write the file contents (if changed) and quit.

:x

Write the file contents (if changed) and quit (the ex equivalent of ZZ).

ZZ

Write the file contents (if changed) and quit.

16.1.3 Basic Navigation Commands

While in command mode, you can move around your file by character, word, sentence, paragraph, and major section. You can position the cursor at various places in lines or relative to the screen. Table 16-2 lists some of the most frequently used navigation commands.

Table 16-2. Commands for Moving Around in vi

Key Command

Description

Ctrl-b

Move up one screen.

Ctrl-f

Move down one screen.

0 (zero)

Move to the beginning of the current line.

^

Move to the first non-whitespace character on the current line.

$

Move to the end of the current line.

b

Move backward one word.

G

Move to the end of the file.

h

Move left one character.

H

Move to the top of the screen.

j

Move down one line.

k

Move up one line.

l

Move right one character.

L

Move to the bottom of the screen.

w

Move forward one word.

16.1.4 Basic Editing Commands

To edit in vi, you use one of its text-editing commands, including those that enter insert mode, copy and paste, and search for text. Here are some of the frequently used editing commands, grouped by category:

Inserting

To insert new text, first navigate to the location where the text belongs, then enter insert mode and begin typing:

i

Enter insert mode to place text before the cursor.

a

Enter insert mode to append, or place text after the cursor.

Editing

Here are a few handy editing commands:

C

Delete to end-of-line and enter insert mode.

R

Enter replace mode (a variant of insert mode) and overwrite existing characters.

Deleting

Delete a text block defined by a movement command relative to the location where the command started:

dl

Delete the next character.

dw

Delete the current word.

dG

Delete to end-of-file.

dd

Delete the entire current line.

D

Delete to end-of-line (same as d$).

Copy and paste

The yank[2] command is used to copy a text block defined by a movement command relative to the location where the command started:

[2] Emacs users should be careful not to confuse the vi definition of yank (copy) with that of Emacs (paste).

yl

Yank forward one character.

yw

Yank forward one word.

yG

Yank to end-of-file.

yy

Yank the entire current line.

Paste operations insert text that was previously cut:

P

Paste text one line above the cursor.

p

Paste text one line below the cursor.

Find

The following commands can be used to search for text:

/pattern

Search forward for pattern.

?pattern

Search backward for pattern.

n

Repeat the last search.

N

Repeat the last search in the opposite direction.

On the Exam

You'll need to be familiar with vi's command and insert modes, how to switch between them, and how to perform basic navigation and editing tasks.