ed Command in Linux



ed is a command used in Linux and acts as a basic text editor that operates in a command-line interface. It allows you to create and edit text files using simple commands.

Unlike other modern editors, ed command doesnt show the text on the screen, instead, it works with lines of text directly. You can add, delete and modify your lines based on specific commands. The ed command is pretty useful for scripting and batch processing because of its efficiency and simplicity.

Table of Contents

Here is a comprehensive guide to the options available with the ed command −

Syntax of ed Command

The basic syntax to use the ed command on Linux is as follows −

ed [options] [file]

Here, options are various flags that modify the behavior of ed. While file is the file you want to edit. If omitted, ed starts with an empty buffer.

ed Command Options

With ed command, different options can be used, which are provided in the table given below −

Option Description
-E, --extended-regexp Enables the use of extended regular expressions.
-G, --traditional Operates in compatibility mode.
-h, --help Shows help information and exits.
-l, --loose-exit-status Exits with a 0 status even if a command fails.
-p, --prompt=STRING Sets a custom prompt string for interactive mode.
-q, --quiet, --silent Suppresses diagnostic messages to stderr.
-r, --restricted Runs in restricted mode.
-s, --script Suppress byte counts and the ! prompt.
-v, --verbose Provides verbose output, similar to the H command.
-V, --version Displays version information and exits.

How to Use ed Command in Linux?

Lets explore the basic working of ed command on Linux system −

  • Start an Interactive Session
  • Insert Text in Editor
  • Display the Last Line
  • Save the Buffer to a File
  • Exit the Editor
  • Delete a Line
  • Replace a Line

Start an Interactive Session

By directly applying the ed command without an argument, you can start an interactive session on your Linux system with an empty document. This is useful for creating or editing text files, for example −

ed
Start Interactive Session

The above command will open the ed editor with an empty buffer.

Insert Text in Editor

After that, you can enter insert mode by pressing a and then typing your text. To stop inserting, you can type a period (.) on a new line, for example −

a
Hello, Linux Community!
.
Insert Text in Editor

This will insert the text Hello! into the buffer.

Display the Last Line

If you want to display the last line you entered, you can use the p command, for example −

p
Display the Last Line

This will print the last line in the buffer.

Save the Buffer to a File

To save the contents of the buffer to a file, you can use the w command followed by the filename, for example −

w myfile.txt
Save the Buffer to a File

This will write the buffer to myfile.txt.

Exit the Editor

If you want to exit the ed editor, you can use the q command, for example −

q
Exit the Editor

This will quit the ed editor and return you to the terminal.

Delete a Line

If you want to delete a specific line from the file, you can use the line number followed by the d command, for example, to delete a second line from the file myfile.txt, you can use −

2d
Delete a Line

Replace a Line

You can also replace a specific line by using the line number followed by the c command and then add your desired line. For example, to replace line 1, use −

1c

After that, you can type in your desired line, add (dot) to stop inserting, use w to save the file. Then use the cat command or open the file to verify the line is successfully replaced.

Replace a Line

Thats how you can use the ed command to create, open and edit your files on the Linux terminal.

Conclusion

The ed command is a powerful yet simple and lightweight text editing tool for Linux systems. Unlike modern editors, it operates directly with the lines of text and helps you in adding, deleting and modifying lines using specific commands. In this guide, we have explored the syntax of ed command, along with its several options and a few examples. These things will help you in enhancing your text editing skills directly on your Linux terminal.

Advertisements