Using Text Editors - Linux Desktops - Linux All-in-One For Dummies, 5th Edition (2014)

Linux All-in-One For Dummies, 5th Edition (2014)

Book II. Linux Desktops

Chapter 6. Using Text Editors

In This Chapter

arrow Checking out different GUI text editors

arrow Working with the ed text editor

arrow Getting to know the vi text editor

Although the desktop provides a beautiful graphical interface that’s a pleasure to work in, much goes on outside that interface. Most Linux system configuration files are text files. Additionally, Linux gives you the ability to create shell scripts and interact with the operation of a number of programs — all by using text files.

When all is working as it should, you can edit (and even create) those files with graphical tools, but it’s highly recommended that you also know how to edit them outside that interface, should a problem exist that keeps the X Window System from loading. Whether in the interface or not, you’ll be using text editors, programs designed to interact with text files.

In this chapter, you’re introduced to a few text editors — both GUI editors and text mode editors.

Using GUI Text Editors

Each of the GUI desktops — GNOME and KDE — comes with GUI text editors (text editors that have graphical user interfaces).

To use a GUI text editor, look in the main menu and search for text editors in an appropriate category. For example, in the GNOME desktop, choose Applications⇒Text Editor and this will open gedit. After you have a text editor up and running, you can open a file by clicking the Open button on the toolbar to display the Open File dialog box. You can change directories and then select the file to edit by clicking the OK button.

The GNOME text editor loads the file in its window. You can open more than one file at a time and move among them as you edit the files. Figure 6-1 shows a typical editing session with the editor.

 width=

Figure 6-1: You can use the GNOME text editor to edit text files.

In this case, the editor has two files — a new file being written, and motd (from the /etc directory) — open for editing. The filenames appear as tabs below the toolbar of the editor’s window. You can switch between the files by clicking the tabs. The current filename appears in the last line of the window.

If you open a file for which you have only read permission, the text [Read Only] is appended to the filename shown in the window title to indicate that the file is read-only and the location of the file is displayed at the top (such as /etc). The indication that the file is read-only often appears when a regular user is opening system files that only the root can modify.

The rest of the text editing steps are intuitive. To enter new text, click to position the cursor and begin typing. You can select text, copy, cut, and paste by using the buttons on the toolbar above the text editing area.

From the KDE desktop, you can start the KDE text editor (KWrite) by choosing Applications⇒Accessories⇒Text Editor. To open a text file, choose File⇒Open to display a dialog box. From this dialog box, you can go to the directory of your choice, select the file to open, and click OK. KWrite opens the file and displays its contents in the window; you can then edit the file, as shown in Figure 6-2.

 width=

Figure 6-2: You can use the KDE KWrite text editor to edit files.

Text Editing with ed and vi

GUI text editors enable you to edit text files using the mouse and keyboard much the same way as you use any word processor. Text mode editors are a different beast — you work using only the keyboard and you have to type cryptic commands to perform editing tasks, such as cutting and pasting text or entering and deleting text. Linux comes with two text mode text editors:

· ed: A line-oriented text editor

· vi: A full-screen text editor that supports the command set of an earlier editor named ex

The ed and vi editors are cryptic compared to the graphical text editors. However, you should still get to know the basic editing commands of ed and vi because sometimes these two may be the only editors available. For example, if Linux refuses to boot from the hard drive, you may have to boot from a CD, DVD, or flash drive. In that case, you have to edit system files with the ed editor because that editor is small enough to fit on the boot medium. I walk you through the basic text editing commands of ed and vi — they’re not that hard.

Using ed

Typically, you have to use ed only when you boot a minimal version of Linux (for example, from the medium you’ve set up as a boot disk), and the system doesn’t support full-screen mode. In all other situations, you can use the vi editor, which works in full-screen text mode.

When you use ed, you work in command mode or text input mode:

· Command mode: This mode is the default. In this mode, anything that you type is interpreted as a command. The ed text editor has a simple command set in which each command consists of one or more characters.

· Text input mode: This mode is for typing text. You can enter text input mode with the commands a (append), c (change), or i (insert). After entering lines of text, you can leave text input mode by entering a period (.) on a line by itself.

To practice editing a file, copy the /etc/fstab file to your home directory by issuing the following commands:

cd
cp /etc/fstab .

Now you have a file named fstab in your home directory. Type ed -p: fstab to begin editing a file in ed. The editor responds:

878
:

This example uses the -p option to set the prompt to the colon character (:) and opens the fstab file (in the current directory, which is your home directory) for editing. The ed editor opens the file, reports the number of characters in the file (878), displays the prompt (:), and waits for a command.

tip.eps When you’re editing with ed, make sure that you always turn on a prompt character (use the -p option). Without the prompt, distinguishing whether ed is in text input mode or command mode is difficult.

