Using a Command-Line Text Editor


There are a variety of text editors used within the shell, but three stand out as being ubiquitous: ed, vi, and emacs. The first in that list, ed, is by far the simplest. That doesn't necessarily mean that it's simple to use or lacks powerful features, but it simply doesn't match the astonishing power of both vi and emacs. To call vi and emacs simple text editors is to do them a severe disservice, because both are extremely powerful interactive environments. In particular, emacs is considered practically an operating system in itself, and some users of Linux treat it as their shell, executing commands and performing everyday tasks, such as reading and sending e-mail from within it. There are entire books written solely about emacs and vi.

Tip 

A fourth shell-based text text editor found on many Linux systems is pico. This offers many word processor-like features that can be helpful if you've come to Linux from a Windows background. It isn't installed by default on SUSE Linux. To install it, insert your SUSE Linux installation DVD and type the following command at the command-line prompt: yast -i /media/dvd/suse/i586/pico-4.58-145.i586.rpm. After this, you can view the man page by typing man pico.

The downside of all the power within emacs and vi is that both packages can be difficult to learn to use. They're considered idiosyncratic by even their most ardent fans. Both involve the user learning certain unfamiliar concepts, as well as keyboard shortcuts and commands.

Although there are debates about which text editor is better and which is best, it's generally agreed that vi offers substantial text-editing power but isn't too all-encompassing. It's also installed by default on SUSE Linux. On SUSE Linux, emacs must be installed as an optional extra. Both text editors are normally available on virtually every installation of Linux or Unix. We'll concentrate on using vi here.

It's important to understand that there isn't just one program called vi. There are many versions. The original vi program, supplied with Unix, is rarely used nowadays. The most common version of vi is a clone called vim, for vi improved, and this is the version supplied with SUSE Linux. However, there are other versions, such as Elvis. Most work in a virtually identical way.

Note 

There's always been a constant flame war between advocates of vi and emacs, as to which is better. This could be quite a vicious and desperate debate, and the text editor you used was often taken as a measure of your character! Nowadays, the battle between the two camps has softened, and the emacs versus vi debate is considered an entertaining cliché of Linux and Unix use. Declaring online which text editor a user prefers is often followed by a smiley symbol to acknowledge the once-fevered emotions.

Understanding vi Modes

The key to understanding how vi works is to understand the difference between the various modes. Three modes are important: Command mode, Insert mode, and Command-Line mode.

Command Mode

Command mode is vi's central mode. When the editor starts up, it's in Command mode, as shown in Figure 16-2. This lets you move around the text and delete words or lines of text. vi returns to Command mode after most operations. In this mode, the status bar at the bottom of the screen shows information such as the percentage progress through the document. Although you cannot insert text in this mode, you can delete and otherwise manipulate words and lines within the file. You can also move through the text using the cursor keys and the Page Up and Page Down keys.

image from book
Figure 16-2. In vi, the central mode is Command mode.

Table 16-1 shows a list of the commands you can use in Command mode (consider photocopying it and sticking it to the side of your monitor as a handy reference).

Table 16-1. vi Command Mode Commands

Action

Command

Description

Delete text

dd

Delete current line

 

ndd

Delete n number of lines (for example, 5dd will delete five lines)[1]

 

dw

Delete the current word under the cursor[2]

 

db

Delete the word before the cursor[2]

 

D

Delete everything from the cursor to the end of the line[1]

Search

/

Search forward (type the search text straight afterwards)

 

?

Search backwards

 

n

Repeat search in a forward direction

 

N

Repeat search in a backward direction

Cut and paste

yy

Copy the current line[2]

 

nyy

Copy n number of lines into the buffer from the cursor downwards (for example, 5yy copies five lines of text)

 

p

Paste the contents of the clipboard[3]

Insert text

i

Switch to Insert mode at the cursor

 

o

Switch to Insert mode, placing the cursor below current line

 

O

Switch to Insert mode, placing the cursor above current line

Navigation

Cursor keys

Move around the file

 

Page Up/Page Down

Move up and down a page at a time

 

0 (zero)[4]

Move the cursor to the start of the current line

 

Shift+0 (zero)[4]

Move forward one sentence (until the next full stop)

 

$

Move the cursor to the end of the current line

 

w

Move the cursor to the next word

 

b

Move the cursor to the previous word

Miscellaneous

.[4]

Repeat the last command

 

u

Undo the last command

[1]A line ends where a line break control character occurs in the file. Because of this, a line of text may actually take up several lines of the on-screen display.

[2]This will delete the remainder of current word before/after cursor if the cursor is in the middle of a word.

[3]The standard documentation refers to copying as "yanking" and the clipboard as the "buffer."

[4]Press the character on the main keyboard, not the numeric keypad.

Insert Mode

To type your own text or edit text, you need to switch to Insert mode. This is normally done by typing i, but you can also type O or o to change to Insert mode, which is indicated by the word INSERT appearing at the bottom of the screen, as shown in Figure 16-3. The difference between the commands required to switch into Insert mode is that some let you insert before or after the cursor. Generally, i is most useful, because what you type will appear before the character under the cursor, as with most word processors. See Table 16-1 for a description of how the other insert commands work.

image from book
Figure 16-3. Use vi's Insert mode to add and edit text.

In Insert mode, you can still move around the text using the cursor keys. Anything you type will appear the point of the cursor. To quit this mode, press the Esc key. This will return you to Command mode.

Command-Line Mode

