Editing Text Files


A chapter on working with SLES would not be complete without a discussion on one more important topicediting text files. Although editing text files may not sound like a difficult subject, common tools used when editing text files under Linux tend to be rather cryptic. Devoting some space in this chapter to help clear up confusion in this area should make first-time administrators much more comfortable and effective with any task involving text editing.

The vi Utility

It is possible to use graphical text-editing tools under SLES. Many such tools are easily available in both the KDE and GNOME graphical environments. However, a reliance on graphical editors (such as Kate or Gedit) also produces a dependence on a graphical environment. This graphical environment is often not running on a SLES machine. Also, when using a remote shell for SLES administration, graphical tools require tunneling X over SSH. Rather than enabling this tunneling or always using a graphical session on the server, learning and using a command-line editor is more effective and a better solution for the long term.

As in the graphical environment, several command-line text editors are available. Among the choices for command-line editors are pico, emacs, and vi. Pico and emacs are both powerful text-editing utilities that offer several appealing qualities. However, these utilities are not always available on SLES installations. The vi utility is the only text-editing utility that is nearly guaranteed to be available with any Linux installation.

NOTE

The vi utility on SLES is actually the Vi IMproved, or vim utility. As vim is designed to be compatible to vi, it is still commonly referred to as vi.


The vi utility is an extremely powerful text-editing utility. Unfortunately, the almost limitless capabilities of vi have also caused its usage to become fairly cryptic and challenging for new users. The most important vi concept to understand is that vi operates in a bimodal fashion. Bimodal means that when using vi, you can be in one of two possible modescommand mode or insert mode.

The command-mode environment is used for such tasks as performing file operations, copying and pasting text, performing global search-and-replace operations, and working with macros. The insert mode of vi is used for normal text-editing operations. Probably more important than the two modes themselves is the ability to determine what mode vi is currently in!

When vi is initially launched, the interface is left in command mode. It is important to understand that direct file editing is not possible in command mode. Text can be copied and pasted, and various actions such as deleting text can still be accomplished, but adding additional text to the file by typing is not possible. In order to add text to the document, you must first switch to insert mode.

Insert mode is entered by pressing "i" or one of a few other insert text keys. When you are in insert mode, the bottom of the vi window will display the text " INSERT ". Prior to adding text to the file, be sure you are in the correct mode by looking at the bottom of the window for that text. When you're in insert mode, you can switch back to command mode by pressing the ESC key. Table 3.20 lists some of the commands that can be used with insert mode.

Table 3.20. Commands Used to Enter Insert Mode

KEYSTROKE

DESCRIPTION

i

Change to insert mode and place the active cursor before the current character.

I

Change to insert mode and place the active cursor at the beginning of the current line.

a

Change to insert mode and place the active cursor after the current character.

A

Change to insert mode and place the active cursor at the end of the current line.

o

Change to insert mode and insert a new blank line after the current line. Place the active cursor at the beginning of the new line.

O

Change to insert mode and insert a new blank line before the current line. Place the active cursor at the beginning of the new line.

[ESC]

Exit insert mode and return to command mode.


As previously mentioned, the command mode is used to perform file operations, global text operations, and several other possible actions. These operations are either entered via one of several keystroke combinations, or through a special ":" prompt within vi. A few of the possible commands issued via the : prompt are documented in Table 3.21.

Table 3.21. Common Prompt Commands Used in Command Mode

COMMAND

DESCRIPTION

:q

Quit vi.

:q!

Quit without saving.

:w

Write (save) file.

:w filename

Write file as new filename.

:wq

Write file and quit vi.

:!date

Invoke command shell and run specified command. In `this case, date is executed.

:r!date

Invoke command shell, run specified command, and retrieve output into current file. In this case, date is executed and the output is added to the file at the current cursor location.

:1,$ s/Bill/Bob/g

Starting from the first line and continuing until the end of the file, search and replace every instance of "Bill" with "Bob".


Table 3.22 lists keystroke combinations that can be used when in the command mode of vi.

Table 3.22. Common Keystroke Combinations Used in Command Mode

KEYSTROKE

DESCRIPTION

h,j,k,l (or arrow keys)

Move cursor position left, down, up, and right.

w,b

Move cursor forward and back one word at a time.

G

Move to last line of file.

<linenum>G

Move to specified line number. Use 1G to move to the first line of the file.

^

Move cursor to beginning of current line.

$

Move cursor to end of current line.

x, DEL

Delete current character.

5x

Delete five characters beginning at the current cursor position.

dw

Delete current word.

d5w

Delete five words beginning with the word the cursor is currently on.

d$

Delete contents of current line, from current cursor position to end of line.

d^

Delete contents of current line, from current cursor position to beginning of line.

yw

Copy current word to buffer.

y5w

Copy five words to buffer beginning with the word the cursor is currently on.

yy

Copy current line to buffer.

y3y

Copy next three lines to buffer.

dd

Delete current line (stores line in buffer).

d3d

Delete next three lines (stores lines in buffer).

p

Paste contents of buffer underneath current line.

P

Paste contents of buffer above current line.

^

Move cursor to beginning of current line.

$

Move cursor to end of current line.

u

Undo previous operation.

.

Repeat previous operation.

/pattern

Search in the forward direction for specified pattern.

?pattern

Search in the reverse direction for specified pattern.

n,N

Find next and previous instance of search criteria.


TIP

For a great introduction to vi, execute vimtutor from a command prompt and follow the instructions. Upon completion of the tutorial, most important vi commands should at least be a little more familiar when you need to execute them again!


These three tables of vi commands have just barely scratched the surface of what vi is capable of. Although it may seem challenging, the most effective way to learn vi is by using it. When you are proficient with the commands outlined here, enter :help from the command mode of vi for more usage information.



    NovellR Open Enterprise Server Administrator's Handbook SUSE LINUX Edition
    Novell Open Enterprise Server Administrators Handbook, SUSE LINUX Edition
    ISBN: 067232749X
    EAN: 2147483647
    Year: 2005
    Pages: 178

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