Ed

From LQWiki
Jump to: navigation, search

ed is one of the most noted original editors on Unix and Unix-like systems written by Ken Thompson. Ed is a line editor unlike the newer screen oriented editors vi or emacs.

It is noted for its extreme terseness, and has been the subject to Usenet humor, such as the post below.

It is the default command-line based editor in the Plan 9 operating system, apart from acme and sam.

To use ed

$ed               
a                            (To add or append text)
Type something here.
.                            (Signals end of input. Will not show up in file)
w foo                        (Write to file named foo)
q                            (To quit ed)
$ cat foo
Type something here.
$

It is important to remember that ed is a line editing tool. This means that it deals with the text of a file one line at a time or as groups of lines. When this is understood the basic usage of ed becomes fairly simple. The file is loaded into a buffer which is independent from the copy on the disk until you write it out. ed is also a modal editor. This means it has separate command and input modes much like vi.

Basic Commands

a -- append a new line after the current line.
i -- insert a new line above the current line.
d -- delete the current line.
p -- print the current line.
n -- print the current line preceded by it's line number.
u -- undo the last command (a second u undoes the undo).
w -- write the buffer to disk.
q -- quit.
h -- expand on last error ?.

Note on the '?' for an error. If you want to know what the error dealt with you can enter the command 'h' and it will give you a "slightly" better error message.

Usage

If you refer to the first example given you see a clear guide of usage. When you first enter ed you are in command mode. If you have loaded a pre-existing file, ed will print the number of bytes loaded and wait for your command. It automatically starts you with your current line equal to the last line of the file so you can easily start appending text.

Almost all of ed's commands can take a line number or a range of lines as an option. For example, to print the first 10 lines of a file you would enter: 1,10p

To delete the 5th line from a file all you need to do is enter: 5d

Movement is also very easy. You just enter the line number you wish to go to. To get the the start of a file after you load it, simply enter: 1

There are some other little shortcuts to know about when dealing with lines. A '$' (dollar sign) refers to the last line of the file. A '.' (period) refers to the current line. This is very useful because you can use these before any command to give a range for it to work on. To delete every line from the current line to the end of the file, you would enter: .,$d

Sometimes you will enter a command and not see your expected result. There is no error (?), just complete silence. In almost every instance, that this happens, you are in input mode and not command mode. To exit input mode you need to enter a '.' on a line by itself. What follows is an example editing session. The comments (everything after the ;) are not supposed to be typed in.

$ed SomeFilename

5432   ; The number of bytes loaded.
.      ; This prints the current line.
What a funny line to end a file with.
.=     ; Print the current line number.
132
130,$p ; Print the last three lines.
This is weird.
A fake little file.
What a funny line to end a file with.
d      ; delete the current line
a      ; add new text.
This is a much better final line.
p
       ; ??? Why didn't this show our new line?
       ; Because we are still in input mode.
.      ; exit input mode
p      ; print the current line
p      ; it is our 'p' we accidentally added
d      ; delete the current line
p      ; print our new current line
This is a much better final line.
w      ; that is good so save the file
5428   ; the number of bytes saved
q      ; quit

Knowing these simple commands and understanding how they effect the file can often save you if you are unable to use a different editor. ed is actually a very nice little editor with some built-in power which might surprise you. The man page is highly recommended reading and it never hurt anyone to spend 15-20 minutes experimenting with this editor. ed man page


Usenet humor about ed

----- Begin included message -----
 From: patl@athena.mit.edu (Patrick J. LoPresti) 
 Message-ID: <1991Jul11.031731.9260@athena.mit.edu> 
 Sender: news@athena.mit.edu (News system) 
 Subject: The True Path (long) 
 Date: 11 Jul 91 03:17:31 GMT 
 Newsgroups: alt.religion.emacs,alt.slack 
 Organization: Massachusetts Institute of Technology 
 Lines: 95 

 When I log into my Xenix system with my 110 baud teletype, both vi 
 *and* Emacs are just too damn slow. They print useless messages like, 
 'C-h for help' and '"foo" File is read only'. So I use the editor 
 that doesn't waste my VALUABLE time. 

 Ed, man! !man ed 

 ED(1) UNIX Programmer's Manual ED(1) 

 NAME 
 ed - text editor 

 SYNOPSIS 
 ed [ - ] [ -x ] [ name ] 
 DESCRIPTION 
 Ed is the standard text editor. 
 ----- 

 Computer Scientists love ed, not just because it comes first 
 alphabetically, but because it's the standard. Everyone else loves ed 
 because it's ED! 

 "Ed is the standard text editor." 

 And ed doesn't waste space on my Timex Sinclair. Just look: 

 -rwxr-xr-x 1 root 24 Oct 29 1929 /bin/ed 
 -rwxr-xr-t 4 root 1310720 Jan 1 1970 /usr/ucb/vi 
 -rwxr-xr-x 1 root 5.89824e37 Oct 22 1990 /usr/bin/emacs 

 Of course, on the system *I* administrate, vi is symlinked to ed. 
 Emacs has been replaced by a shell script which 1) Generates a syslog 
 message at level LOG_EMERG; 2) reduces the user's disk quota by 100K; 
 and 3) RUNS ED!!!!!! 

 "Ed is the standard text editor." 

 Let's look at a typical novice's session with the mighty ed: 

 golem> ed 

 ? 
 help 
 ? 
 ? 
 ? 
 quit 
 ? 
 exit 
 ? 
 bye 
 ? 
 hello? 
 ? 
 eat flaming death 
 ? 
 ^C 
 ? 
 ^C 
 ? 
 ^D 
 ? 

 --- 
 Note the consistent user interface and error reportage. Ed is 
 generous enough to flag errors, yet prudent enough not to overwhelm 
 the novice with verbosity. 

 "Ed is the standard text editor." 

 Ed, the greatest WYGIWYG editor of all. 

 ED IS THE TRUE PATH TO NIRVANA! ED HAS BEEN THE CHOICE OF
 EDUCATED AND IGNORANT ALIKE FOR CENTURIES! ED WILL NOT CORRUPT YOUR
 PRECIOUS BODILY FLUIDS!! ED IS THE STANDARD TEXT EDITOR! ED MAKES THE SUN 
 SHINE AND THE BIRDS SING AND THE GRASS GREEN!! 

 When I use an editor, I don't want eight extra KILOBYTES of worthless 
 help screens and cursor positioning code! I just want an EDitor!! 
 Not a "viitor". Not a "emacsitor". Those aren't even WORDS!!!! ED! 
 ED! ED IS THE STANDARD!!! 

 TEXT EDITOR. 

 When IBM, in its ever-present omnipotence, needed to base their 
 "edlin" on a UNIX standard, did they mimic vi? No. Emacs? Surely 
 you jest. They chose the most karmic editor of all. The standard. 

 Ed is for those who can *remember* what they are working on. If you 
 are an idiot, you should use Emacs. If you are an Emacs, you should 
 not be vi. If you use ED, you are on THE PATH TO REDEMPTION. THE 
 SO-CALLED "VISUAL" EDITORS HAVE BEEN PLACED HERE BY ED TO
 TEMPT THE FAITHLESS. DO NOT GIVE IN!!! THE MIGHTY ED HAS SPOKEN!!! 

 -- 
 Miquel van Smoorenburg | Our vision is to speed up time, 
 miquels@cistron.nl | eventually eliminating it. 


End included message -----

See Also