
\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:customize-ed@aartaka.me}{(email)}}
\date{\today}
\title{Customizing ed(1)}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

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

I like
\href{https://www.man7.org/linux/man-pages/man1/ed.1p.html}{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!
\href{https://bsd.network/@ed1conf/113437773862270913}{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.


\section*{Picking an Implementation} \label{implementation}

BSD, GNU, Mac, Plan 9, or
\href{https://github.com/slewsys/ed}{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.

\section*{Picking an Environment} \label{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.
\href{https://github.com/ryanprior/ed-mode}{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 urxvt and Emacs (e)shell.
My non-portable customizations will be specific to these.

\section*{GNU ed flags} \label{flags}

GNU ed provides some flags to pre-configure ed:

\begin{description}\item[-v or --verbose]
To toggle more \verb|H|-elpful messages on.
\item[-G or --traditional]
For more conservative ed experience.
\item[-E or --extended-regexp]
To use
\href{the-regex#extended-posix}{POSIX EREs}.
I don't use them for the sake of portability, but they are much more consistent and featureful than default BREs!
\end{description}

Other options are less useful, but
\href{https://www.gnu.org/software/ed/manual/ed_manual.html#Invoking-ed}{do check them out anyway}.
Here's how my customization started:

\begin{figure}[h!]\begin{verbatim}
ed -vG file
\end{verbatim}\caption{GNU ed invocation}\end{figure}

\section*{The Prompt} \label{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) heart!
Want to pretend your ed is ex? Set the prompt to a mere colon!

\section*{ANSI Escapes} \label{ansi}

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

\begin{itemize}\item \verb|:| as an ex homage.
\item \verb|¶| for creative writing.
\item \verb|--| because it looks cool and CLI-y.
\item \verb|ë| because I'm artYOm.
\end{itemize}

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

\begin{figure}[h!]\begin{verbatim}
ed -vGp "[30;48;5;210mPROMPT[0m"
\end{verbatim}\caption{Indian red prompt (code might not display properly in the browser)}\end{figure}

You can go much further than that, though!
It's just that I'm relatively boring.
Here's a comprehensive listing of
\href{https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797}{ANSI Escape Codes}
for your prompt engineering 🥁

\section*{Contextual Prompt} \label{contextual}

When I moved to Arch, the default Bash prompt was quite nice.
User, hostname, and the current directory name: <code>[aartaka@paranoidal ~]$</code>.
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:

\begin{figure}[h!]\begin{verbatim}
r !pwd
# Replace HOME with tilde
g|^/home/aartaka/*$|s//~/
# Remove prefix dir
g|/|s|.*/
||s/$/:/
p
Q
\end{verbatim}\caption{Script to get a prettified directory name as a prompt}\end{figure}

With it, my prompt looks like
\verb|customize-ed-1.htm:|

\section*{Using Readline} \label{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 \verb|rlwrap| and how it enhances everything.
So here's my incantation for Readline-enabled ed:

\begin{figure}[h!]\begin{verbatim}
rlwrap -crq "/|" -H "$HOME/.ed_history" ed -vGp "[30;48;5;210m$dirname:[0m" file
\end{verbatim}\caption{Readline-enabled ed call with custom quotes, history, and file completion}\end{figure}

\section*{Copy-Paste?} \label{copy-paste}

GNU ed has (heretical) \verb|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.

\section*{Localization} \label{localization}

\href{https://www.man7.org/linux/man-pages/man1/ed.1p.html#ENVIRONMENT_VARIABLES}{POSIX specifies that ed should pay attention to \verb|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.

\section*{Multiple Files/Buffers?} \label{multiple-files}

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.
\href{#copy-paste}{Maybe even copy-pasting between the screens/tabs}.
Maybe setting the keybindings to some ed-specific actions, like \verb|g/re/s///|.
And using Readline too, to get an almost visual kind of editing experience.

The next step is obvious:

\section*{Using a front-end like vi} \label{frontend}

No, that's too much.
\href{https://github.com/aartaka/emvi}{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!

\section*{Reusing My Customizations} \label{reuse}

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

<a customize-ed-2.htm>See Part 2 of Customizing ed(1)</2> for more!


\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}