After ed opens a file for editing, the current line is the last line of the file. To see the current line number (the current line is the line to which ed applies your command), use the .= command, like this:

:.=
9

This output tells you that the fstab file has nine lines. (Your system’s /etc/fstab file may have a different number of lines, in which case ed shows a different number.)

You can use the 1,$p command to see all lines in a file, as the following example shows:

:1,$p
# This file is edited by fstab-sync - see 'man fstab-sync' for details
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
/dev/devpts /dev/pts devpts gid=5,mode=620 0 0
/dev/shm /dev/shm tmpfs defaults 0 0
/dev/proc /proc proc defaults 0 0
/dev/sys /sys sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/scd0 /media/cdrecorder auto pamconsole,exec,noauto,managed 0 0
/dev/hdc /media/cdrom auto pamconsole,exec,noauto,managed 0 0
:

To go to a specific line, type the line number:

:2

The editor responds by displaying that line:

/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
:

Suppose you want to delete the line that contains cdrom. To search for a string, type a slash (/) followed by the string that you want to locate:

:/cdrom
/dev/hdc /media/cdrom auto pamconsole,exec,noauto,managed 0 0
:

The editor locates the line that contains the string and then displays it. That line becomes the current line.

To delete the current line, use the d command as follows:

:d
:

To replace a string with another, use the s command. To replace cdrom with the string cd, for example, use this command:

:s/cdrom/cd/
:

To insert a line in front of the current line, use the i command:

