[Linux] Interactively Editing Text Files with the ed Command (Line Editor)

目次

Overview

The ed command is a standard line editor that has existed since the early days of UNIX. Unlike screen editors such as vi or nano, it does not display the entire file on the screen. Instead, you perform editing operations “line-by-line” using specific commands.

While it is rarely used for daily editing today, it is extremely useful for system recovery in minimal environments or for understanding the working principles of sed (Stream Editor). Additionally, it can be used within shell scripts to automate interactive editing tasks.


Specifications (Arguments/Options)

Syntax

ed [options] [filename]

Main Options

OptionDescription
-p [string]Specifies a prompt string for interactive mode (e.g., * or >). By default, ed displays nothing and waits for input.
-sSilent mode. Suppresses byte counts and diagnostic messages. Suitable for script processing.
-vVerbose mode. Displays more detailed error messages than usual.

Main Internal Commands

These are commands entered after starting ed. They are the ancestors of vi command mode.

| Command | Description |

| :— | :— |

| a | Starts input mode after the current line (append). |

| i | Starts input mode before the current line (insert). |

| d | Deletes the specified line (or the current line). |

| p | Displays the content of the line (print). Use ,p to display all lines. |

| r !command | Executes an external command and reads its output into the buffer. |

| w [file] | Saves the buffer content to a file (write). |

| q | Quits ed. A warning appears if changes are not saved (use Q to force quit). |

| . | Ends input mode and returns to command mode (entered on a new line). |


Basic Usage

Starting and Exiting with an Empty Buffer

This is the most basic procedure. Since ed does not show a prompt by default, it may look like the cursor has simply stopped if started without options.

ed
q

Example behavior:

  1. (The editor waits for input)
  2. q
  3. (Returns to the shell)

Starting with a Prompt

To make it easier to see that the editor is active, it is recommended to use the -p option to display a prompt symbol (such as %).

Bash

ed -p%

Example behavior:

% q

Practical Commands

Creating a New File and Importing External Command Output

The following is a sequence of entering text, inserting the result of the Linux date command, and saving the file.

  1. Start editing a new file named new_note.txt with a : prompt:
ed -p: new_note.txt
  1. Interactive input sequence:
  • i: Start input mode.
  • Enter your text.
  • .: End input mode.
  • r !date: Read the output of the date command.
  • ,p: Display all lines to verify.
  • w: Save.
  • q: Quit.

Example execution:

:i
This is a memo file.
Created by ed command.
.
:r !date
Thu Jan 15 12:00:00 JST 2026
: ,p
This is a memo file.
Created by ed command.
Thu Jan 15 12:00:00 JST 2026
:w
68
:q

Note: 68 is the number of bytes written.

  1. Verify the created file:
cat new_note.txt

Customization Points

  • Prompt Character (-p:): Since the default mode is silent, it is helpful to specify a clear symbol like *, >, or : to prevent operation errors.
  • Script Usage (-s): When performing automatic editing using Here Documents, add -s to suppress unnecessary output.

Important Notes

  • Non-intuitive Operation: You must always be aware of where the current line is. If you get lost, use p to show the current line or n to display lines with numbers.
  • Limited Undo: You can use the u command to undo the last operation, but it is not as flexible as modern high-functionality editors.
  • Ending Input Mode: To exit text input mode, you must type a single period . on a new line and press Enter. If you forget this, the editor will continue to treat your commands as plain text.

Advanced Application

Automatic Editing with Here Documents

This is a standard pattern for using ed within a shell script to append settings to the end of a file without manual interaction.

# Automatically append settings to the end of config.conf
ed -s config.conf << EOF
$a
# Added by setup script
ENABLE_FEATURE=true
.
w
q
EOF

Conclusion

The ed command proves its worth in extreme environments where screen editors are unavailable or within automation scripts.

Crucial Point: Enter a line containing only . to exit input mode.The command mode operations in vi, such as :w and :q, have their roots in ed. Knowing this will help you understand vi/vim more deeply.

Best Use Cases: Very old environments, systems without displays, or simple file editing within shell scripts.

Key Setting: Use the prompt character (-p) to make the interface easier to read.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次