‘ed’ is a simple, straight-forward text editor available by default in Linux. It can be used to create / edit text files easily. If you ever get stuck in a situation where you don’t have access to advanced editors like ‘vi’ (e.g., when trying to recover your system after a crash), or simply for fun, your knowledge of ‘ed’ can help you.
Let’s go through a few basic commands for ‘ed’.
1. Invoking ‘ed’:
From the command line, invoke ‘ed’ by using the command ‘ed’ with (or without specifying) a file name. If a file name is specified, ‘ed’ loads the file contents in buffer, and outputs the number of characters in the file. If no filename is specified, no output is displayed. You can infer that ‘ed’ has been launched by noticing that the shell prompt (‘$’or ‘#’) is no longer available. Instead, you are welcomed by a blank line.
Here, I am invoking ‘ed’ with a blank file (pramod.txt) that I created using ‘touch’ command.
The editor responds with ‘0’ since it is a blank file. Notice that the cursor on following line is not showing any ‘$’ or ‘#’ prompt.
2. Appending text:
Now, let’s add 2 lines of text to our empty file.In order to do so, use the ‘a’ command (for append). Once you type ‘a’ followed by ENTER, ‘ed’ will wait for your inputs silently. It continues to store the inputs received in the buffer memory till the time you signal it to stop doing so by entering a ‘.’ by itself on a blank line. Here is an example:
3. Reviewing file content:
In order to review the contents of a file, use the command ‘,p’. This will list out the entire contents of the file loaded in buffer, and allow you to review the same.
If you want to review specific lines in the file instead, simply type the corresponding line number (First line is line # 1).
4. Deleting a particular line:
If you want to delete any specific line, type in the line number followed by ‘d’. E.g., in the screenshot shown below, I have deleted the first line, and am then listing the file contents using the ‘,p’ command:
Notice that on reviewing the file contents after deleting the first line, I am only shown the 2nd line (which is what remains in the buffer memory).
5. Saving changes:
After having worked on the desired updates, don’t forget to save the changes to the disk by using the ‘w’ command. If you exit the editor without saving your changes, the same will be lost. Once you issue the ‘w’command, ‘ed’ will respond with the updated character count for the file.
6. Exiting the editor:
Once you are satisfied with the updates to file content and want to return to the shell command prompt, use the ‘q’ command. Once you issue this command, you will be returned to the shell, and you can view the updated file contents using ‘cat’ command.
That’s it for now. Do remember to use ‘w’ command to save your changes before exiting the editor otherwise your changes won’t be saved, and the file on disk will remain unaltered.
Thanks!
Top comments (1)
This is handy. Nice combination of explaining each action and showing screenshot - screenshots are like path markers for the audience to follow. (I like the step by step screenshot style rather than only showing a selection.)
Something interesting for someone to write out step by step examples for, is using regular expressions with ed.