Customizing ed(1)

By Artyom Bologov A bright thumbnail with jarring colors.     In the center, it says 'ed(1)' in bright white.     Inside the parentheses, a barely visible dim 'custom' is appended to 1.     There are attributions to 'aartaka' and 'Artyom B.' in the corners.

I like ed(1) for its simplicity and hidden power. But, as an Emacs user, I miss the endless hours spent on customization of my editor. I want to customize ed too! Maybe I can even spend more time customizing ed than actually using it! This post is my attempts at making ed more personal and accomodating.

Picking an Implementation

BSD, GNU, Mac, Plan 9, or one of the dozens of ed forks? Take your pick and enjoy some killer features these might offer! And customize your implementation, if it gives you some customization space.

(No, I'm not considering patches to ed itself. Though that might be quite fun!)

I'm using GNU ed, so my non-portable customizations will be specific to it.

Picking an Environment

You can run ed with a line printer. You can run it in a terminal. You can run The Standard Editor™ in a graphical environment. You can run it in Emacs. You can run it on your phone!

Depending on the environment you use, customizations might differ. I'm mostly using ed inside terminal emulators, like suckless st and Emacs (e)shell. My non-portable customizations will be specific to these.

GNU ed flags

GNU ed provides some flags to pre-configure ed:

-v or --verbose
To toggle more H-elpful messages on.
-G or --traditional
For more conservative ed experience.
-E or --extended-regexp
To use POSIX EREs. I don't use them for the sake of portability, but they are much more consistent and featureful than default BREs!

Other options are less useful, but do check them out anyway. Here's how my customization started:

ed -vG file
GNU ed invocation

The Prompt

Some people may say there's only one customization point in ed: its prompt. You can set the prompt text to... any text! Want a Christmas vibe? Set it to Christmas tree! Want to show some love? Use purple (or whatever color you prefer—I like black hearts) heart! Want to pretend your ed is ex? Set the prompt to a mere colon!

ANSI Escapes

Setting the prompt to random chars is fun and I tried many!

But we can use some non-portable hacks, like ANSI sequences! I like indian red, so I colored my prompt black on red:

ed -vGp "PROMPT"
Indian red prompt (code might not display properly in the browser)

You can go much further than that, though! It's just that I'm relatively boring. Here's a comprehensive listing of ANSI Escape Codes for your prompt engineering 🥁

Contextual Prompt

When I moved to Arch, the default Bash prompt was quite nice. User, hostname, and the current directory name: [aartaka@paranoidal ~]$. I want to have this directory thing in ed too! So I wrote an ed script (the most obvious thing to do, right?) to format the prompt:

r !pwd
# Replace HOME with tilde
g|^/home/aartaka/*$|s//~/
# Remove prefix dir
g|/|s/.*///
s/$/:/
p
Q
Script to get a prettified directory name as a prompt

With it, my prompt looks like Downloads:

Using Readline

That's when ed fundamentalists (like me) will reach for their pitchforks. ed should not be visual and immediate. You're not supposed to edit your commands. You need neither history nor completion. Your prompt must be static. Filtering the printouts is extraneous. Right?

Still, I like rlwrap and how it enhances everything. So here's my incantation for Readline-enabled ed:

rlwrap -crq "/|" -H "$HOME/.ed_history" ed -vGp "$dirname:" file
Readline-enabled ed call with custom quotes, history, and file completion

Copy-Paste?

GNU ed has (heretical) x/y commands for line copy-pasting. But what about copy-pasting non-line content? We can reuse terminal emulator-provided bindings, like Shift-Insert! Though you might say I'm cheating by extending the scope. Still, if it works it works.

Localization

POSIX specifies that ed should pay attention to LC_* variables. So I can make my ed output errors in e.g. Russian. I'm not going to do that, but the mere possibility adds some customization points.

Multiple Files/Buffers?

Again, cheating by extending the scope. But what if you run ed under tmux or GNU screen? Or with a mere tabbed terminal emulator? This way, you can open several files and work with their contents in parallel. Maybe even copy-pasting between the screens/tabs. Maybe setting the keybindings to some ed-specific actions, like g/re/s///. And using Readline too, to get an almost visual kind of editing experience.

The next step is obvious:

Using a front-end like vi

No, that's too much. Even though I have a Lisp-oriented ex/vi config. Too much. No.

But! I'm thinking of writing my own front-end for ed. Maybe using HTML&JS, because that's the most portable GUI there is. And ed is quite suitable for the editing back-end role!

Reusing My Customizations

Time to wrap this all into a script. So that I (and you) can reuse this configuration. And improve it. Here's the script: scripts/aed. (For "aartaka's ed", if you haven't guessed already.)

Leave feedback! (via email)