Ed
Table of Contents
- About Ed
- Getting started
- Start ed, editing an empty document (which can be saved as a new file in the current directory):
- Start ed, editing an empty document, with `:` as a command prompt indicator:
- Start ed editing an existing file (this shows the byte count of the loaded file):
- Toggle the printing of error explanations. (By default, explanations are not printed and only a ’?’ appears):
- Add text to the current document:
- Change a line:
- Delete a line:
- Undo last command:
- To copy a line and paste it at some other location:
- To search forward:
- Global command:
- Substitute command:
- Run shell commands inside ed:
- Write the current document to a new file (the filename can be omitted if `ed` was called with an existing file):
- Quit ed:
About Ed
GNU ed is a line-oriented text editor. It is used to create, display, modify and otherwise manipulate text files, both interactively and via shell scripts. A restricted version of ed, red, can only edit files in the current directory and cannot execute shell commands. Ed is the ’standard’ text editor in the sense that it is the original editor for Unix, and thus widely available. For most purposes, however, it is superseded by full-screen editors such as GNU Emacs or Vi/Vim.
(In)famous for its terseness, ed gives almost no visual feedback. For example, ed does not display a prompt by default, which is really confusing since there are two modes in ed: command mode and insert mode. But without a prompt, it’s difficult to tell which mode you are in. Another example of ed’s terseness, the message that ed produces in case of error, or when it wants to make sure the user wishes to quit without saving, is “?”. It does not report the current filename or line number, or even display the results of a change to the text, unless requested. Older versions (c. 1981) did not even ask for confirmation when a quit command was issued without the user saving changes.
This terseness was appropriate in the early versions of Unix, when consoles were teletypes, modems were slow, and memory was precious. Every character printed meant time, paper, ink and mechanical wear. As computer technology improved and these constraints were loosened, editors with more visual feedback became the norm.
In current practice, ed is rarely used interactively, but does find use in some shell scripts. For interactive use, ed was subsumed by the vi and Emacs editors in the 1980s. That being said, ed can still be found on virtually every version of Unix and Linux available, and as such is useful for people who have to work with multiple versions of Unix.
NOTE:
ed is not installed by default on Arco Linux.
Getting started
Start ed, editing an empty document (which can be saved as a new file in the current directory):
ed
Start ed, editing an empty document, with `:` as a command prompt indicator:
ed -p :
Start ed editing an existing file (this shows the byte count of the loaded file):
ed -p : path/to/file
Toggle the printing of error explanations. (By default, explanations are not printed and only a ’?’ appears):
H
Add text to the current document:
Add text on line after current line:
a<Enter>text_to_insert<Enter>.
Mark completion by entering a period by itself in a new line.
NOTE:
Adds this line after the line your currently on.
Add text on line before current line:
i<Enter>text_to_insert<Enter>.
Mark completion by entering a period by itself in a new line.
The entire document (`,` is a shortcut to the range `1,$` which covers the start to the end of the document):
- ,p
The current line
- p
A specific line number or range
- 2p (line 2)
- or just type ’2’ without the p
- -1p (one line up from current line)
- +2p (two lines down from current line)
- 1,2p (lines 1-2)
To print with the line number:
- n (instead of p)
Change a line:
c<Enter>text_to_insert<Enter>. Mark completion by entering a period by itself in a new line. Ranges can be used: (.,.)c
Delete a line:
(.,.)d Just ’d’ deletes last line.
Undo last command:
u
To copy a line and paste it at some other location:
Copy line 2 to position 0:
2t0
Move line 2 to position 0:
2m0
To search forward:
/keyword<ENTER> The editor will display the first line (containing the keyword) it encounters. You can run that command again to continue searching.
Global command:
(1,$)g/re/command g/re/p (search for string or regex and print lines containing the pattern)
NOTE:
The g/re/p command was so commonly used that a separate ’grep’ program was eventually created.
Substitute command:
Substitution
(.,.)s/re/replacement/
Repeat last substitution
(.,.)s
Run shell commands inside ed:
- !command
- r !command (reads the output and stores it temporarily in a buffer)
- w (writes the file)
Write the current document to a new file (the filename can be omitted if `ed` was called with an existing file):
- w filename
Quit ed:
- q
- wq (write and quit)
Footer
Copyright © 2020-2021 Derek Taylor (DistroTube)
This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).
The source code for distro.tube can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.