By Emmett Dulaney

Linux can be a powerful operating system if you know how to use commands. For example, you can edit files in Linux using ed. Typically, you have to use ed only when you boot a minimal version of Linux (from the medium you’ve set up as a boot disk, for example) 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 or text-input mode:

  • Command mode: This mode is the default. In this mode, anything 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), and 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 as follows:

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.

When you’re editing with ed, make sure that you always turn on a prompt character (by using 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 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 that 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 (capital Q) command.

These examples give you an idea of how to use ed commands to perform basic tasks in editing a text file. The table below lists some of the commonly used ed commands for Linux.

Common ed Commands
Linux Command Does the Following
!command Executes a shell command. (!pwd displays the current directory, for example.)
$ Goes to the last line in the buffer.
% Applies a command that follows to all lines in the buffer. (%p prints all lines, for example.)
+ 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. (,p prints all lines, for example.) 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 the buffer in the default file — the file whose contents ed is currently editing.)