1 .\" $FreeBSD: head/bin/ed/ed.1 281997 2015-04-26 10:03:05Z bapt $
23 utility is a line-oriented text editor.
24 It is used to create, display, modify and otherwise manipulate text
30 mode, in which the only difference is that the editor restricts the
31 use of filenames which start with
33 (interpreted as shell commands by
37 Note that editing outside of the current directory is only prohibited
38 if the user does not have write access to the current directory.
39 If a user has write access to the current directory, then symbolic
40 links can be created in the current directory, in which case
42 will not stop the user from editing the file that the symbolic link
47 argument, then a copy of
49 is read into the editor's buffer.
50 Changes are made to this copy and not directly to
55 any changes not explicitly saved with a
59 Editing is done in two distinct modes:
66 In this mode commands are read from the standard input and
67 executed to manipulate the contents of the editor buffer.
68 A typical command might look like:
77 which replaces all occurrences of the string
82 When an input command, such as
91 This is the primary means
92 of adding text to a file.
93 In this mode, no commands are available;
94 instead, the standard input is written
95 directly to the editor buffer.
96 Lines consist of text up to and
100 Input mode is terminated by
101 entering a single period
107 commands operate on whole lines or ranges of lines; e.g.,
110 command deletes lines; the
112 command moves lines, and so on.
113 It is possible to modify only a portion of a line by means of replacement,
114 as in the example above.
115 However even here, the
117 command is applied to whole lines at a time.
121 commands consist of zero or more line addresses, followed by a single
122 character command and possibly additional parameters; i.e.,
123 commands have the structure:
127 .Op Ar address Op , Ar address
128 .Ar command Op Ar parameters
132 The address(es) indicate the line or range of lines to be affected by the
134 If fewer addresses are given than the command accepts, then
135 default addresses are supplied.
137 The following options are available:
138 .Bl -tag -width indent
140 Suppress diagnostics.
141 This should be used if
143 standard input is from a script.
145 Prompt for an encryption key to be used in subsequent reads and writes
150 Specify a command prompt.
151 This may be toggled on and off with the
155 Specify the name of a file to read.
159 bang (!), then it is interpreted as a shell command.
162 the standard output of
166 To read a file whose name begins with a bang, prefix the
167 name with a backslash (\\).
168 The default filename is set to
170 only if it is not prefixed with a bang.
173 An address represents the number of a line in the buffer.
179 typically supplied to commands as the default address when none is specified.
180 When a file is first read, the current address is set to the last line
182 In general, the current address is set to the last line
183 affected by a command.
186 constructed from one of the bases in the list below, optionally followed
188 The offset may include any combination
189 of digits, operators (i.e.,
195 Addresses are read from left to right, and their values are computed
196 relative to the current address.
198 One exception to the rule that addresses represent line numbers is the
202 This means "before the first line,"
203 and is legal wherever it makes sense.
205 An address range is two addresses separated either by a comma or
207 The value of the first address in a range cannot exceed the
209 If only one address is given in a range, then
210 the second address is set to the given address.
213 of addresses is given where
215 then the corresponding range is determined by the last two addresses in
218 If only one address is expected, then the last address is used.
220 Each address in a comma-delimited range is interpreted relative to the
222 In a semi-colon-delimited range, the first address is
223 used to set the current address, and the second address is interpreted
224 relative to the first.
226 The following address symbols are recognized:
227 .Bl -tag -width indent
229 The current line (address) in the buffer.
231 The last line in the buffer.
238 is a number in the range
242 This is equivalent to
244 and may be repeated with cumulative effect.
250 is a non-negative number.
253 This is equivalent to
255 and may be repeated with cumulative effect.
261 is a non-negative number.
263 The first through last lines in the buffer.
264 This is equivalent to
268 The current through last lines in the buffer.
269 This is equivalent to
273 The next line containing the regular expression
275 The search wraps to the beginning of the buffer and continues down to the
276 current line, if necessary.
277 // repeats the last search.
280 previous line containing the regular expression
282 The search wraps to the end of the buffer and continues up to the
283 current line, if necessary.
284 ?? repeats the last search.
287 line previously marked by a
289 (mark) command, where
291 is a lower case letter.
293 .Sh REGULAR EXPRESSIONS
294 Regular expressions are patterns used in selecting text.
295 For example, the command:
298 .Cm g No / Em string Xo
303 prints all lines containing
305 Regular expressions are also
308 command for selecting old text to be replaced with new.
310 In addition to a specifying string literals, regular expressions can
313 Strings thus represented are said to be matched
314 by the corresponding regular expression.
315 If it is possible for a regular expression
316 to match several strings in a line, then the left-most longest match is
319 The following symbols are used in constructing regular expressions:
320 .Bl -tag -width indent
324 not listed below, including
334 Any backslash-escaped character
346 Match any single character.
348 Match any single character in
354 it must be the first character.
355 A range of characters may be specified by separating the end characters
360 specifies the lower case characters.
361 The following literal expressions can also be used in
363 to specify sets of characters:
365 .Bl -column "[:alnum:]" "[:cntrl:]" "[:lower:]" "[:xdigit:]" -compact
366 .It [:alnum:] Ta [:cntrl:] Ta [:lower:] Ta [:space:]
367 .It [:alpha:] Ta [:digit:] Ta [:print:] Ta [:upper:]
368 .It [:blank:] Ta [:graph:] Ta [:punct:] Ta [:xdigit:]
373 appears as the first or last
376 then it matches itself.
377 All other characters in
385 .Bl -item -compact -offset 2n
387 .Op \&. Ns Ar col-elm Ns .\&
390 .Op = Ns Ar col-elm Ns =
396 .Em collating element
397 are interpreted according to the current locale settings
398 (not currently supported).
403 for an explanation of these constructs.
405 Match any single character, other than newline, not in
413 is the first character of a regular expression, then it
414 anchors the regular expression to the beginning of a line.
415 Otherwise, it matches itself.
419 is the last character of a regular expression, it
420 anchors the regular expression to the end of a line.
421 Otherwise, it matches itself.
423 Anchor the single character regular expression or subexpression
424 immediately following it to the beginning of a word.
425 (This may not be available)
427 Anchor the single character regular expression or subexpression
428 immediately following it to the end of a word.
429 (This may not be available)
431 Define a subexpression
433 Subexpressions may be nested.
434 A subsequent backreference of the form
438 is a number in the range [1,9], expands to the text matched by the
441 For example, the regular expression
444 consisting of identical adjacent substrings.
445 Subexpressions are ordered relative to
446 their left delimiter.
448 Match the single character regular expression or subexpression
449 immediately preceding it zero or more times.
453 character of a regular expression or subexpression, then it matches
457 operator sometimes yields unexpected results.
458 For example, the regular expression
460 matches the beginning of
463 (as opposed to the substring
466 is the only left-most match.
467 .It \e{n,m\e} or \e{n,\e} or \e{n\e}
468 Match the single character regular expression or subexpression
469 immediately preceding it at least
476 is omitted, then it matches at least
479 If the comma is also omitted, then it matches exactly
484 Additional regular expression operators may be defined depending on the
491 commands are single characters, though some require additional parameters.
492 If a command's parameters extend over several lines, then
493 each line except for the last
494 must be terminated with a backslash (\\).
496 In general, at most one command is allowed per line.
497 However, most commands accept a print suffix, which is any of
505 to print the last line affected by the command.
507 An interrupt (typically ^C) has the effect of aborting the current command
508 and returning the editor to command mode.
513 recognizes the following commands.
514 The commands are shown together with
515 the default address or address range supplied if none is
516 specified (in parenthesis).
517 .Bl -tag -width indent
519 Append text to the buffer after the addressed line.
520 Text is entered in input mode.
521 The current address is set to last line entered.
523 Change lines in the buffer.
524 The addressed lines are deleted
525 from the buffer, and text is appended in their place.
526 Text is entered in input mode.
527 The current address is set to last line entered.
529 Delete the addressed lines from the buffer.
530 If there is a line after the deleted range, then the current address is set
532 Otherwise the current address is set to the line
533 before the deleted range.
537 and sets the default filename.
540 is not specified, then the default filename is used.
541 Any lines in the buffer are deleted before
542 the new file is read.
543 The current address is set to the last line read.
545 Edit the standard output of
550 The default filename is unchanged.
551 Any lines in the buffer are deleted before the output of
554 The current address is set to the last line read.
559 This is similar to the
562 except that unwritten changes are discarded without warning.
563 The current address is set to the last line read.
565 Set the default filename to
569 is not specified, then the default unescaped filename is printed.
570 .It (1,$)g/re/command-list
573 to each of the addressed lines matching a regular expression
575 The current address is set to the
576 line currently matched before
581 command, the current address is set to the last line affected by
586 must be on a separate line,
587 and every line except for the last must be terminated by a backslash
589 Any commands are allowed, except for
601 Interactively edit the addressed lines matching a regular expression
603 For each matching line,
605 the current address is set,
606 and the user is prompted to enter a
610 command, the current address
611 is set to the last line affected by (the last)
616 is the same as that of the
619 A newline alone acts as a null command list.
622 repeats the last non-null command list.
624 Toggle the printing of error explanations.
625 By default, explanations are not printed.
626 It is recommended that
628 scripts begin with this command to aid in debugging.
630 Print an explanation of the last error.
632 Insert text in the buffer before the current line.
633 Text is entered in input mode.
634 The current address is set to the last line entered.
636 Join the addressed lines.
637 The addressed lines are
638 deleted from the buffer and replaced by a single
639 line containing their joined text.
640 The current address is set to the resultant line.
642 Mark a line with a lower case letter
644 The line can then be addressed as
646 (i.e., a single quote followed by
648 in subsequent commands.
649 The mark is not cleared until the line is
650 deleted or otherwise modified.
652 Print the addressed lines unambiguously.
653 If a single line fills more than one screen (as might be the case
654 when viewing a binary file, for instance), a
656 prompt is printed on the last line.
659 utility waits until the RETURN key is pressed
660 before displaying the next screen.
661 The current address is set to the last line
664 Move lines in the buffer.
665 The addressed lines are moved to after the
666 right-hand destination address, which may be the address
669 The current address is set to the
672 Print the addressed lines along with
674 The current address is set to the last line
677 Print the addressed lines.
678 The current address is set to the last line
681 Toggle the command prompt on and off.
682 Unless a prompt was specified by with command-line option
684 the command prompt is by default turned off.
692 This is similar to the
695 except that unwritten changes are discarded without warning.
699 to after the addressed line.
702 is not specified, then the default
704 If there was no default filename prior to the command,
705 then the default filename is set to
707 Otherwise, the default filename is unchanged.
708 The current address is set to the last line read.
711 to after the addressed line
712 the standard output of
717 The default filename is unchanged.
718 The current address is set to the last line read.
719 .It (.,.)s/re/replacement/
720 .It (.,.)s/re/replacement/g
721 .It (.,.)s/re/replacement/n
722 Replace text in the addressed lines
723 matching a regular expression
727 By default, only the first match in each line is replaced.
730 (global) suffix is given, then every match to be replaced.
735 is a positive number, causes only the
737 match to be replaced.
738 It is an error if no substitutions are performed on any of the addressed
740 The current address is set the last line affected.
745 may be delimited by any character other than space and newline
749 If one or two of the last delimiters is omitted, then the last line
750 affected is printed as though the print suffix
758 is replaced by the currently matched text.
759 The character sequence
763 is a number in the range [1,9], is replaced by the
765 backreference expression of the matched text.
772 from the last substitution is used.
773 Newlines may be embedded in
775 if they are escaped with a backslash (\\).
777 Repeat the last substitution.
780 command accepts a count suffix
782 or any combination of the characters
789 is given, then only the
795 the regular expression of the last search to be used instead of the
796 that of the last substitution.
799 suffix toggles the global suffix of the last substitution.
802 suffix toggles the print suffix of the last substitution
803 The current address is set to the last line affected.
805 Copy (i.e., transfer) the addressed lines to after the right-hand
806 destination address, which may be the address
809 The current address is set to the last line
812 Undo the last command and restores the current address
813 to what it was before the command.
820 are treated as a single command by undo.
823 .It (1,$)v/re/command-list
826 to each of the addressed lines not matching a regular expression
828 This is similar to the
832 Interactively edit the addressed lines not matching a regular expression
834 This is similar to the
838 Write the addressed lines to
840 Any previous contents of
842 is lost without warning.
843 If there is no default filename, then the default filename is set to
845 otherwise it is unchanged.
846 If no filename is specified, then the default
848 The current address is unchanged.
850 Write the addressed lines to
855 .It (1,$)w Ar !command
856 Write the addressed lines to the standard input of
861 The default filename and current address are unchanged.
863 Append the addressed lines to the end of
865 This is similar to the
867 command, expect that the previous contents of file is not clobbered.
868 The current address is unchanged.
870 Prompt for an encryption key which is used in subsequent reads and
872 If a newline alone is entered as the key, then encryption is
874 Otherwise, echoing is disabled while a key is read.
875 Encryption/decryption is done using the
881 lines at a time starting at addressed line.
884 is not specified, then the current window size is used.
885 The current address is set to the last line printed.
891 If the first character of
895 then it is replaced by text of the
900 utility does not process
902 for backslash (\\) escapes.
903 However, an unescaped
905 is replaced by the default filename.
906 When the shell returns from execution, a
908 is printed to the standard output.
909 The current line is unchanged.
911 Print the line number of the addressed line.
913 Print the addressed line, and sets the current address to
917 .Bl -tag -width ".Pa /tmp/ed.*" -compact
923 attempts to write the buffer if the terminal hangs up
926 When an error occurs,
930 and either returns to command mode
931 or exits if its input is from a script.
932 An explanation of the last error can be
939 (global) command masks any errors from failed searches and substitutions,
940 it can be used to perform conditional operations in scripts; e.g.,
950 replaces any occurrences of
956 (undo) command occurs in a global command list, then
957 the command list is executed only once.
959 If diagnostics are not disabled, attempting to quit
961 or edit another file before writing a modified buffer
963 If the command is entered a second time, it succeeds,
964 but any changes to the buffer are lost.
976 .%B Software Tools in Pascal
985 arguments for backslash escapes, i.e., in a filename,
986 any characters preceded by a backslash (\\) are
987 interpreted literally.
989 If a text (non-binary) file is not terminated by a newline character,
992 appends one on reading/writing it.
993 In the case of a binary file,
995 does not append a newline on reading/writing.
997 per line overhead: 4 ints
1006 utility does not recognize multibyte characters.