ed(1) conference

@ed1conf@bsd.network
1.7K Followers
2 Following
4.6K Posts
The premier conference for ed(1), the standard text editor. Accessible at 300 baud.
Pronouns: it/its
Pronounsit/they
ed(1)the standard text editor

while non-POSIX, the commonly-implemented z command displays a window of text (from the current+1 line and the following window-size, usually defaulting to $LINES) worth of text.

You can change the default window-size by specifying it after the z

z4

to show the next line and 4 more, and subsequent z commands will remember the 1+4-line window-size.

As usual, you can use the n and l suffixes to number lines and/or print them unambiguously:

5z3nl

will unambiguously print lines 5-8, including line-numbers.

You can use this to print the before/after lines of context:

g/pattern/-1z2n

will search for pattern and print/number the line before and after for additional context.

To modify a range of lines with a filter, use a temp-file:

3,5w > tmpfile
5r !filter < tmpfile
3,5d
!rm tmpfile

It's not as convenient as vi/vim where you can

3,5! filter

Someone shared patches to provide the same functionality in ed(1) but I seem to have lost the link/attribution.

Would you like to inefficiently determine whether your file has an odd or even number of lines using ed(1)?

$ ed file.txt
3141
g/^/P

If your file has an odd number of lines, you'll see the prompt (*), if it has an even number of lines, it won't show the prompt.

Or, you could use = and look at the resulting number to determine if it's odd/even if you prefer. 

Recently stumbled across this Deep Dive into ed(1) post by @jxself

(it looks like a little of the markup got mangled when getting transformed to HTML, particularly $ and % character-escaping, but otherwise a decent article)

A Deep Dive into the ed Text Editor

To underline all lines starting with "CHAPTER",

g/^CHAPTER/t.\
s/./=/g

This copies each line to the line below it, then changes each of those resulting characters to an "=", resulting in appearing underlined (such as one might use for Markdown headings if you don't use the common # heading syntax)

Daring Fireball: Markdown Syntax Documentation

To join a range of lines, use the j command:

5,8j

Note that it does not add any extra spaces, so you might first need to do something like

5,7s/$/ /

before doing 5,8j

You can also use the g command to selectively join lines

g/someFunction/.,+2j

To reverse a file, you can use a variant of yesterday's tip:

g/^/m0

This loops through all the lines and moves each one to top of the file. By the time it reaches the bottom of the original data, the file is inverted top-to-bottom.

For a subset of the file in-place, you can do things like

9,20g/^/m8

note that the 8 refers to a line outside the range. so if you have a range of lines done with marks, you can't just

'a,'bg/^/m'a-

because the line marked by 'a will move during the process, so you need to drop an extra mark:

'a-kc
'a,'bg/^/m'c

To copy all lines matching /pattern/ to the end of the document,

g/pattern/t$

or you can move them with

g/pattern/m$

If your terminal supports emoji, you can use them for festive seasonal prompts:

$ PS1="❄️"
❄️ ed -p'🌟 ' file.txt
3141
🌟 q
❄️

Other seasonal candidates: πŸŽ„πŸŒ²πŸŒŸπŸŽβ›ͺπŸ””β„οΈβ˜ƒοΈβ›„πŸ‘ΌπŸ§£πŸ§€πŸŽ…πŸ€ΆπŸ¦ŒπŸͺπŸ₯›

If you're more old-school or using a terminal that doesn't support emoji, you can still use a Christmas tree as your prompt:

$ ed -p'*<<<= ' file.txt

or don your Santa hat and beard:

$ ed -p'*<|:)} ' file.txt

which should both work fine on your ASR-33. 

and using yesterday's tip, you can target an offset match like

s/the/&\
/3

to put a line-break after the third "the" of the current line line.