 Anytime I do a video about text editing on Linux or Unix-like operating systems in general, yeah, I show you guys the VIM text editor or Emacs or GNU Nano or any of these cool text editors that are available on Linux, somebody always makes the remark that Ed is the standard editor. Ed is a very old text editor that dates back to the late 1960s, and these people, although they're making a joke, they are actually partly right because if you actually read the man page for the Ed editor, it actually says Ed is the standard editor. It's the standard Unix text editor. It should be available on all Unix-like operating systems, and it's a really good thing to know for those of you that do work in system administration and IT, you deal with a lot of Unix systems, then it's a good idea to at least have a basic working knowledge of the Ed editor, and that's what we're going to talk about today. So let me switch over to my desktop, and I'm going to go ahead and open up a terminal, and I'm going to zoom way in here so you guys can see everything, and I'm going to go ahead and open a file in Ed. Actually, let's just open a new file. So I'm going to do edtest.txt. So this file doesn't exist, so I get this message test.txt, no such file or directory, but then I don't get my shell prompt back. Why not? Because we're still in Ed. And by default, Ed doesn't actually have a prompt, or it's kind of hard to tell what's going on with Ed. One of the real complaints a lot of people have with Ed is it's very terse. It is actually very infamous in its terse-ness, meaning it doesn't print much information on the screen at all. By default, you don't get a prompt. By default, you don't get error messages. If you type something that Ed can't understand, for example, if I did B here and hit enter, I get a question mark. That is what Ed defaults to any time there's an error. It doesn't give you an error message, it's because Ed is such an old program dating back to the late 1960s. People use computers very differently back then. For one thing, you didn't typically have display monitors that would display the entire document you're working on. Typically, you were interacting with teletype printers. You would enter some information in Ed. You would change a few lines of text, and then you would get that information printed out to you. Because of computing being so different back then, so slow, so costly sometimes, characters on the screen, especially characters that were printed out, cost money. That's really the reason that Ed is so terse is because the terse-ness actually was very appropriate in the early days of Unix, because consoles were teletypes, modems were really, really, really slow, memory was very precious. Every single character that was printed, that meant time, paper, ink, wear on the equipment. As computer technology, of course, evolved, once we get into the late 1970s, early 1980s, then computing was a little bit different. You had display monitors. People didn't interact with teletype printers anymore, and then you saw the rise of more modern text editors that we're familiar with today, like VI, Vim, Emacs. What I want to do is I actually want to get out of Ed and restart it the right way, because I want to give it a prompt, because visually we need a prompt. So I could just do Q to quit, very similar to the Vim commands for quitting, right? You could do colon Q to quit out of Vim. Here in Ed, you just do Q and hit Enter, or I could do W for right. That might be interesting, even though it's an empty document. I do want to save it, and it tells me this document, it wrote it, but it returns a zero, meaning zero bytes. That's how big this document is, meaning it's an empty document, and then I do Q to quit. And now let me rerun Ed, so this time I'm going to give it this flag, Ed-P, and let's give it a prompt. So we can specify any prompt we want. Typically, what you want to use is something like a asterisk or a colon. You don't want to use a dollar sign, because a dollar sign, of course, symbolizes the actual bash shell, you know, the standard default shell on Linux, and you don't want to confuse yourself. You want to make sure you know when you're in a proper shell and when you're inside Ed. So I'm going to set it to a colon here, and once again, let's open test.txt. When I hit Enter, you see it immediately tells me how big the file is. It's currently zero bytes, and then we get the colon, which is our prompt. Now one thing I think most people want are proper error messages. You don't want to get an error and only receive that question mark. That question mark maybe was appropriate back in the 1960s when you didn't, it was costly to have a whole bunch of information outputted, but I want a proper error message when I get one. So I want to do capital H and hit Enter. And now when I do something that Ed doesn't understand, it will give me a real error message rather than just a single question mark. So if I did B again, which is not an Ed command, and hit Enter, now I get the question mark, but then I also get a proper error message this time, unknown command. So let's go ahead and start typing some stuff into this empty document. One thing those of you that are familiar with VI and VIM, you will notice that the Ed commands are very similar to VIM commands, because VI really kind of evolved from Ed. It was kind of the next step of evolution of text editing, where Ed started out in the 60s, and then once VI came around in the 70s, they really kept a lot of the same keybindings and syntax. So to insert a line of text here, what we want to do is type A and hit Enter. And now we don't get a prompt on the next line, because that's letting us know we're in insert mode. So anytime I have the prompt, we are in command mode, meaning whatever letters I happen to type, they're essentially keybindings to run a command. And then the lines that don't have a prompt, that is insert mode, meaning I can start typing lines of text. So it's very similar to VI and VIM, where you have normal mode, command mode, insert mode, and Ed, you really have two modes. You have command mode, where you enter commands, and then insert mode, where you're typing text. So A was, hey, add a line of text. So let's go ahead. This is line one. And then I'm going to hit Enter. And then Ed, anytime you're done with a command, type period, and hit Enter. And that just created one line of text. This is line one. If you wanted to read it, what you could do is you could do comma P, and that prints out the entire document. There's only one line of the document. Now comma P, and that's actually shorthand for one comma dollar sign P, which is a range, meaning line one to the very last line of the document, P to print. That's the same thing, but you can just shorten it to just comma P, and it works the same way. Now let's go ahead and add another line of text. So A, and this time I'm going to do, this is line two, woo, hoo. Hit Enter, then hit period, and then hit Enter again. And now when I do comma P, we should get two lines of text. You see, this is line one, and then this is line two, woo, hoo. Now one thing about the terseness of ed once again, it doesn't give us line numbers. Sometimes you actually want line numbers, especially if you're spinning out a whole bunch of text. Here we're only dealing with two lines, it's obvious what line we're on. But if you wanted actual line numbers instead of P for print, you do N for print with line numbers. So if I did comma N, I get lines one and two, and I get the actual line numbers preceding those lines as well. And one thing about ed being a line editor, you've got to make sure that you know what line you're on any time you're running commands, because it's going to do those commands on the line you're on. So anytime you want to figure out what line you're on, you can always hit P, and it will print out that line, or just hit N, and it will print out that line plus the line number. If you want to print out a line that you're not currently on, just give it a range. So if I did one P, for example, that's going to print out line one for me. If I did one comma two P, it's going to print out a range, lines one through two. So let's go ahead and do a P, because I want to make sure or actually do a N. I'm on line two. I'm going to go ahead and add one more line, so I'm going to hit A again. And this time I'm going to do this is line four, and hit enter, and then hit period to finish that command. And now when I do comma P, you see I've got this is line one, this is line two, and this is line four. I made a mistake, right? Line three actually reads this is line four. Well, let me go ahead and P, to make sure I know what line we're on, so it prints out this is line four. Well I want to go ahead and insert a line that says this is line three. Well if I hit A, that line will appear after this is line four, because A always inserts the next line of the document, which makes sense typically when you're adding lines to a document, you're always adding the next line. But if you wanted to add a line before the one you're on, instead of A, type I. And now I'm going to do this is line three. Hit enter, and then period to finish that command and hit enter. And now when I do comma N, because we'll get the line numbers this time when we print it, you see that fixed the problem. I inserted line three in the proper place on line three. And because you're going to be printing lines of text all the time, we should also discuss some other things you could do. So P of course always prints the line you're on and also prints the line you're on with the line number. But maybe you want to go back or forwards printing a line. So if I actually did minus one P, it prints the line before the line I'm currently on. If I did plus one P, it would print the line after the one I'm currently on. So you can actually do plus or minus plus numbers for the P and N commands. Now say I want to change a line. I did one of these lines and it didn't turn out right. I want to go back and change that line of text. Well, first of all, let's make sure we know what line we're on. So I'll do it in. Currently I'm on line three. So the line I want to change is actually line four, the very last line. So I'm going to do four and then I'm going to give it this command C for change. Now that's very similar to VIM and VI that uses C for change. So you do CW for change word, you know, in VIM. Same thing with ID. C means change. In this case, it means change line because again, it is a line editor. So I'm going to do four C. And now I get the insert mode here and this is where I type a new line four. The old line four goes away and I change it. And this time I could say I'm changing line four. Hit enter, then hit period and then hit enter again to signify I'm done with that change. And now when I do colon in, you see the changed line four appears. And just like C signifies change and add and in VIM, D signifies delete both in add and in VIM. So if I wanted to delete a line, I just give it a line number or a range of lines and then give it D. So if I wanted to delete line one, I would do 1D, hit enter. And now when I do comma in to print out everything you see. Now line one is actually this is line two woohoo because we deleted the line that read this is line one. Now very similar to VIM, you can undo commands, but in ed, you can only really undo the last command. You only get one shot at it, right? So I just deleted line one and as long as I don't run any other command in between, I can always type U and hit enter and that should undo the last command, which means if I do comma in to print out everything. Now I've got all four lines back including the line one that I had deleted earlier. Now let's talk about copying lines and pasting them somewhere else. Well, we're also gonna talk about just moving lines somewhere else. So to copy a line and paste it somewhere else, you use the T command. To move a line somewhere else, you use the M command. And these commands need to be wrapped in numbers. So what you would do, for example, you could run something like two T zero. Now what this does is it says line two, copy it to position zero. Let me run that. And now when I do comma in, you see what was line two is actually now three, because we copied it and we pasted it in the zero position. Meaning the copied line became the new line one. You see now we have five lines instead of four. Now if you want to move lines, what you could do is something like I could do one M two, meaning take line one and move it to the second position. If I do that and then do comma in, you see how I just move the lines around that shifted the order. So a number T and then another number copies a line and pasted it somewhere. A number M and another number moves a line somewhere. Now how do you search for a string of text in a document? Well, everyone knows in Vim, it's very easy. You just hit the slash character and then type the string that you're searching for. It's the same way in it actually. So if I wanted to, I could type the forward slash and then I could type a string. For example, I see that the word this appears several times in the document. So I hit enter on that. It gives me line one because it gives me the first instance of that. If I want to do a second search on that, what I would have to do is run the command again and this time it gives me line two. And of course you can do regular expression. So I could do the forward slash and then the carrot symbol for the beginning of the line capital T and it will give me the first instance of that. And if I wanted to do that again, I would have to once again do the search again. It's a little tedious as not as straightforward as searching through Vim where you do the forward slash and the string and then just hit enter and then in, in, in to go to the next instance of that string you were searching for. You can't really do that in ed because of course ed is not displaying the whole document for you anyway. Again, it is kind of a strictly a line editor rather than a full blown text editor. And speaking of searching for things and using regular expression. So let's talk about the global command in ed. So what the global command is typically you give it a range of numbers. So it could be, you know, first line to last line and then G for the global command slash and then give it a regular expression such as I'm just going to do a string of text RE and then slash and then the command you actually want to run on the lines that contain that string of text or that regular expression. Maybe I just want to run the P command for print. So if I actually ran that what this is going to do is it's going to search for every line that has RE that particular string and then print it. Now I don't have a single line that contains that but I could add one really quickly. So I'm going to do a to add a new line and I'm going to say this is a really cool document. And then I'm going to enter and then period to end that command. And now I'm going to do comma in to show you now I've got a new line on line five because we were actually on line four when I ran that command. This is a really cool document. And now let me run that global command. So once again I can do a range meaning do this on every single line of the document lines one through the last line and then G slash RE is the string we're going to search for slash P and then hit enter and it's going to print the line that contained RE because RE is part of the word really. Now why did I make a new line just so I could do this because I wanted to actually show you where grip comes from historically. Because this command was so commonly used in ed eventually people created the actual grip command and there's actually now a command line utility called grip that does something similar. Other than the global command you also have a substitution command here in ed which would be S slash and then a regular expression or a string of text slash and then what you want to replace that string with. So what I'm going to do is I'm going to substitute the word really and I will change that to very and then of course give it an ending slash and then if I run that and then do comma in. You see line five is now this is a very cool document where before it read this is a really cool document and just like the global command the G command you can add ranges to it line numbers line ranges you can do that with the S command the substitution command as well. One cool thing that you can do inside ed is you can run shell commands inside ed so if I wanted to run something I don't know we'll just do a simple date command all I need to do is the exclamation point followed by the name of the shell command I want to run. And you see I get the output of that right here in ed because you know sometimes you want to run a shell command but you're already inside ed you don't want to exit ed run the command and then come back to ed you can just do it right here inside ed. One cool thing you can actually do is you can have this output into the document itself so instead of just doing exclamation command I could do r for read and then exclamation and I'll do the date command one more time and now it tells me there's 32 bytes and let me do a comma in to print and the reason it was telling me the number of the bytes in the output of the date command in the line it added because of course it it's going to tell you the size because that's one of the things with ed anytime you make changes it typically spits out byte information you know the number of bytes in that line that you just added or the number of bytes in the document if you write to the document. By the way we should talk about writing to the document how do you write in vim you do a colon w how do you write in ed well we don't do a colon because our prompt was the colon I mean you can make the prompt anything you just do a w for write and then the name of the file you're writing to in my case we had already created a file test.txt if I don't give it a file path it's just going to write to test.txt so I'm just going to do w and hit enter and it tells me the document is 178 bytes to quit out of ed q for quit just like in vim colon q it's up in ed it's just q and if you wanted to do both commands at the same time write and quit just wq for write and quit and hit enter so that was a very quick look at the ed editor ed it's not very hard it really is and I think a lot of people think it's a really hard thing to use but honestly in that 10 or 15 minutes of me playing around there I showed you pretty much all of the basic commands now is ed something that you're going to use as your primary text editor all the time no because ed is not really a text editor in the modern sense of it or again ed is really a line editor it's really it really dates back to a previous generation of computers a way of doing things that's no longer appropriate because now it is kind of tedious and slow like you you would never write a novel inside ed right we have more appropriate things now so typically if you're going to do anything that requires a lot of time in a text editor making massive edits and really complicated things you're just going to open up vi or vim on most unix like operating systems these days but it's a good idea to have some basic working knowledge of ed because again ed is the standard editor and you never know maybe one day you'll actually ssh into some unix machine and it might be the only text editor on the system that can and does happen sometimes and now if you ever are faced with a situation like that hopefully this video will help you out now before i go i need to thank a few special people i need to thank the producers of this episode i need to thank absy gaib james michael paul wesekami alan chuck kurt david dylan gregory hyko erion alexander peace arching fedora polytech raver red prophet scott steven and willie these guys they're my highest tiered patrons over on patreon without these guys this episode about the ed editor would not have been possible the show is also brought to you by each and every one of these ladies and gentlemen as well all these names you're seeing on the screen right now each and every one of these fine ladies and gentlemen help support my work over on patreon because i don't have any corporate sponsors it's just me and you guys the community if you like my work and want to support me please consider subscribing to distro tube over on patreon all right guys peace i finally found an editor more painful to use than nano