Using the `ed` Command (with examples)

Using the `ed` Command (with examples)

1. Starting an interactive editor session with an empty document

To start an interactive editor session with an empty document, simply enter the command ed in your terminal.

Motivation: This use case is useful when you want to create a new text file and edit its contents using ed.

Explanation: The ed command starts an interactive text editor session. When no filename is provided, it opens an empty document for editing.

Example:

$ ed

2. Starting an interactive editor session with a specific prompt

If you want to customize the prompt displayed in the interactive editor session, you can use the --prompt option followed by a string that represents the desired prompt.

Motivation: This use case can be helpful when you want to set a distinct prompt to differentiate between different editor sessions.

Explanation: The --prompt option allows you to specify the prompt character or string displayed in the interactive editor session. By default, the prompt is set to * .

Example:

$ ed --prompt='> '

3. Starting an interactive editor session with user-friendly errors

When debugging or working with unfamiliar files, it can be beneficial to have more descriptive error messages. The --verbose option enables user-friendly error messages in the interactive editor session.

Motivation: By using the --verbose option, you can obtain detailed error messages that help you understand and resolve any issues encountered while editing the file.

Explanation: The --verbose option enables verbose mode, which provides informative error messages instead of terse ones.

Example:

$ ed --verbose

4. Starting an interactive editor session without diagnostics, byte counts, and ‘!’ prompt

When you want to have a clutter-free editor session and avoid additional information such as diagnostics, byte counts, and the ‘!’ prompt, you can use the --quiet option.

Motivation: This use case is useful when you prefer a clean and distraction-free editing experience without unnecessary information.

Explanation: The --quiet option starts an interactive editor session without displaying diagnostics (such as warnings or errors), byte counts, or the ‘!’ prompt.

Example:

$ ed --quiet

5. Starting an interactive editor session without exit status change when command fails

By default, when a command fails in ed, it changes the exit status of the program. However, the --loose-exit-status option allows you to prevent the exit status from changing when a command fails.

Motivation: Use this use case when you want to execute a series of ed commands and continue with subsequent commands even if a command fails.

Explanation: The --loose-exit-status option ensures that the exit status of the program remains unchanged even if a command within the editor session fails.

Example:

$ ed --loose-exit-status

6. Editing a specific file

To edit a specific file using ed, provide the path to the file as an argument.

Motivation: This use case is helpful when you want to make changes to an existing text file using ed.

Explanation: When a filename is provided as an argument, ed opens the specified file for editing. This also displays the byte count of the loaded file.

Example:

$ ed path/to/file

7. Replacing a string with a specific replacement for all lines

To replace a specific string with a given replacement for all lines in ed, you can use the ,s/regular_expression/replacement/g command.

Motivation: This use case is useful for performing a global search and replace operation within the entire file, changing all occurrences of a specific string to a desired replacement.

Explanation: The ,s/regular_expression/replacement/g command searches for the regular expression pattern and replaces it with the specified replacement on all lines in the file being edited.

Example:

, s/foo/bar/g

This command will replace all occurrences of the string “foo” with “bar” on all lines within the current file.


By understanding and utilizing these different use cases of the ed command, you can effectively create, edit, and manipulate text files within an interactive editor session. Whether you need a basic empty document or more advanced features like global search and replace, ed provides a versatile and powerful editing experience.

Related Posts

Using the mailx Command (with examples)

Using the mailx Command (with examples)

Use Case 1: Sending Mail with a Subject and Recipient Address To send a plain text email with a subject and recipient address, use the following command:

Read More
How to use the command jdupes (with examples)

How to use the command jdupes (with examples)

jdupes is a powerful duplicate file finder that is an enhanced fork of fdupes.

Read More
How to use the command sgitopnm (with examples)

How to use the command sgitopnm (with examples)

The sgitopnm command is used to convert SGI (Silicon Graphics Image) files to PNM (Portable Anymap) files.

Read More