1 .\" $FreeBSD: src/bin/ed/ed.1,v 1.35 2005/01/16 16:41:56 ru Exp $
2 .\" $DragonFly: src/bin/ed/ed.1,v 1.6 2007/10/20 17:56:46 swildner Exp $
24 utility is a line-oriented text editor.
25 It is used to create, display, modify and otherwise manipulate text
31 mode, in which the only difference is that the editor restricts the
32 use of filenames which start with
34 (interpreted as shell commands by
38 Note that editing outside of the current directory is only prohibited
39 if the user does not have write access to the current directory.
40 If a user has write access to the current directory, then symbolic
41 links can be created in the current directory, in which case
43 will not stop the user from editing the file that the symbolic link
48 argument, then a copy of
50 is read into the editor's buffer.
51 Changes are made to this copy and not directly to
56 any changes not explicitly saved with a
60 Editing is done in two distinct modes:
67 In this mode commands are read from the standard input and
68 executed to manipulate the contents of the editor buffer.
69 A typical command might look like:
78 which replaces all occurrences of the string
83 When an input command, such as
92 This is the primary means
93 of adding text to a file.
94 In this mode, no commands are available;
95 instead, the standard input is written
96 directly to the editor buffer.
97 Lines consist of text up to and
101 Input mode is terminated by
102 entering a single period
108 commands operate on whole lines or ranges of lines; e.g.,
111 command deletes lines; the
113 command moves lines, and so on.
114 It is possible to modify only a portion of a line by means of replacement,
115 as in the example above.
116 However even here, the
118 command is applied to whole lines at a time.
122 commands consist of zero or more line addresses, followed by a single
123 character command and possibly additional parameters; i.e.,
124 commands have the structure:
128 .Op Ar address Op , Ar address
129 .Ar command Op Ar parameters
133 The address(es) indicate the line or range of lines to be affected by the
135 If fewer addresses are given than the command accepts, then
136 default addresses are supplied.
138 The following options are available:
139 .Bl -tag -width indent
141 Suppress diagnostics.
142 This should be used if
144 standard input is from a script.
146 Prompt for an encryption key to be used in subsequent reads and writes
151 Specify a command prompt.
152 This may be toggled on and off with the
156 Specify the name of a file to read.
160 bang (!), then it is interpreted as a shell command.
163 the standard output of
167 To read a file whose name begins with a bang, prefix the
168 name with a backslash (\\).
169 The default filename is set to
171 only if it is not prefixed with a bang.
174 An address represents the number of a line in the buffer.
180 typically supplied to commands as the default address when none is specified.
181 When a file is first read, the current address is set to the last line
183 In general, the current address is set to the last line
184 affected by a command.
187 constructed from one of the bases in the list below, optionally followed
189 The offset may include any combination
190 of digits, operators (i.e.,
196 Addresses are read from left to right, and their values are computed
197 relative to the current address.
199 One exception to the rule that addresses represent line numbers is the
203 This means "before the first line,"
204 and is legal wherever it makes sense.
206 An address range is two addresses separated either by a comma or
208 The value of the first address in a range cannot exceed the
210 If only one address is given in a range, then
211 the second address is set to the given address.
214 of addresses is given where
216 then the corresponding range is determined by the last two addresses in
219 If only one address is expected, then the last address is used.
221 Each address in a comma-delimited range is interpreted relative to the
223 In a semi-colon-delimited range, the first address is
224 used to set the current address, and the second address is interpreted
225 relative to the first.
227 The following address symbols are recognized:
228 .Bl -tag -width indent
230 The current line (address) in the buffer.
232 The last line in the buffer.
239 is a number in the range
243 This is equivalent to
245 and may be repeated with cumulative effect.
251 is a non-negative number.
254 This is equivalent to
256 and may be repeated with cumulative effect.
262 is a non-negative number.
264 The first through last lines in the buffer.
265 This is equivalent to
269 The current through last lines in the buffer.
270 This is equivalent to
274 The next line containing the regular expression
276 The search wraps to the beginning of the buffer and continues down to the
277 current line, if necessary.
278 // repeats the last search.
281 previous line containing the regular expression
283 The search wraps to the end of the buffer and continues up to the
284 current line, if necessary.
285 ?? repeats the last search.
288 line previously marked by a
290 (mark) command, where
292 is a lower case letter.
294 .Sh REGULAR EXPRESSIONS
295 Regular expressions are patterns used in selecting text.
296 For example, the command:
299 .Cm g No / Em string Xo
304 prints all lines containing
306 Regular expressions are also
309 command for selecting old text to be replaced with new.
311 In addition to a specifying string literals, regular expressions can
314 Strings thus represented are said to be matched
315 by the corresponding regular expression.
316 If it is possible for a regular expression
317 to match several strings in a line, then the left-most longest match is
320 The following symbols are used in constructing regular expressions:
321 .Bl -tag -width indent
325 not listed below, including
335 Any backslash-escaped character
347 Match any single character.
349 Match any single character in
355 it must be the first character.
356 A range of characters may be specified by separating the end characters
361 specifies the lower case characters.
362 The following literal expressions can also be used in
364 to specify sets of characters:
366 .Bl -column "[:alnum:]" "[:cntrl:]" "[:lower:]" "[:xdigit:]" -compact
367 .It [:alnum:] Ta [:cntrl:] Ta [:lower:] Ta [:space:]
368 .It [:alpha:] Ta [:digit:] Ta [:print:] Ta [:upper:]
369 .It [:blank:] Ta [:graph:] Ta [:punct:] Ta [:xdigit:]
374 appears as the first or last
377 then it matches itself.
378 All other characters in
386 .Bl -item -compact -offset 2n
388 .Op \&. Ns Ar col-elm Ns .\&
391 .Op = Ns Ar col-elm Ns =
397 .Em collating element
398 are interpreted according to the current locale settings
399 (not currently supported).
404 for an explanation of these constructs.
406 Match any single character, other than newline, not in
414 is the first character of a regular expression, then it
415 anchors the regular expression to the beginning of a line.
416 Otherwise, it matches itself.
420 is the last character of a regular expression, it
421 anchors the regular expression to the end of a line.
422 Otherwise, it matches itself.
424 Anchor the single character regular expression or subexpression
425 immediately following it to the beginning of a word.
426 (This may not be available)
428 Anchor the single character regular expression or subexpression
429 immediately following it to the end of a word.
430 (This may not be available)
432 Define a subexpression
434 Subexpressions may be nested.
435 A subsequent backreference of the form
439 is a number in the range [1,9], expands to the text matched by the
442 For example, the regular expression
445 consisting of identical adjacent substrings.
446 Subexpressions are ordered relative to
447 their left delimiter.
449 Match the single character regular expression or subexpression
450 immediately preceding it zero or more times.
454 character of a regular expression or subexpression, then it matches
458 operator sometimes yields unexpected results.
459 For example, the regular expression
461 matches the beginning of
464 (as opposed to the substring
467 is the only left-most match.
468 .It \e{n,m\e} or \e{n,\e} or \e{n\e}
469 Match the single character regular expression or subexpression
470 immediately preceding it at least
477 is omitted, then it matches at least
480 If the comma is also omitted, then it matches exactly
485 Additional regular expression operators may be defined depending on the
492 commands are single characters, though some require additional parameters.
493 If a command's parameters extend over several lines, then
494 each line except for the last
495 must be terminated with a backslash (\\).
497 In general, at most one command is allowed per line.
498 However, most commands accept a print suffix, which is any of
506 to print the last line affected by the command.
508 An interrupt (typically ^C) has the effect of aborting the current command
509 and returning the editor to command mode.
514 recognizes the following commands.
515 The commands are shown together with
516 the default address or address range supplied if none is
517 specified (in parenthesis).
518 .Bl -tag -width indent
520 Append text to the buffer after the addressed line.
521 Text is entered in input mode.
522 The current address is set to last line entered.
524 Change lines in the buffer.
525 The addressed lines are deleted
526 from the buffer, and text is appended in their place.
527 Text is entered in input mode.
528 The current address is set to last line entered.
530 Delete the addressed lines from the buffer.
531 If there is a line after the deleted range, then the current address is set
533 Otherwise the current address is set to the line
534 before the deleted range.
538 and sets the default filename.
541 is not specified, then the default filename is used.
542 Any lines in the buffer are deleted before
543 the new file is read.
544 The current address is set to the last line read.
546 Edit the standard output of
551 The default filename is unchanged.
552 Any lines in the buffer are deleted before the output of
555 The current address is set to the last line read.
560 This is similar to the
563 except that unwritten changes are discarded without warning.
564 The current address is set to the last line read.
566 Set the default filename to
570 is not specified, then the default unescaped filename is printed.
571 .It (1,$)g/re/command-list
574 to each of the addressed lines matching a regular expression
576 The current address is set to the
577 line currently matched before
582 command, the current address is set to the last line affected by
587 must be on a separate line,
588 and every line except for the last must be terminated by a backslash
590 Any commands are allowed, except for
602 Interactively edit the addressed lines matching a regular expression
604 For each matching line,
606 the current address is set,
607 and the user is prompted to enter a
611 command, the current address
612 is set to the last line affected by (the last)
617 is the same as that of the
620 A newline alone acts as a null command list.
623 repeats the last non-null command list.
625 Toggle the printing of error explanations.
626 By default, explanations are not printed.
627 It is recommended that
629 scripts begin with this command to aid in debugging.
631 Print an explanation of the last error.
633 Insert text in the buffer before the current line.
634 Text is entered in input mode.
635 The current address is set to the last line entered.
637 Join the addressed lines.
638 The addressed lines are
639 deleted from the buffer and replaced by a single
640 line containing their joined text.
641 The current address is set to the resultant line.
643 Mark a line with a lower case letter
645 The line can then be addressed as
647 (i.e., a single quote followed by
649 in subsequent commands.
650 The mark is not cleared until the line is
651 deleted or otherwise modified.
653 Print the addressed lines unambiguously.
654 If a single line fills for than one screen (as might be the case
655 when viewing a binary file, for instance), a
657 prompt is printed on the last line.
660 utility waits until the RETURN key is pressed
661 before displaying the next screen.
662 The current address is set to the last line
665 Move lines in the buffer.
666 The addressed lines are moved to after the
667 right-hand destination address, which may be the address
670 The current address is set to the
673 Print the addressed lines along with
675 The current address is set to the last line
678 Print the addressed lines.
679 The current address is set to the last line
682 Toggle the command prompt on and off.
683 Unless a prompt was specified by with command-line option
685 the command prompt is by default turned off.
693 This is similar to the
696 except that unwritten changes are discarded without warning.
700 to after the addressed line.
703 is not specified, then the default
705 If there was no default filename prior to the command,
706 then the default filename is set to
708 Otherwise, the default filename is unchanged.
709 The current address is set to the last line read.
712 to after the addressed line
713 the standard output of
718 The default filename is unchanged.
719 The current address is set to the last line read.
720 .It (.,.)s/re/replacement/
721 .It (.,.)s/re/replacement/g
722 .It (.,.)s/re/replacement/n
723 Replace text in the addressed lines
724 matching a regular expression
728 By default, only the first match in each line is replaced.
731 (global) suffix is given, then every match to be replaced.
736 is a positive number, causes only the
738 match to be replaced.
739 It is an error if no substitutions are performed on any of the addressed
741 The current address is set the last line affected.
746 may be delimited by any character other than space and newline
750 If one or two of the last delimiters is omitted, then the last line
751 affected is printed as though the print suffix
759 is replaced by the currently matched text.
760 The character sequence
764 is a number in the range [1,9], is replaced by the
766 backreference expression of the matched text.
773 from the last substitution is used.
774 Newlines may be embedded in
776 if they are escaped with a backslash (\\).
778 Repeat the last substitution.
781 command accepts a count suffix
783 or any combination of the characters
790 is given, then only the
796 the regular expression of the last search to be used instead of the
797 that of the last substitution.
800 suffix toggles the global suffix of the last substitution.
803 suffix toggles the print suffix of the last substitution
804 The current address is set to the last line affected.
806 Copy (i.e., transfer) the addressed lines to after the right-hand
807 destination address, which may be the address
810 The current address is set to the last line
813 Undo the last command and restores the current address
814 to what it was before the command.
821 are treated as a single command by undo.
824 .It (1,$)v/re/command-list
827 to each of the addressed lines not matching a regular expression
829 This is similar to the
833 Interactively edit the addressed lines not matching a regular expression
835 This is similar to the
839 Write the addressed lines to
841 Any previous contents of
843 is lost without warning.
844 If there is no default filename, then the default filename is set to
846 otherwise it is unchanged.
847 If no filename is specified, then the default
849 The current address is unchanged.
851 Write the addressed lines to
856 .It (1,$)w Ar !command
857 Write the addressed lines to the standard input of
862 The default filename and current address are unchanged.
864 Append the addressed lines to the end of
866 This is similar to the
868 command, expect that the previous contents of file is not clobbered.
869 The current address is unchanged.
871 Prompt for an encryption key which is used in subsequent reads and
873 If a newline alone is entered as the key, then encryption is
875 Otherwise, echoing is disabled while a key is read.
876 Encryption/decryption is done using the
882 lines at a time starting at addressed line.
885 is not specified, then the current window size is used.
886 The current address is set to the last line printed.
892 If the first character of
896 then it is replaced by text of the
901 utility does not process
903 for backslash (\\) escapes.
904 However, an unescaped
906 is replaced by the default filename.
907 When the shell returns from execution, a
909 is printed to the standard output.
910 The current line is unchanged.
912 Print the line number of the addressed line.
914 Print the addressed line, and sets the current address to
918 .Bl -tag -width /tmp/ed.* -compact
924 attempts to write the buffer if the terminal hangs up
927 When an error occurs,
931 and either returns to command mode
932 or exits if its input is from a script.
933 An explanation of the last error can be
940 (global) command masks any errors from failed searches and substitutions,
941 it can be used to perform conditional operations in scripts; e.g.,
951 replaces any occurrences of
957 (undo) command occurs in a global command list, then
958 the command list is executed only once.
960 If diagnostics are not disabled, attempting to quit
962 or edit another file before writing a modified buffer
964 If the command is entered a second time, it succeeds,
965 but any changes to the buffer are lost.
977 .%B Software Tools in Pascal
986 arguments for backslash escapes, i.e., in a filename,
987 any characters preceded by a backslash (\\) are
988 interpreted literally.
990 If a text (non-binary) file is not terminated by a newline character,
993 appends one on reading/writing it.
994 In the case of a binary file,
996 does not append a newline on reading/writing.
998 per line overhead: 4 ints
1007 utility does not recognize multibyte characters.