How to Use ed (Standard Line Editor) on Unix/Linux

Linux TLDR
Last Updated:
Reading time: 6 minutes

Ed is one of the oldest line editor, having existed for almost four decades. It was introduced long before Vi or Vim, and by looking at its functionality, you can clearly say that Vi drew inspiration from ed, as they share many similarities.

Before we continue, let me clarify that this line editor is rarely used today. So, why bother learning about it? Well, Vi is frequently found in minimalist Linux distributions, but sometimes in distros like Alpine Linux or Busybox, where Vi is not present, ed is the only option.

As previously mentioned, ed is the oldest editor and is shipped with every Unix/Linux system by default. You can execute the “ed” command to check its presence (to exit, type and enter “Q“). It features a very minimal interface, which might confuse beginners while working, but once you become comfortable, you can easily use it to create, edit, display, and manipulate files.

In this article, I’ll focus mostly on its usage rather than explaining ed history. So, let’s begin…

Tutorial Details

Descriptioned (a Line Editor)
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredNo

Usage of ed

If you’re a Nano user, there’s no need for questioning, but Vi or Vim users can quickly grasp ed usage as it also features modes similar to Command and Insert, which are identical to them.

One thing I must recommend is to use a prefix for the command mode so you can easily identify the current mode. The reason is that when you enter in ed, you will directly start seeing a blank screen without any menus, options, messages, or anything.

So, for beginners, it often becomes difficult to work with their text file. Let me demonstrate what I mean: Open your terminal and execute the “ed” command, and you’ll see the following output:

ed without command prefix

In the above picture, I’ve entered multiple commands and written some text on an undefined file. Now, there’s no way to distinguish between them, but adding a prefix to the command mode will make it easier to work with ed. For that purpose, use this command with the “-p” flag to add a prefix to the “ed” command mode.

📝
Feel free to substitute the prefix “: ” with something else.
$ ed -p ": "

Output:

ed with command prefix

This time you can find some sense in the output, so let me tell you what we did here. First, everything is a command next to the “: ” prefix (similarly to Vi commands); the “l” and “m” commands are incorrect, resulting in the “?” in the output indicating an error (type and enter “H” to enable a full error message).

The “a” command is used to enter in insert mode, after which we wrote “Linux TLDR” text to our undefined file. To exit insert mode and return to command mode, you must first move to a new line, type and enter “.” (dot).

I hope you understood how to handle your text file with ed, especially if you’re somewhat familiar with Vi or its derivatives. One crucial point about ed is its line-by-line editing nature, which requires a sequential approach rather than simultaneous manipulation of all content. If you didn’t get it, then don’t worry, as after looking at different examples, you will find what I mean.

Alright, I’ll now show you how to use ed for creating, editing, displaying, and manipulating files with different examples, so let’s get started…

1. You can use ed to edit an existing file, create a new one, or begin working without specifying a filename; all changes will be saved in the buffer, requiring later saving into a text file using the command mode. Right now, I’ll show you how to work without specifying a filename, since once you grasp it, the rest will be straightforward. To start, simply type and enter “ed” to open the editor without specifying any file.

ed without specifying file

2. When you enter “ed“, by default, you’ll be in command mode (accepting commands). To write content in a text file, type and enter “a” to enter in insert (or write) mode. Write your text content, and once finished working, type and enter “.” on a new line to return to command mode.

writing content in text file using ed

3. Every time you need to write new content in a text file, you must first enter in insert mode, write your content, and then return to command mode (similar to Vi). For instance, after writing the lines in the example above, I once again entered in insert mode, wrote a new line, and then returned to command mode.

adding new lines in ed

4. Once you’ve finished working with your text file, save it by specifying a filename. For example, to preserve all the above changes in a new file, you can use “f <filename>” in command mode, and then use the “w” command to write the content from the buffer to the specified file.

creating a new file and saving the content

The final line, indicating 81, represents the number of bytes written to the text file.

5. After saving the changes to a new file, you can exit ed by typing “Q” in command mode, then later view your saved file in the current directory with “ls” and read its contents with “cat“.

