Guide to using ‘ed’ editor in Linux
Tags: ed, linux, editor, ubuntu
Published: 2024-01-22 02:08:58

‘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.



![Invoking 'ed' editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hazhfvg3w85desco1t8s.png)

 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:


![Appending text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxpg2v532o9wwnww15tv.png)



## 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.


![Reviewing file content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isquvievo4fm4sk18xgc.png)
If you want to review specific lines in the file instead, simply type the corresponding line number (First line is line # 1).


![Reviewing specific lines](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fru9qd8kh8r5kpu5cio2.png)

## 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:


![Reviewing updated file content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eucgfc2mhnju7ghnzbhf.png)

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.


![Saving changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpi9waph1aktizd8ycf2.png)


## 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.


![Exiting the editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzlr8s4284ga38a6lela.png)


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!