A Beginner's Guide to Using ed Editor in Linux (Examples Included)
Did you know what kind of editors existed before screen-based editors like vi and vim came into existence? Well, those were known as line editors. They were used at a time when computers didn't have a video display, meaning interactive editing wasn't possible. One such editor is ed, which was developed way back in 1969.
The ed editor still ships with many Linux distributions. If, for whatever reason, you want to know how to use this tool, you've come to the correct place, as here, in this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, do note that all examples and instructions mentioned here have been tested on Ubuntu 16.04LTS.
Linux ed command
The tool's man page simply describes it as a "line-oriented text editor." Here's its syntax:
ed [options] [file]
The following Q&A-type examples should give a better idea about the tool and its usage.
Q1. How to start using ed?
To launch the editor, execute the following command:
This is how the terminal space would like when the above mentioned command is run:
So by default, the editor creates an empty buffer for you to write, similar to the way any other visual or command line based editor works when you invoke it without a file name.
Before you start entering anything, press 'a'. And after you're done writing stuff, enter a period (.) to signify this to the editor. The following screenshot will make things more clear:
So if you've ever used vi or vim, you can think of the initial 'a' and the final '.' as ways to enter and exit the insert mode. Now, to save the buffer in a file, use 'w' followed by a file name of your choice, and then 'q' to quit the editor.
To cross check whether or not the file has been created, use the cat command:
Now, in case you need to edit the same file again, you can do that by passing the name of the file as argument to the ed command, and then following the same procedure we discussed above.
Q2. How to make changes to a specific line?
Now that we know basic editing using ed, let's move ahead and discuss other editing aspects. For example, how would you make changes to a particular line?
Typing 'p' gives you the current line (at which the control is currently):
But 'n' is even better as it gives you the line number as well:
To take control to some other line, either enter the corresponding line number, or use the + or - symbols to move relatively. For example, to move the control from line number 3 to 1, you can either directly enter 1:
or you can type '-2'
Now coming to the original requirement, once you've reached the line where you want to make the change, you can enter 'c' to change that line by typing the text again. For example, I changed the first line, by writing 'Hello' instead of 'Hi.'
After that, I saved the file and quit editor using 'w' and 'q', respectively. And cross checked the change using the cat command:
Please note that the command 'a' lets you enter a line after the current line. But in case you want to enter a line before the current line, use the 'i' command. Also, to delete a line, use the 'd' command (preceede it with the corresponding line number in case the line you want to delete is not the current line).
Q3. How to make ed display error messages by default?
When you type something which ed can't understand, it displays a question mark (?) by default.
To make the response more user-friendly, you can ask the editor to print the error, something which you can do by entering the 'h' command:
And if you want to make this error-reporting a default behaviour for the editor, use the 'H' command instead:
Q4. How to make ed have its own command prompt?
If you want to make ed have its own prompt, use the P command.
So as you can see, by default, the prompt is *. However, if you want, you can have a custom prompt using the -p option when the ed command is run. For example, the following example shows how you can use % symbol as the prompt.
Q4. How to perform copy and move operations?
To copy a line and paste it at some other location, use the 't' command. You need to preceede 't' with the line number of the line you want to copy, and append the destination line number. For example, to copy line 2 to position 0, use the following command:
The move operation also works similarly, just that you need to use 'm' instead of 't'.
Q5. How to perform search operations?
Searching is very easy. To search forward, enter / followed by the search keyword. The moment you press enter, the editor will display the first line (containing the keyword) it encounters. You can run that command again to continue searching.
Agreed, ed isn't user-friendly, but that's a limitation only if you compare it with what all alternatives are available today. So, it's safe to say that this editor is not for all. However, if you are on a setup where you need to use a line-editor, this tutorial should give a good head start. For more information, head to the tool's man page, or read the GNU manual.