How to get started with the ed text editor
Ed is one of the oldest, smallest, and most powerful text editors available.
For as well-loved as the
vi command is, it's the
ed command that's considered the standard Unix text editor. It was the very first text editor for Unix, and it's available on even the most modern Linux systems.
Unlike text editors you may be used to on Linux or another system,
ed doesn't open a window or even a screen of its own. That's because it's a functional editor that you can control either interactively or with a script. If you're already familiar with
sed, then you'll find
ed easy to learn. If you're new to both,
ed can give you a different perspective on how you can process and modify data on your system.
ed is easy; just enter the command at the prompt:
When you first launch
ed, you get no feedback or prompt. That's the expected behavior, so don't panic. Your system hasn't crashed,
ed is just waiting for your instructions.
ed to be a little more visual, use the
p command to create a prompt. Type the letter
p followed by the Return or Enter key:
$ ed p ?
The question mark (
?) is the default
Use the ed buffer
ed is active, it uses a place in memory to store data. This location is called a buffer. Such storage is significant because you're not editing a file directly. You're editing a copy of file data placed into the buffer. As long as you save the buffer when you're done,
ed preserves any changes you make to the data.
If you exit
ed without writing changes to a file on disk, it loses all changes because they only existed in the buffer. It's no different than closing any application without saving changes, but
ed doesn't warn you, so keep this in mind.
Generate text with ed
Similar to the
ed starts in command mode. This means you can issue commands to the editor, as you did to display a prompt, but you can't write or edit text without issuing a command first.
You can append text to the current buffer using the
a command followed by the Return or Enter key. Whatever text you type into the terminal now will be appended to the buffer. Stop
ed from appending text to the buffer by typing a solitary dot (
.) on its own line.
This example adds two lines (
widget=True) to the buffer:
? a [myconfig] widget=True .
After a terminating dot,
ed returns to command mode.
Save the buffer to disk
Once you're happy with your text, you can write the buffer to a file using the
w command followed by the destination file's name:
? w myconfig.txt 23
As confirmation, it outputs the number of characters written to the file.
Read an ed file
You will probably use
ed to edit existing config files more often than you use it to write new text files from scratch. To load a file into the buffer, enter
ed followed by the name of the file you want to load:
$ ed myfile.txt
ed, you can open an existing file into the buffer using the
? r /etc/myconfig.txt
[ Download the Linux commands cheat sheet. ]
View the buffer
To see all lines in the buffer, type
,p and then press Return:
? ,p [myconfig] widget=True
To see just a specific line, type the line number:
? 1 [myconfig] 2 widget=True
Edit the buffer
To edit a file, first load it in the buffer:
$ ed myconfig.txt ,p [myconfig] widget=True foo=bar openssl=libssl
To change the word "True" to "False" in the first setting of this file, select the line you want to target (2) and then invoke the search function by entering
s followed by the replacement term:
? 2 widget=True s/True/False/ 2 widget=False
To target another line, use a different line number and search terms:
? 4 openssl=libssl s/libssl/libgnutls/ s/openssl/ssl/
View the edits you've made to the buffer using the
[myconfig] widget=True foo=bar ssl=libgnutls
You haven't written the buffer back to the file yet, so the altered lines exist only in memory. To save your changes back into the file, use the
w myfile.txt 45
Clear the buffer
To start a new document or load one into a clean environment, you must clear out the buffer. The
c command clears the buffer, which you can verify using the print command (
There are two common ways to end an
ed session: you can press Ctrl+D or you can type the
q command. This doesn't give you a chance to save your buffer, so make sure you've written data you want to keep out to a file!
Get to know ed
If nothing else, learning
ed is a powerful safeguard against getting left without a text editor when your system is in a state of recovery and you're left with only the most basic toolset. This happened to me once, and I was able to fix an errant configuration file only because I had just enough recollection of using
ed in a Linux course I'd taken at a community center long ago.
It's true that
ed might be the last resort, but it's nice to know what to do with the command when it's your one and only choice. And even if you don't anticipate needing
ed (even in an emergency) it's a fun command to explore and gives you a good understanding of how tools like
sed came about. Use
info ed to view the full manual to learn more.
Learning this lightweight text editing tool can help you navigate faster, inside and outside of the text editor.
This collection of sed and grep use cases might help you better understand how these commands can be used in Linux.
Once you've committed Vi's keyboard shortcuts to muscle memory, watch how fast you work.