Ed Command Reference - File and Buffer Management

Comprehensive Ed command reference for file and buffer management, line editing, and search/replace operations. Learn to use ed for text manipulation.

Ed Command Reference

File and Buffer Management

The ed command-line text editor provides fundamental commands for managing files and buffers.

Core File and Buffer Operations

  • :e {file}: Load a specified file into the current buffer.
  • :q: Quit the editor.
  • :Q: Quit without saving any changes.
  • :f {name}: Change the name of the current buffer.
  • :w: Write the buffer's content to the file with the current buffer name.
  • :w {file}: Write the buffer's content to a new file specified by {file}.
  • :wq: Write the buffer to the existing file and then exit the editor.

Line-Oriented Addressing and Movement

ed uses line addresses to navigate and operate within the text buffer. You can specify single lines or ranges of lines.

  • . or p: Display the current line.
  • n: Display the current line with its line number.
  • 1,$ or ,: Represents the entire buffer; displays the final line.
  • 3 or 3p: Move to line 3 and display it.
  • 3n: Move to line 3 and display it with its line number.
  • 1,$p or ,p: Display the entire buffer without line numbers.
  • 1,$n or ,n: Display the entire buffer with line numbers.
  • 15,25p: Display lines 15 through 25 of the buffer.
  • 15,25n: Display lines 15 through 25 with their line numbers.

Line Editing Commands

These commands allow for modification and insertion of text within the buffer. They can be prefixed with line addresses or ranges.

Editing and Deleting Lines

  • i: Start inserting new text before the current line.
  • a: Start inserting new text after the current line.
  • c: Clear the content of the current line and replace it with new text.
  • . (a period on a new line): Ends the active editing mode (insert, append, change).
  • d: Delete the current line.
  • 1,3d: Delete lines 1 through 3.
  • u: Undo the last edit operation.
  • r {file}: Read the contents of {file} into the buffer after the current line.
  • r !{command}: Read the output of {command} into the buffer after the current line.

Example Editing Session

e test.txt                      # Edit file "test.txt"
45                              # 'ed' returns character count as confirmation
1,3n                            # Display lines 1 through 3 with line numbers
1    This is line 1
2    This is line 2
3    This is line 3
2c                              # Change line 2
I am typing a new line 2
.                               # End active edit
1,3n                            # Display lines 1 through 3 with line numbers
1    This is line 1
2    I am typing a new line 2
3    This is line 3
wq                              # Write file "test.txt" and exit

Search and Replace

ed supports powerful search and replace functionalities using regular expressions.

Searching for Text

  • g/foo/: Search all lines for the pattern foo and display matching lines.
  • g/foo/n: Search all lines for foo and display matches with line numbers.
  • 4,9/foo/: Search lines 4 through 9 for foo and display matches.
  • 4,9/foo/n: Search lines 4 through 9 for foo and display matches with line numbers.

Replacing Text

  • s/foo/bar: Replace the first instance of foo with bar on the current line.
  • s/foo/bar/{n}: Replace the {n}th instance of foo with bar on the current line.
  • s/foo/bar/g: Replace all instances of foo with bar on the current line.
  • 1,4/foo/bar/g: Replace all instances of foo with bar in lines 1 through 4.
  • ,s/foo/bar/g: Replace all instances of foo with bar in the entire buffer.