Vim 101: ed and ex

Alex R. Young
usevim
Published in
4 min readOct 27, 2012

When I first started using Vim, I didn’t really appreciate its relationship with ed. My first experience with ed started when a Unix system (Solaris circa 1998) had been configured with it as the default editor. I ran something like crontab -e and entered a world of confusion:

edit
?
open
?
ls
?
help
?
h
invalid command suffix
?
quit
?
q

Vim users are actually poised to take advantage of ed, and it isn't as confusing as the wall of question marks would have you believe. Both vi and Vim were derived from older command-line editors, which were designed to work on teletypes rather than the screens we're more familiar with today. Eventually ex was created, and on many modern systems with Vim installed ex is actually Vim running in Ex mode. Also related, and still widely used, is sed which is a descendent of ed.

There are also some environments where the line-oriented paradigm is the only option. These include environments where output of different programs continuously clutters your screen (MultiUserDungeons, for example), terminals that have no positioning commands, and very slow lines (you don’t want ncurses to refresh your screen on a 2400 baud connection every time you add a line, even if curses uses differential display).

Ed Is The Standard Text Editor

ed is a modal editor that supports a command mode, text mode, and viewing mode -- text can be added, edited, and viewed just like any other editor. It supports regular expressions and accepts standard input. It's also part of the Single UNIX Specification, so you'll find it on almost every Unix system. These facts make it a great addition to your scripting arsenal.

For now, let's just look at basic ed usage and its relationship to Vim's Ex mode.

Basic ed Usage

Like Vim, ed is modal and works on buffers. Buffers work by using a temporary file for editing operations. When ed starts up, command mode is active. Commands consist of zero or more line addresses, then a single character that represents the command, and finally zero or more parameters:

[address[,address]]command[parameters]

Practical Example

In a terminal, type ed and then enter the following lines of text:

  • a
  • This is a text file.
  • There are many like it.
  • But this one is mine.
  • .
  • ,p
  • ,s/But/And/
  • ,p

The a command switches ed into input mode, appending text as it is typed. Typing . on a line by itself switches back to command mode.

Typing ,p prints the text in the buffer. The , is known as a line address -- it means the first through to the last lines of the buffer. Given a suitable address, p prints the related lines.

Typing ,s/But/And/ uses the same address and replaces the matching text. It's interesting to note that % is a synonym for , -- %s/But/And/ should look familiar to regular Vim 101 readers.

I've taken a screenshot of this session in my terminal so you can see what the results look like.

Vim 101: ed

One feature I like is editing the standard output of other commands. For example, typing e !figlet Hello World will place the output of figlet Hello World into the buffer.

Finally, type Q to quit without saving.

To read more about ed, the manual page (man ed) is succinct but easy to follow.

Ex

Ex mode can be entered from within Vim by typing Q in Normal mode, but on most systems you can just type ex in a shell. Once you're in Ex mode, you should be able to repeat the previous ed tutorial fairly closely:

Entering Ex mode.  Type "visual" to go to Normal mode.
:a
This is a text file.
There are many like it.
But this one is mine.
.
:%p
1 This is a text file.
2 There are many like it.
3 But this one is mine.

We use Ex commands every day -- these are the commands start with :. Like ed, Vim's Ex mode is useful in shell scripts. Other commands like sed and awk are stream oriented, but Vim allows random access to files and has more powerful multiline regular expressions. Most Unix script writers use commands they're sure are available on a wide range of systems, so you'll see ed, sed, and awk more often than Vim's Ex mode.

Conclusion

If anything, hopefully you now know how to quit ed when you get dumped in it by an unfamiliar Unix system! In a world where £25 computers are more powerful than 1970s Unix systems, it's difficult to emphasise why ed is worth learning. I actually learned it "backwards" in a sense -- my familiarity with Vim allowed me to get comfortable with ed. And you never know, it may come in handy for shell scripts or editing files on embedded systems.

--

--