articles/ed.md: basic ed tutorial

This commit is contained in:
tocariimaa 2025-03-02 12:52:33 -03:00
parent 1ea87cbea7
commit d23193c962

View file

@ -8,4 +8,76 @@ machine was through a teletype. Video terminals were a luxury in that time.
?
## Tutorial
Executing ed:
```sh
$ ed
$ # Or specifying a file to open:
$ ed file.txt
```
If you pass a file to ed and if it exists, it will print its size in bytes, and if the file does not
exist ed may complain about it (at least on [GNU](gnu.md) ed).
Unless you specify a prompt with the `-p` flag, you will only get an empty line. Here, ed is waiting
for a command. An example basic session is shown below:
```
a
Hello from the standard text EDitor!
.
w file.txt
37
q
```
First we enter into file **a**ppending mode, in which ed will begin accepting input
for writing into the file. To tell ed that we finished typing text, we write a single `.`
on its own line; this returns ed into command mode, then with the `w` command we save the
text to the file `file.txt` and finally we **q**uit ed by using the `q` command.
To select a specific line, we simply type its number; ed will print the line and select it
for operations such as substitution. For example, here is how we would use the `s` command:
```
37
1
Hello from the standard text EDitor!
s/Hello/Hi/
1
Hi from the standard text EDitor!
wq
34
```
### Command summary
- `a`: append a line, after the current line
- `i`: insert a line, before the current line
- `d`: delete a line
- `p`: prints the content of the whole buffer.
- `s`: **s**ubstitute text in the current selected line.
- `w`: write buffer to the file, optionally accepts a filename if it wasn't specified when launching ed.
- `q`: quits ed
- `wq`: write and quit
- `!`: execute a shell command
- `r !`: appends the output of a shell command into the buffer (the space between `r` and `!` is relevant)
- ...
Commands like `a`, `i`, `d`, `p`, `s` and others can be preceded by a line number or range (in the form `x,y`),
with the character `,` being equivalent to the current line.
For example, here's how we can delete multiple lines:
```
a
extra line
.
a
another extra line
.
,p
Hi from the standard text EDitor!
extra line
another extra line
2,3d
,p
Hi from the standard text EDitor!
```
If you ever used a [vi](vi.md) editor, ed commands should be quite familiar. After all, vi is a visual
form of the `ex` editor (an extension to ed by Berkley).
TODO