Home > Text Processing & Search > ed

ed: Line-Oriented Text Editor

ed is the standard line-oriented text editor used in Unix-like systems. It is used for editing text files in the terminal without a graphical interface and can be useful for automated text processing in scripts or pipelines.

Overview

ed is a powerful yet concise editor specialized for editing text files line by line. In addition to interactive mode, it can be used non-interactively in scripts, making it suitable for automated text processing.

Key Features

  • Line-oriented editing: Works one line at a time
  • Script-friendly: Suitable for non-interactive use
  • Extremely lightweight: Uses minimal resources
  • POSIX standard: Included by default on most Unix/Linux systems

Key Options

ed has few command-line options; most functionality is performed through editor internal commands.

Behavior Control

Generated command:

Try combining the commands.

Description:

`ed` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

ed can be used interactively or via input redirection in scripts.

Create a new file and add text

ed newfile.txt
a
Hello, ed!
This is a test line.
.
w
q

Open a new file, add text, then save and quit.

Open an existing file and view its content

ed existing_file.txt
1,$p
q

Open an existing file, print all lines, then quit.

Substitute specific text (non-interactive)

echo '1,$s/old_text/new_text/g
w
q' | ed -s my_document.txt

Replace all occurrences of 'old_text' with 'new_text' in the file and save. Suppress output with the -s option.

Append content to the end of a file (non-interactive)

echo '$a
New line added at the end.
.
w
q' | ed -s my_document.txt

Add a new line to the end of the file and save.

Tips & Notes

ed's usage is very different from other modern editors, so understanding a few key concepts is important.

Key Concepts for Using ed

  • **Command Mode and Input Mode**: ed is in command mode by default. You enter input mode with commands like `a` (append), `i` (insert), and `c` (change). In input mode, typing `.` (dot) on a line by itself returns you to command mode.
  • **Addressing**: Commands can be applied to specific lines (e.g., `1` for the first line, `$` for the last line, `.` for the current line) or line ranges (e.g., `1,$` for the entire file).
  • **Saving and Quitting**: Use the `w` command to save changes and the `q` command to exit the editor. Combinations like `wq` are not supported. `Q` quits without saving changes.
  • **Checking Current Line**: Typing `.` prints the content of the current line, and the `=` command prints the current line number.
  • **Using Regular Expressions**: Powerful regular expressions can be used with the `s` (substitute) command to find and replace text.

Same category commands