reading the saved file

6. Once again, when you open the file with the “ed” command, you will first get an output of the total content in bytes. To view the full content of the file, type and enter “,p” in command mode.

📝
If you simply type and enter “p“, you will receive the last (or most recent) line in the file.
displaying content of file in ed

7. If you want to print a specific line in the file, you can directly type and enter “<line-number>” or use “<line-number>p” in command mode.

📝
If the specified line does not exist, you will get a “?” in the output.
print specific line from the file

8. If you want to print all the content in your file along with the line number, you can use “,n” in command mode.

📝
If you simply type and enter “n“, you will receive the last (or most recent) line in the file with the line number.
print content with line number

9. To output a specific line with a line number, you can use “<line-number>n” in command mode.

print specific line with line number

10. If you wish to insert new content within a line, such as adding content at the top (position zero), use “<line-number>a“, or to add content to the second last line, use “i” in command mode.

add new line in between

11. If you wish to update an existing line, you can use “<line-number>c” in command mode.

updating the content of file

12. If you wish to delete content from a file, you can specify the range of lines like this: “<from>,<to>d” or directly specify a specific line to remove like this: “<line-number>d” in command mode.

deleting line using ed command

13. If you accidentally updated or removed content, you can type and enter “u” in command-mode to undo, and repeat it to redo changes. However, remember that only the last change can be undone, so be cautious when editing files using ed.

undo redo in ed command

14. To copy a range of lines or a specific line and paste it to a specific position, you can use “<from>,<to>t<position>” or “<line-number>t<position>” in command mode.

copying lines in ed

15. Instead of duplicating, you can relocate a line from one position to another using a method very similar to the previous one. Simply replace “t” with “m” in “<from>,<to>m<position>” or “<line-number>m<position>” in command mode.

moving lines in ed

16. If your file is too large and you’re looking for a specific text within it, use “/<query>” in command mode. However, remember that to find each occurrence, you must repeatedly execute the same command.

search text in file using ed command

17. To replace the first occurence of a range of text, specific text, or most recent text, you can use “<from>,<to>s/search/replace“, “<line-number>s/search/replace“, “s/search/replace” in command mode.

replace the text using ed command

18. While working with a text file in ed, you may wish to execute a shell command. At that point, rather than quitting the editor, you can use “!<command>” to execute a shell command within ed.

execute command in ed

19. To save the output to an existing file after executing the shell command using the previous method, you can use “r !<command>“.

saving command output in file

20. Here, we’ve covered nearly all examples, but to simplify your practice, each command used is listed with its definition below.

ed CommandDefination
HEnable the full error message reporting.
aInsert mode.
<line-number>aAdd new content to a specific line number.
iUpdate the second-last line.
.Quit insert mode.
f <filename>Save the file to a specific filename.
QQuit the ed.
,pPrint all the content of file.
<line-number> or <line-number>pPrint specific line from the file.
<from>,<to>pPrint the range of lines from the file.
,nPrint all the content of the file with a line number.
<line-number>nPrint specific line from the file with the line number.
<from>,<to>nPrint the range of lines from the file with the line number.
<line-number>cUpdate the content of the specified line.
<from>,<to>cUpdate the content between the range of lines.
<line-number>dDelete the content of the specified line.
<from>,<to>dDelete the content between the range of lines.
<line-number>t<position>Copy the specific line content to the specified position.
<from>,<to>t<position>Copy the range of line content to the specified position.
<line-number>m<position>Move the specific line content to the specified position.
<from>,<to>m<position>Move the range of line content to a specified position.
/<query>Search for the specified query.
s/search/replaceSearch and replace each instance of text.
<line-number>s/search/replaceSearch and replace each instance of text on the specified line.
<from>,<to>s/search/replaceSearch and replace each instance of text within the range of line

Finally, here comes the end of this article. Well, the “ed” isn’t used much today, and most of you haven’t even heard about it until now, but it’s a fun editor that you can try using sometimes.

Now, if you have any questions or queries related to the article, do let us know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.