# Introduction to ed For as well-loved as the Vi command is, it's the `ed` command that's called the standard Unix text editor. It was the very first text editor for Unix, and it's available on even the most modern of Linux systems. Unlike text editors you may be used to on Linux or any other system, `ed` doesn't open a window or even a screen of its own. That's because it's a functional editor that you can control either interactively or with a script. If you're already [familiar with `sed`](https://www.redhat.com/sysadmin/manipulating-text-sed), then you'll find `ed` easy to learn. If you're new to both, `ed` can provide you with a new perspective on how you can process and modify data on your system. ## Launching ed When you first launch `ed`, you get no feedback or prompt. That's the expected behaviour, so don't panic. Your system hasn't crashed, `ed` is just waiting for your instructions. ``` $ ed ``` To get `ed` to be a little more visual, use the `p` command to create a prompt. Type the letter `p` followed by the **Return** or **Enter** key: ```bash $ ed p ? ``` The question mark (`?`) is the default `ed` prompt. ## The `ed` buffer While `ed` is active, it uses a place in memory to store data. This is called a _buffer_. This is significant because you're not editing a file directly. You're editing a copy of file data placed into the buffer. As long as you save the buffer when you're done, any changes you make to the data is preserved. Should you exit `ed` without writing changes to a file on disk, though, all changes are lost because they only existed in the buffer. It's no different than closing any application without saving changes first, but `ed` doesn't warn you, so keep it in mind. ## Generating text with ed Similar to the `vi` editor, `ed` starts out in *command mode*. This means you can issue commands to the editor, as you did to display a prompt, but you can't writing or edit text without issuing a command first. You can append text to the current buffer using the `a` command followed by the **Return** or **Enter** key. Whatever text you type into the terminal now gets appended to the buffer. Stop `ed` from appending text to the buffer by typing a solitary dot (`.`) on its own line. This example adds two lines (`[myconfig]` and `widget=True`) to the buffer: ```bash ? a [myconfig] widget=True . ``` After a terminating dot, `ed` returns to command mode. ## Saving the buffer to disk Once you're happy with your text, you can write the buffer to a file using the `w` command, followed by the name of a destination file: ```bash ? w myconfig.txt 23 ``` As confirmation, you see the number of characters written to the file. ## Reading a file More often than writing new text files from scratch, `ed` is used to edit existing config files. You can launch `ed` followed by the file name you want it to load into the buffer: ```bash $ ed myfile.txt ``` From within `ed` you open an existing file into the buffer using the `r` command: ```bash ? r /etc/myconfig.txt ``` ## Viewing the buffer To see all lines in the buffer, type `,p` and then press **Return**: ``` ? ,p [myconfig] widget=True ``` To see just a specific line, type the line number: ```bash ? 1 [myconfig] 2 widget=True ``` ## Editing the buffer Assume you have a file loaded in the buffer: ``` $ ed myconfig.txt ,p [myconfig] widget=True foo=bar openssl=libssl ``` To change the word "True" to "False" in the first setting, select the line you want to target (2) and then invoke the _search_ function with `s` along with the replacement term: ``` ? 2 widget=True s/True/False/ 2 widget=False ``` To target a different line, use a different line number and different search terms: ``` ? 3 openssl=libssl s/libssl/libgnutls/ s/openssl/ssl/ ``` View the edits you've made to the buffer using the `,p` command: ``` [myconfig] widget=True foo=bar ssl=libgnutls ``` You haven't written the buffer back to the file yet, so the altered lines only exist in memory. To save your changes back into the file, use the `w` command: ``` w myfile.txt 45 ``` ## Clearing the buffer To start a new document or load one into a clean environment, you must clear out the buffer. The `c` command clears the buffer, which you can verify using the print command: ``` c ,p ``` ## How to quit ed There are many stories of users who launched `vi` back in 1994 and remain stuck there to this day. There are two common ways to end an `ed` session: you can press **Ctrl+D** or type the `q` command. This doesn't give you a chance to save your buffer, so make sure you've written data you want to keep out to a file! ## Get to know ed If nothing else, learning `ed` is a powerful safeguard against getting left without a text editor when your system is in a state of recovery. This happened to me once, and I was only able to fix an errant configuration file because I had just enough recollection of using `ed` in a Linux course I'd taken at a community center long ago. It's true that `ed` might be the last resort, but it's nice to know you what to do with the command when it's your one and only choice. And even if you don't anticipate needing `ed` even in an emergency, it's a fun command to explore, and gives you a good understanding of how tools like `vim` and `sed` came about. To learn more, use `info ed` to view the full manual.