Section 5.9. Objective 8: Perform Basic File Editing Operations Using vi


5.9. Objective 8: Perform Basic File Editing Operations Using vi

vi is perhaps the most ubiquitous text editor available on Linux systems. Since most system administration tasks eventually require editing text files, being able to work effectively in vi essential.

This Objective concentrates on a subset of vi functionality. Learning the vi Editor (O'Reilly) is an indispensable reference for anyone interested in learning more about vi and the enhancements available in the various implementations of vi. There is also a large amount of documentation available at http://vimdoc.sourceforge.net and http://www.vim.org for the popular vi implementation Vim, most of which is applicable to any version of vi.

5.9.1. Invoking vi

To start vi, simply execute it. You will be editing a temporary file. To directly edit one or more files, give the names of the 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).

5.9.2. 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.

Table 5-9. vi commands

Key command

Description

h

Move left one character.

j

Move down one line.

k

Move up one line.

l

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 non-whitespace 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 at the beginning of the current line.

a

Append after the current cursor position.

A

Append to the end of the current line.

o

Start a new line after the current line.

O

Start a new line before the current line.

r

Replace the character at the current cursor position.

R

Start replacing (overwriting) at the current cursor position.

x

Delete the character at the current cursor position.

X

Delete the character immediately before (to the left) of the current cursor position.

s

Delete the character at the current cursor position and go into insert mode. (This is the equivalent of the combination xi.)

S

Delete the contents of the current line and go into insert mode.

dX

Given a movement command X, cut (delete) the appropriate number of characters, words, or lines from the current cursor position.

dd

Cut the entire current line.

D

Cut from the current cursor position to the end of the line. (This is equivalent to d$.)

cX

Given a movement command X, cut the appropriate number of characters, words, or lines from the current cursor position and go into insert mode.

cc

Cut the entire current line and go into insert mode.

C

Cut from the current cursor position to the end of the line and enter insert mode. (This is equivalent to c$.)

yX

Given a movement command X, copy (yank[a]) the appropriate number of characters, words, or lines from the current cursor position.

yy or Y

Copy the entire current line.

p

Paste after the current cursor position.

P

Paste before the current cursor position.

.

Repeat the last command.

u

Undo the last command.[b]

/regex

Search forward for regex.

?regex

Search backward for regex.

n

Find the next match.

N

Find the previous match. (In other words, repeat the last search in the opposite direction.)

:n

Next file; when multiple files are specified for editing, this command loads the next file. Force this action (if the current file has unsaved changes) with :n!.

:e file

Load file in place of the current file. Force this action with :e! file.

:r file

Insert the contents of file after the current cursor position.

:q

Quit without saving changes. Force this action with :q!.

:w file

Write the current buffer to file. To append to an existing file, use :w >>file. Force the write (when possible, such as when running as root) with :w! file.

:wq

Write the file contents and quit. Force this action with :wq!.

:x

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

ZZ

Write the file contents (if changed) and quit.

:! command

Execute command in a subshell.

[a]

[b]


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

[b] Many of the popular vi implementations support multi-level undo. Vim breaks compatibility with traditional vi by making a repeated u perform another level of undo. Nvi uses . after u to do multi-level undo, and, like traditional vi, uses a repeated u to redo (undo the undo, so to speak). This can cause some confusion when moving between Linux distributions that have different default implementations of vi.


Tip: Keep in mind that this is not a complete list, but it is not necessary to know every vi command to use it effectively. In fact, even after using vi as your only editor for years, you may find yourself only using a small subset of the available commands.

There is a pattern in vi's keyboard commands that makes them easier to remember. For every lowercase character that has some action assigned to it, the same uppercase character usually has some related action assigned to it. As an example, i and I both put vi in insert mode, at the current cursor position and at the beginning of the line respectively.

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.





LPI Linux Certification in a Nutshell
LPI Linux Certification in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596005288
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net