:i
(type the line you want to insert)
. (type a single period to indicate you're done)
:

You can enter as many lines as you want. After the last line, enter a period (.) on a line by itself. That period marks the end of text input mode, and the editor switches to command mode. In this case, you can tell that ed switches to command mode because you see the prompt (:).

When you’re happy with the changes, you can write them to the file with the w command. If you want to save the changes and exit, type wq to perform both steps at the same time:

:wq
857

The ed editor saves the changes in the file, displays the number of saved characters, and exits. If you want to quit the editor without saving any changes, use the Q command.

These examples give you an idea of how to use ed commands to perform the basic tasks of editing a text file. Table 6-1 lists some of the commonly used ed commands.

Table 6-1 Common ed Commands

Command

Does the Following

!command

Executes a shell command. (For example, !pwd displays the current directory.)

$

Goes to the last line in the buffer.

%

Applies a command that follows to all lines in the buffer. (For example, %p prints all lines.)

+

Goes to the next line.

+n

Goes to the nth next line (where n is a number you designate).

,

Applies a command that follows to all lines in the buffer. (For example, ,p prints all lines.) This command is similar to %.

-

Goes to the preceding line.

-n

Goes to the nth previous line (where n is a number you designate).

.

Refers to the current line in the buffer.

/text/

Searches forward for the specified text.

;

Refers to a range of lines — the current line through the last line in the buffer.

=

Prints the line number.

?text?

Searches backward for the specified text.

^

Goes to the preceding line. (See also the - command.)

^n

Goes to the nth previous line (where n is a number you designate). (See also the -n command.)

a

Appends the current line.

c

Changes the specified lines.

d

Deletes the specified lines.

i

Inserts text before the current line.

n

Goes to line number n (where n is a number you designate).

Press Enter

Displays the next line and makes that line current.

q

Quits the editor.

Q

Quits the editor without saving changes.

r file

Reads and inserts the contents of the file after the current line.

s/old/new/

Replaces an old string with a new one.

u

Undoes the last command.

W file

Appends the contents of the buffer to the end of the specified file.

w file

Saves the buffer in the specified file. (If no file is named, it saves in the default file — the file whose contents ed is currently editing.)

Using vi

After you dabble with ed, you’ll find vi is a dream come true, even though it’s still a command-line editor. The vi editor is a full-screen text editor, so you can view several lines at the same time. Most Unix systems, including Linux, come with vi. Therefore, if you know the basic features of vi, you can edit text files on almost any Unix-based system.

technicalstuff.eps When vi edits a file, it reads the file into a buffer — a block of memory — so you can change the text in the buffer. The vi editor also uses temporary files during editing, but the original file isn’t altered until you save the changes.

To start the editor, type vi and follow it with the name of the file you want to edit, like this:

vi /etc/fstab

The vi editor then loads the file into memory and displays the first few lines in a text screen and positions the cursor on the first line, as shown in Figure 6-3.

 width=

Figure 6-3: You can edit files with the vi full-screen text editor.

The last line shows the pathname of the file as well as the number of lines (2) and the number of characters (59) in the file. In this case, the text [readonly] appears after the filename because I’m opening the /etc/fstab file while I’m logged in as a normal user (which means I don’t have permission to modify the file). Later, the last line in the vi display functions as a command entry area. The rest of the lines display the file. If the file contains fewer lines than the screen, vi displays the empty lines with a tilde (~) in the first column.

The current line is marked by the cursor, which appears as a small black rectangle. The cursor appears on top of a character.

When using vi, you work in one of three modes:

· Visual command mode: This mode is the default. In this mode, anything that you type is interpreted as a command that applies to the line containing the cursor. The vi commands are similar to the ed commands.

· Colon command mode: You use this mode for reading or writing files, setting vi options, and quitting vi. All colon commands start with a colon (:). When you type the colon, vi positions the cursor on the last line and waits for you to type a command. The command takes effect when you press Enter.

· Text input mode: This mode is for typing text. You can enter text input mode with the command a (insert after cursor), A (append at end of line), o (open a line below the current one), O (open a line above the current one), or i (insert after cursor). After entering lines of text, you have to press Esc to leave text input mode and re-enter visual command mode.

One problem with all these modes is that you can’t easily tell the current mode that vi is in. You may begin typing only to realize that vi isn’t in text input mode, which can be frustrating.

tip.eps If you want to make sure that vi is in command mode, just press Esc a few times. (Pressing Esc more than once doesn’t hurt.)

To view online help in vi, type :help while in colon command mode. When you’re finished with help, type :q to exit the Help screen and return to the file you’re editing.

The vi editor initially positions the cursor on the first character of the first line, and one of the handiest things you can know is how to move the cursor around. To get a bit of practice, try the commands shown in Table 6-2.

Table 6-2 Cursor Movement Commands in vi

Key

Moves the Cursor

One line down

One line up

One character to the left

One character to the right

W

One word forward

B

One word backward

Ctrl+D

Half a screen down

Ctrl+U

Half a screen up

You can go to a specific line number at any time by using the handy colon command. To go to line 6, for example, type the following and then press Enter:

:6

When you type the colon, vi displays the colon on the last line of the screen. From then on, vi uses any text you type as a command. You have to press Enter to submit the command to vi. In colon command mode, vi accepts all commands that the ed editor accepts — and then some.

To search for a string, first type a slash (/). The vi editor displays the slash on the last line of the screen. Type the search string and then press Enter. The vi editor locates the string and positions the cursor at the beginning of that string. Thus, to locate the string cdrom in the file/etc/fstab, type

/cdrom

To delete the line that contains the cursor, type dd. The vi editor deletes that line of text and makes the next line the current one.

To begin entering text in front of the cursor, type i. The vi editor switches to text input mode. Now you can enter text. When you finish entering text, press Esc to return to visual command mode.

After you finish editing the file, you can save the changes in the file with the :w command. To quit the editor without saving any changes, use the :q! command. If you want to save the changes and exit, you can use the :wq command to perform both steps at the same time. The vieditor saves the changes in the file and exits. You can also save the changes and exit the editor by pressing Shift+ZZ (hold Shift down and press Z twice).

vi accepts a large number of commands in addition to the commands I just mentioned. Table 6-3 lists some commonly used vi commands, organized by task.

Table 6-3 Common vi Commands

Command

Does the Following

Insert Text

a

Inserts text after the cursor.

A

Inserts text at the end of the current line.

I

Inserts text at the beginning of the current line.

i

Inserts text before the cursor.

Delete Text

D

Deletes up to the end of the current line.

dd

Deletes the current line.

dG

Deletes from the current line to the end of the file.

dw

Deletes from the cursor to the end of the following word.

x

Deletes the character on which the cursor rests.

Change Text

C

Changes up to the end of the current line.

cc

Changes the current line.

J

Joins the current line with the next one.

rx

Replaces the character under the cursor with x (where x is any character).

Move Cursor

h or ←

Moves one character to the left.

j or ↓

Moves one line down.

k or ↑

Moves one line up.

L

Moves to the end of the screen.

l or →

Moves one character to the right.

w

Moves to the beginning of the following word.

b

Moves to the beginning of the previous word.

Scroll Text

Ctrl+D

Scrolls forward by half a screen.

Ctrl+U

Scrolls backward by half a screen.

Refresh Screen

Ctrl+L

Redraws screen.

Cut and Paste Text

yy

Yanks (copies) current line to an unnamed buffer.

P

Puts the yanked line above the current line.

p

Puts the yanked line below the current line.

Colon Commands

:!command

Executes a shell command.

:q

Quits the editor.

:q!

Quits without saving changes.

:r filename

Reads the file and inserts it after the current line.

:w filename

Writes a buffer to the file.

:wq

Saves changes and exits.

Search Text

/string

Searches forward for a string.

?string

Searches backward for a string.

Miscellaneous

u

Undoes the last command.

Esc

Ends input mode and enters visual command mode.

U

Undoes recent changes to the current line.