Using the vi Editor

Using the vi Editor

Linux relies on a large number of text files for configuration. Therefore, you need a text editor to configure Linux. The vi editor may seem old. It certainly isn t the most popular editor even in the Linux community. The one- or two-letter commands are cryptic, but if you ever need to rescue your system with a boot disk, vi may be the only editor at your disposal.

It is easy to open a file with vi . For example, if you want to open the /etc/inittab file, use this command:

 # vi /etc/inittab 

There are three basic ways to work in vi . Command mode is the default; you use insert mode when you want to insert text; and with a few special characters , execute mode can be used to run regular shell commands.

Command Mode

When you open a file in vi , the first mode is command mode. This is what you use to scroll through text, search for different text strings, or delete specific characters, words, or lines.

One aid in vi is line numbers , which you can activate by typing the following in the editor, which should lead to a result that looks similar to Figure 6.3:

 :set nu 
click to expand
Figure 6.3: vi with line numbers

Getting Around

Although current versions of vi allow you to use the directional keys on your keyboard (arrows, Page Up, Page Down), this editor was designed for older U.S. keyboards that did not have these keys. Four lowercase letters take the place of the navigational arrows on the standard U.S. keyboard:

h

Left arrow

j

Down arrow

k

Up arrow

l

Right arrow

The alternatives to the Page Up and Page Down keys are Ctrl+B (back) and Ctrl+F (forward), respectively.

If you already know the line number you want, the G command can help. When used alone, it takes you to the last line in the file. When used with a line number, such as 20G , it takes you to the desired line. As with Linux shells , case makes a difference, so make sure that you re using the uppercase G for this command.

Deleting Text

It is easy to delete text in vi . Three deletion commands are associated with the current location of the cursor:

x

Deletes the current character, even if that character is a blank space or a tab

dw

Deletes the current word

dd

Deletes the current line

If you accidentally delete something, the u command reverses the last command entered.

Searching for Text

It is easy to search for text in vi . Just start with a forward slash. For example, if you want to search for the word dollar in a file, type the following:

 /dollar 

The cursor highlights the first place this word is found in the file. To proceed to the next instance of this word, type n . Just remember, case matters in a search in the vi editor.

Insert Mode

When you want to insert text into a file, use insert mode. There are several ways to do this, relative to the current location of the cursor (see Table 6.11).

Table 6.11: Insert Mode Options

Command

Action

Comment

i

Insert

Everything you type is inserted, starting at the current position of the cursor.

a

Append

Everything you type is inserted, starting one character after the current position of the cursor. This is closely related to A , where everything you type is inserted, starting at the end of the line with the cursor.

o

Open

Everything you type is inserted, starting one line below the current position of the cursor. Closely related is O (uppercase), where everything you type is inserted, starting one line above the current position of the cursor.

cw

Change word

Deletes the word (or space) that corresponds to the current position of the cursor. You get to insert text starting with that word.

In any case, getting out of insert mode is easy; just press the Esc key on your keyboard.

Execute Mode

You can run regular shell commands from inside the vi editor. Just type :! , followed by the command. For example, if you were creating a script, you might need to know the directory location of a certain file. You could list the files in the /etc/cron.daily directory with the following command:

 :!ls /etc/cron.daily 

Regular execute mode starts with the colon ( : ). Several file management commands are associated with execute mode, including :q (to exit a file) and :w (to write the current text to the file). A number of basic commands for vi in all modes are shown in Table 6.12.

Tip  

If you want to exit from vi without saving any changes, use the !q command.

Table 6.12: Basic vi Commands

Command

Description

a

Starts insert mode after the current cursor position.

A

Starts insert mode by appending at the end of the current line.

cw

Deletes the current word and then enters insert mode to allow you to replace that word.

dw

Deletes the current word without entering insert mode.

dd

Deletes the current line.

G

Moves the cursor to the end of the line.

15G

Moves the cursor to the fifteenth line.

h

Moves the cursor left one space.

I

Enters insert mode.

o

Enters insert mode by opening a line directly below the current cursor.

O

(Uppercase O) Enters insert mode by opening a line directly above the current cursor.

:q

Exits from vi . If you have made changes and want to quit without saving, use :q! .

r

Replace; the next character that you type replaces the current character.

:set nu

Activates line numbers for the current file.

u

Undoes the last change.

:w

Writes the current file.

Esc

Exits from insert mode.

/system

Searches for the word system in the current file.

 


Mastering Red Hat Linux 9
Building Tablet PC Applications (Pro-Developer)
ISBN: 078214179X
EAN: 2147483647
Year: 2005
Pages: 220

Similar book on Amazon

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