
\documentclass[12pt]{article}
\usepackage[T2A,OT1]{fontenc}
\usepackage[default]{cantarell}
\usepackage[a4paper, top=20mm, bottom=20mm, left=20mm, right=20mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[normalem]{ulem}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\usepackage{setspace}
\onehalfspacing
\author{Artyom Bologov \href{mailto:sed-ed@aartaka.me}{(email)}}
\date{\today}
\title{s/sed/ed}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{./assets/sed-ed.png}

This post starts with holding a grudge:

\begin{figure}[h!]\begin{quote}
Posix regular expressions are extremely hard to get wrong? Uh... Have you really written any? Sounds like you might not really know either Posix or PCRE.
\end{quote}\caption{u/bigmell in reply to 5 (Wrong) Regex To Parse Parentheses}\end{figure}

Yes, I’ve written some regular expressions.
\href{https://github.com/aartaka/modal.ed}{I made a compiler from Modal to ed, made as an ed script}.
\href{scripts/wisp.ed}{I implemented Wisp syntax preprocessor for Scheme, the only Wisp implementation besides the reference by Arne Babenhauserheide}.
\href{https://rosettacode.org/wiki/Category:Ed}{Most of 100 RosettaCode entries for ed are completed by me, and I created ed category in the first place}.

... come again? ed?????
Why are you using such an antiquated editor?
Why are you writing scripts for it?
It wasn’t even intended for scripting, right?
There’s sed for scripting, so just use sed!

No.
I stay with ed, because it is

\begin{itemize}\item Simple (yes, including regex.
\href{https://manpage.me/?q=ed#REGULAR_EXPRESSIONS}{, BRE specification fits on two screens of text}).
\item Feature-complete.
\item More powerful than sed.
</li>

But I guess I have to list all the things that sed misses and ed has.
If only to convince myself that I need ed after all.
So...

\section*{Interactive: I Want to Test Things Out} \label{interactive}

The difference between ed and sed is akin to the interpreted/compiled language difference.
You have to run the whole sed script to see the result.
While with ed, you can fool around, test regex, and \verb|u|ndo them.
I use ed as a regex playground before integrating the regex into my projects, like
\href{https://github.com/aartaka/reb}{Reb, my regex-heavy Brainfuck toolkit}.

I think this problem is less acknowledged than it should be.
Likely because sed is implied to be the a smart one-liner tool.
One substitution and we’re done.
Easy to iterate on, but gets increasingly hard as commands / substitutions accumulate.
And no one stays with sed (and ed as an “obvious” “equivalent” of sed) after things get complicated.
Most just switch to awk or something.

A shame—ed might have worked well enough and would certainly be easier to iterate on.

\section*{Multiline: Because Most Inputs Are} \label{multiline}

Many of my scripts are working with multiline input.
A pattern that I keep using is:

\begin{itemize}\item Add a line marker to (the end of) every line.
\item Join lines.
\item Operate on these lines with “multiline” regex.
\item And split the lines back out.
\end{itemize}

\begin{figure}[h!]\begin{verbatim}
# Join lines with semicolons (there are none left)
# Also add one semicolon to the front (useful in wrapping forms below)
g/$/s//;/
,j
s/^/;/
\end{verbatim}\caption{Example of line joining commands}\end{figure}

\paragraph{But why join if you can use multiline regex feature?} \begin{quote}
Because this feature is not portable.
I know this is premature, but I want my scripts to be runnable in most ed versions out there.
Including BSD ed, Mac ed, and...
\href{https://merveilles.town/@aartaka/113432052692702413}{they work in ex too!}
This portability point especially applies to scripts on RosettaCode—too many eyes on them to fail.
\end{quote}

Reproducing this “multiline” behavior in sed means using the forbidden magic:
repeatedly reading new lines and (inevitably) using multiline regex.
Much harder to understand and only covered by the
\href{https://gnu.org/software/sed/manual/sed.html#Multiline-techniques}{ “Advanced” section of the manual}, in passing.
Compared to basic commands ed has for these: \verb|s| and \verb|j|.

\section*{Stateful and Chaotic: All Over the Place} \label{stateful}

The fact that ed works with files and buffers allows for some flexibility.
One can overwrite files, re-read them, run shell commands, move lines around, and undo anything.
\href{#interactive}{Especially so—interactively}.

Some of my scripts
(like the unreleased Binary Lambda Calculus scripts)
are working on files, and overwriting the contents when ready.
Because files are a nice way to retain state between script runs.
And sed, being a streaming editor, is missing out on state.
Yes, you can redirect the output to file or use \verb|w| command to append to files.
But that’s an afterthought, not a fundamental feature.

\section*{So, is ed perfect then?} \label{ed-perfect}

No.
Here are some quality of life features from sed that I miss:

\begin{description}\item[-f for script files)]
It’s relatively annoying to have to
\verb|cat| and pipe files into ed, but nothing critical.
\item[y command for transcription]
My ed script for Armenian transliteration is 30 lines longer than respective sed one.
\verb|y| command is rarely useful, but when it is there’s no ed command for this.
I have to substitute every char individually.
\item[looping and branching]
Now this one is a killer feature—I can run scripts until the file is ready!
I can achieve this with ed by overwriting files in a Bash \verb|for| loop.
Or by making ed script call itself recursively.
But still.
No reasonable ed-native way.
Update:
\href{https://merveilles.town/@aartaka/113652327421803906}{ed can be made self-aware and looping with \verb|w !ed|}.
\end{description}

As you can see, there are not many features I miss.
And it’s nothing compared to ed’s killer features!

\section*{Use Ed!} \label{use}

I mean, no, you don’t have to.
It’s unlikely that you (like me) are into sticking with obscure tech just because it’s fun.
And then discovering that it’s more powerful than expected/mainstream.

But still, the next time you use sed or awk, think:
This one-liner could’ve been an ed script.
A more stateful yet clear and powerful script.


\par\noindent\rule{\textwidth}{0.4pt}
\href{https://creativecommons.org/licenses/by/4.0}{CC-BY 4.0} 2022-2026 by Artyom Bologov (aartaka,)
\href{https://codeberg.org/aartaka/pages/commit/a91befa}{with one commit remixing Claude-generated code}.
Any and all opinions listed here are my own and not representative of my employers; future, past and present.
\end{document}