The third mode you should be aware of is Command-Line mode (note that, irritatingly, this is not the same as the Command mode). As its name suggests, this is the mode in which you can enter commands to save and load files, as well as perform other fundamental tasks to control vi or to quit the program. You can enter Command-Line mode by typing a colon (:), although if you're in Insert mode, you'll first need to leave it by pressing the Esc key. You can identify when vi is in this mode because the cursor will be at the bottom of the screen next to a colon symbol, as shown in Figure 16-4. To quit Command-Line mode, press the Esc key. You'll be returned to Command mode. Note that you'll automatically leave Command-Line mode after each command you issue has completed.

image from book
Figure 16-4. Use vi's Command-Line mode to issue commands.

For a list of basic Command-Line mode commands, see Table 16-2.

Table 16-2. Some vi Command-Line Mode Commands

Command

Description

:w

Save file to disc

:w!

Save file and ignore errors such as an existing file with the same

 

filename

:q

Quit vi

:q!

Quit vi and ignore errors such as an unsaved file

:s/word/replacement/

Search from the cursor downwards and replace any examples of word with replacement [1]

:help

View help documentation

[1] The search tool is very powerful and uses a number of command options for additional flexibility. Read the vi help file to learn more.

Using vi to Edit a File

As an example, let's use vi to edit the OpenOffice.org README file. We don't want to actually alter this file, so start by making a copy of it in your home directory:

cp /opt/OpenOffice.org/share/readme/README01 .

This will copy the file README01 to the current location, which you indicate using a single period.

After this, you need to change the file's permissions, because, by default, this file is read-only for all users (r--r--r--). Use the following command to change the permissions:

chmod +w README01

Then fire up vi with the file, like this:

vi README01
Note 

Windows makes a lot of use of file extensions in order to recognize files and therefore know what program to use to run them. By default, a file with a .doc extension tells Windows that it should use Microsoft Word to open the file, for example. Linux uses a different system based on the first few bytes of each file. Because of this, file extensions are used within Linux simply to let the users know what type of file they're dealing with. Often, they're not used at all. If a file is called README, you can be fairly certain that it's a text file, for example.

Once the file is opened, you'll find yourself automatically in Command mode and will be able to move around the file using the cursor keys. Altering the text is achieved using various commands (see Table 16-1). For example, typing dd will delete the line of text that the cursor is currently within. Typing x will delete the letter under the cursor. Typing dw will delete the current word under the cursor. Try some of these to see how they work.

To actually edit a file and type text, you'll need to switch to Insert mode. Type i to do this. Insert mode is fairly easy to understand. You can move around the text using the cursor keys, and then simply start typing wherever you want. The Backspace key will delete text behind the cursor, and the Delete key will delete text in front of the cursor.

When you're finished, press the Esc key to return to Command mode. Once back in Command mode, you can page through the text. The Page Up and Page Down keys will move a screenful of text at a time. Pressing the up and down cursor keys will cause the screen to scroll when the cursor reaches the top or bottom.

After you're finished editing, you'll need to save the file. This is done in Command-Line mode. You can enter this mode by typing a colon (:). You'll see a colon appear at the bottom of the screen, and this is where you type the commands. Note that after you type a command, you'll immediately exit Command-Line mode, so if you want to issue another command, you'll need to type a colon again.

To save a file, in Command-Line mode, type :w (which stands for "write"). If you want to save the current file with a different name, you'll need to enter a filename after the w command, like this:

:w mytextfile

To quit vi, type :q. However, if you've edited a file, you won't be able to quit until the file has been saved. If you want to save the file and then quit, you can type :wq. If you don't want to save the file, type :q!. The exclamation point tells vi to override any objections it might have. You can also use it with the save command—:w!—to force the overwriting of a file that already exists.

Note 

If you don't have the correct permissions to write a file, vi might tell you that you can use :w! to override. In this case, it's wrong. The only way to write to a file for which you don't have permissions is to change its permissions.

Creating a New Text File Using vi

Creating and editing a new file with vi is easy. From any command-line shell, simply type:

vi myfile 

This will start vi and give your new file a name. However, the file won't be saved until you manually issue the save command (:w) in vi. This means that if your computer crashes before you save, the file will be lost!

Note 

The version of vi provided with SUSE Linux, vim, includes some elementary file-save protection. If, for any reason, vim is not shut down properly, there's a chance you'll be able to recover a version of file the next time vim starts. However, as with all such protection in any kind of program, you shouldn't rely on this. You should use the :w command to save your file periodically.

As always with vi, you start out in the default Command mode. To start typing immediately, enter Insert mode by typing i. You'll notice when typing that although the text is wrapped on each line, words are not carried over, and they often break across lines in an ugly way. This is because vi is primarily a text editor, not a word processor. For people who create text files, like programmers, having line breaks shown in this way can be useful.

When you're finished typing a sentence or paragraph, you can press the Enter key as usual to start a new line. You should then be able to move between lines using the up and down cursor keys. You'll notice an odd thing when you try to do this, however: unlike with a word processor, moving up a line of text that spreads across more than one line on screen will take the cursor to the start of the line, rather than into the middle of it. This again relates to vi's text editor background, where such a feature is useful when editing documents such as program configuration files.

When you're finished, press the Esc key to switch to Command mode. Then type a colon to enter Command-Line mode. Type :w to save the file according to the filename you gave it earlier. If you started vi without specifying a filename, you'll need to specify a filename with the save command, such as :w myfile.




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

Similar book on Amazon

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