xref: /netbsd-src/bin/ed/ed.1 (revision 883cee82b383717759001da56d2a6d91c4c3b170)
1*883cee82Swiz.\"	$NetBSD: ed.1,v 1.35 2018/04/09 06:57:01 wiz Exp $
225eb7d56Swiz.\"	$OpenBSD: ed.1,v 1.42 2003/07/27 13:25:43 jmc Exp $
349f0ad86Scgd.\"
425eb7d56Swiz.\" Copyright (c) 1993 Andrew Moore, Talke Studio.
525eb7d56Swiz.\" All rights reserved.
625eb7d56Swiz.\"
725eb7d56Swiz.\" Redistribution and use in source and binary forms, with or without
825eb7d56Swiz.\" modification, are permitted provided that the following conditions
925eb7d56Swiz.\" are met:
1025eb7d56Swiz.\" 1. Redistributions of source code must retain the above copyright
1125eb7d56Swiz.\"    notice, this list of conditions and the following disclaimer.
1225eb7d56Swiz.\" 2. Redistributions in binary form must reproduce the above copyright
1325eb7d56Swiz.\"    notice, this list of conditions and the following disclaimer in the
1425eb7d56Swiz.\"    documentation and/or other materials provided with the distribution.
1525eb7d56Swiz.\"
1625eb7d56Swiz.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1725eb7d56Swiz.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1825eb7d56Swiz.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1925eb7d56Swiz.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2025eb7d56Swiz.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2125eb7d56Swiz.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2225eb7d56Swiz.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2325eb7d56Swiz.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2425eb7d56Swiz.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2525eb7d56Swiz.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2625eb7d56Swiz.\" SUCH DAMAGE.
2725eb7d56Swiz.\"
28107d499dSsnj.Dd April 5, 2018
2925eb7d56Swiz.Dt ED 1
3025eb7d56Swiz.Os
3125eb7d56Swiz.Sh NAME
3225eb7d56Swiz.Nm ed
3325eb7d56Swiz.Nd text editor
3425eb7d56Swiz.Sh SYNOPSIS
3525eb7d56Swiz.Nm
3625eb7d56Swiz.Op Fl
37c02518ebSchristos.Op Fl ESsx
3825eb7d56Swiz.Op Fl p Ar string
3925eb7d56Swiz.Op Ar file
4025eb7d56Swiz.Sh DESCRIPTION
4125eb7d56Swiz.Nm
4220d73299Salmis a line-oriented text editor.
4325eb7d56SwizIt is used to create, display, modify, and otherwise manipulate text files.
44f7f6e6ffSalmIf invoked with a
4525eb7d56Swiz.Ar file
4620d73299Salmargument, then a copy of
4725eb7d56Swiz.Ar file
4820d73299Salmis read into the editor's buffer.
4920d73299SalmChanges are made to this copy and not directly to
5025eb7d56Swiz.Ar file
5120d73299Salmitself.
5220d73299SalmUpon quitting
5325eb7d56Swiz.Nm ,
5420d73299Salmany changes not explicitly saved with a
5525eb7d56Swiz.Ic w
5620d73299Salmcommand are lost.
5725eb7d56Swiz.Pp
5820d73299SalmEditing is done in two distinct modes:
5925eb7d56Swiz.Em command
6020d73299Salmand
6125eb7d56Swiz.Em input .
6220d73299SalmWhen first invoked,
6325eb7d56Swiz.Nm
6420d73299Salmis in command mode.
6525eb7d56SwizIn this mode, commands are read from the standard input and
6620d73299Salmexecuted to manipulate the contents of the editor buffer.
6725eb7d56Swiz.Pp
6820d73299SalmA typical command might look like:
6925eb7d56Swiz.Bd -literal -offset indent
7025eb7d56Swiz,s/old/new/g
7125eb7d56Swiz.Ed
7225eb7d56Swiz.Pp
7318dec071Sperrywhich replaces all occurrences of the string
7425eb7d56Swiz.Pa old
7520d73299Salmwith
7625eb7d56Swiz.Pa new .
7725eb7d56Swiz.Pp
7820d73299SalmWhen an input command, such as
7925eb7d56Swiz.Ic a
8020d73299Salm(append),
8125eb7d56Swiz.Ic i
8225eb7d56Swiz(insert),
8325eb7d56Swizor
8425eb7d56Swiz.Ic c
8525eb7d56Swiz(change) is given,
8625eb7d56Swiz.Nm
8714dfaa4bSwizenters input mode.
8814dfaa4bSwizThis is the primary means of adding text to a file.
8920d73299SalmIn this mode, no commands are available;
9014dfaa4bSwizinstead, the standard input is written directly to the editor buffer.
9125eb7d56SwizLines consist of text up to and including a newline character.
9225eb7d56SwizInput mode is terminated by entering a single period
9325eb7d56Swiz.Pq Ql \&.
9425eb7d56Swizon a line.
9525eb7d56Swiz.Pp
9620d73299SalmAll
9725eb7d56Swiz.Nm
9820d73299Salmcommands operate on whole lines or ranges of lines; e.g.,
9920d73299Salmthe
10025eb7d56Swiz.Ic d
10120d73299Salmcommand deletes lines; the
10225eb7d56Swiz.Ic m
10320d73299Salmcommand moves lines, and so on.
10420d73299SalmIt is possible to modify only a portion of a line by means of replacement,
10514dfaa4bSwizas in the example above.
10625eb7d56SwizHowever, even here, the
10725eb7d56Swiz.Ic s
10820d73299Salmcommand is applied to whole lines at a time.
10925eb7d56Swiz.Pp
11020d73299SalmIn general,
11125eb7d56Swiz.Nm
11220d73299Salmcommands consist of zero or more line addresses, followed by a single
11320d73299Salmcharacter command and possibly additional parameters; i.e.,
11420d73299Salmcommands have the structure:
11525eb7d56Swiz.Bd -literal -offset indent
11625eb7d56Swiz[address [,address]]command[parameters]
11725eb7d56Swiz.Ed
11825eb7d56Swiz.Pp
119aef94df1SalmThe address(es) indicate the line or range of lines to be affected by the
12014dfaa4bSwizcommand.
12114dfaa4bSwizIf fewer addresses are given than the command accepts, then
122aef94df1Salmdefault addresses are supplied.
12325eb7d56Swiz.Pp
12425eb7d56SwizThe options are as follows:
12525eb7d56Swiz.Bl -tag -width Ds
12625eb7d56Swiz.It Fl
12725eb7d56SwizSame as the
12825eb7d56Swiz.Fl s
12925eb7d56Swizoption (deprecated).
13029119d0eSwiz.It Fl E
13129119d0eSwizEnables the use of extended regular expressions instead of the basic
13229119d0eSwizregular expressions that are normally used.
13329119d0eSwiz.It Fl p Ar string
13429119d0eSwizSpecifies a command prompt.
13529119d0eSwizThis may be toggled on and off with the
13629119d0eSwiz.Ic P
13729119d0eSwizcommand.
138*883cee82Swiz.It Fl S
139*883cee82SwizDisables using of the
140*883cee82Swiz.Ic \&!
141*883cee82Swizcommand (executing a subshell).
142*883cee82SwizIntended to be used by batch jobs like
143*883cee82Swiz.Xr patch 1 .
14425eb7d56Swiz.It Fl s
14525eb7d56SwizSuppress diagnostics.
14614dfaa4bSwizThis should be used if
14725eb7d56Swiz.Nm
14820d73299Salmstandard input is from a script.
14925eb7d56Swiz.It Fl x
15025eb7d56SwizPrompt for an encryption key to be used in subsequent reads and writes
15120d73299Salm(see the
15225eb7d56Swiz.Ic x
15320d73299Salmcommand).
15425eb7d56Swiz.It Ar file
15514dfaa4bSwizSpecifies the name of a file to read.
15614dfaa4bSwizIf
15725eb7d56Swiz.Ar file
15820d73299Salmis prefixed with a
15925eb7d56Swizbang
16025eb7d56Swiz.Pq Ql \&! ,
16125eb7d56Swizthen it is interpreted as a shell command.
16225eb7d56SwizIn this case, what is read is the standard output of
16325eb7d56Swiz.Ar file
16420d73299Salmexecuted via
16525eb7d56Swiz.Xr sh 1 .
16620d73299SalmTo read a file whose name begins with a bang, prefix the
16725eb7d56Swizname with a backslash
16825eb7d56Swiz.Pq Ql \e .
169f7f6e6ffSalmThe default filename is set to
17025eb7d56Swiz.Ar file
171f7f6e6ffSalmonly if it is not prefixed with a bang.
17225eb7d56Swiz.El
17325eb7d56Swiz.Ss LINE ADDRESSING
174ba4d688dSalmAn address represents the number of a line in the buffer.
17525eb7d56Swiz.Nm
17620d73299Salmmaintains a
17725eb7d56Swiz.Em current address
17825eb7d56Swizwhich is typically supplied to commands as the default address
17925eb7d56Swizwhen none is specified.
18020d73299SalmWhen a file is first read, the current address is set to the last line
18114dfaa4bSwizof the file.
18214dfaa4bSwizIn general, the current address is set to the last line affected by a command.
18325eb7d56Swiz.Pp
18425eb7d56SwizA line address is
18525eb7d56Swizconstructed from one of the bases in the list below, optionally followed
18625eb7d56Swizby a numeric offset.
18714dfaa4bSwizThe offset may include any combination of digits, operators (i.e.,
18825eb7d56Swiz.Sq + ,
18925eb7d56Swiz.Sq - ,
19020d73299Salmand
19125eb7d56Swiz.Sq ^ ) ,
19220d73299Salmand whitespace.
19320d73299SalmAddresses are read from left to right, and their values are computed
19420d73299Salmrelative to the current address.
19525eb7d56Swiz.Pp
19620d73299SalmOne exception to the rule that addresses represent line numbers is the
19720d73299Salmaddress
19825eb7d56Swiz.Em 0
19920d73299Salm(zero).
20025eb7d56SwizThis means
20125eb7d56Swiz.Dq before the first line ,
20220d73299Salmand is legal wherever it makes sense.
20325eb7d56Swiz.Pp
20425eb7d56SwizAn address range is two addresses separated either by a comma or semi-colon.
20514dfaa4bSwizThe value of the first address in a range cannot exceed the
20614dfaa4bSwizvalue of the second.
20725eb7d56SwizIf only one address is given in a range,
20825eb7d56Swizthen the second address is set to the given address.
20914dfaa4bSwizIf an
21025eb7d56Swiz.Em n Ns No -tuple
21120d73299Salmof addresses is given where
21201869ca4Swiz.Em n > 2 ,
21325eb7d56Swizthen the corresponding range is determined by the last two addresses in the
21425eb7d56Swiz.Em n Ns No -tuple.
215ba4d688dSalmIf only one address is expected, then the last address is used.
21625eb7d56Swiz.Pp
21720d73299SalmEach address in a comma-delimited range is interpreted relative to the
21814dfaa4bSwizcurrent address.
21925eb7d56SwizIn a semi-colon-delimited range, the first address is
22025eb7d56Swizused to set the current address, and the second address is interpreted
22125eb7d56Swizrelative to the first.
22225eb7d56Swiz.Pp
22325eb7d56SwizThe following address symbols are recognized:
22425eb7d56Swiz.Bl -tag -width Ds
22525eb7d56Swiz.It Em \&.
22620d73299SalmThe current line (address) in the buffer.
22725eb7d56Swiz.It Em $
22820d73299SalmThe last line in the buffer.
22925eb7d56Swiz.It Em n
23020d73299SalmThe
23125eb7d56Swiz.Em n Ns No th
23225eb7d56Swizline in the buffer where
23325eb7d56Swiz.Em n
23420d73299Salmis a number in the range
23525eb7d56Swiz.Em [0,$] .
23625eb7d56Swiz.It Em - No or Em ^
23720d73299SalmThe previous line.
23820d73299SalmThis is equivalent to
23925eb7d56Swiz.Em -1
24020d73299Salmand may be repeated with cumulative effect.
24125eb7d56Swiz.It Em -n No or Em ^n
24220d73299SalmThe
24325eb7d56Swiz.Em n Ns No th
24420d73299Salmprevious line, where
24525eb7d56Swiz.Em n
24620d73299Salmis a non-negative number.
24725eb7d56Swiz.It Em +
24825eb7d56SwizThe next line.
24920d73299SalmThis is equivalent to
25025eb7d56Swiz.Em +1
25120d73299Salmand may be repeated with cumulative effect.
25225eb7d56Swiz.It Em +n
25320d73299SalmThe
25425eb7d56Swiz.Em n Ns No th
25520d73299Salmnext line, where
25625eb7d56Swiz.Em n
25720d73299Salmis a non-negative number.
25825eb7d56Swiz.It Em whitespace Em n
25925eb7d56Swiz.Em whitespace
26020d73299Salmfollowed by a number
26125eb7d56Swiz.Em n
26220d73299Salmis interpreted as
26325eb7d56Swiz.Sq Em +n .
26425eb7d56Swiz.It Em \&, No or Em %
26514dfaa4bSwizThe first through last lines in the buffer.
26614dfaa4bSwizThis is equivalent to the address range
26725eb7d56Swiz.Em 1,$ .
26825eb7d56Swiz.It Em \&;
26925eb7d56SwizThe current through last lines in the buffer.
27014dfaa4bSwizThis is equivalent to the address range
27125eb7d56Swiz.Em .,$ .
27225eb7d56Swiz.It Em / Ns Ar re Ns Em /
27325eb7d56SwizThe next line containing the regular expression
27425eb7d56Swiz.Ar re .
27520d73299SalmThe search wraps to the beginning of the buffer and continues down to the
27620d73299Salmcurrent line, if necessary.
27725eb7d56Swiz.Em //
27825eb7d56Swizrepeats the last search.
279fd71eb70Sjoerg.It Em \&? Ns Ar re Ns Em \&?
28025eb7d56SwizThe previous line containing the regular expression
28125eb7d56Swiz.Ar re .
28220d73299SalmThe search wraps to the end of the buffer and continues up to the
28320d73299Salmcurrent line, if necessary.
28425eb7d56Swiz.Em ??
28525eb7d56Swizrepeats the last search.
28625eb7d56Swiz.It Em \&\' Ns Ar lc
28725eb7d56SwizThe line previously marked by a
28825eb7d56Swiz.Ic k
28920d73299Salm(mark) command, where
29025eb7d56Swiz.Ar lc
29120d73299Salmis a lower case letter.
29225eb7d56Swiz.El
29325eb7d56Swiz.Ss REGULAR EXPRESSIONS
29420d73299SalmRegular expressions are patterns used in selecting text.
29520d73299SalmFor example, the
29625eb7d56Swiz.Nm
29720d73299Salmcommand
29825eb7d56Swiz.Bd -literal -offset indent
29925eb7d56Swizg/string/
30025eb7d56Swiz.Ed
30125eb7d56Swiz.Pp
30220d73299Salmprints all lines containing
30325eb7d56Swiz.Em string .
30425eb7d56SwizRegular expressions are also used by the
30525eb7d56Swiz.Ic s
30620d73299Salmcommand for selecting old text to be replaced with new.
30725eb7d56Swiz.Pp
308dcc13ddbSwizIn addition to specifying string literals, regular expressions can
30914dfaa4bSwizrepresent classes of strings.
31025eb7d56SwizStrings thus represented are said to be matched by the
31125eb7d56Swizcorresponding regular expression.
31225eb7d56SwizIf it is possible for a regular expression to match several strings in
31325eb7d56Swiza line, then the leftmost longest match is the one selected.
31425eb7d56Swiz.Pp
31520d73299SalmThe following symbols are used in constructing regular expressions:
31625eb7d56Swiz.Bl -tag -width Dsasdfsd
31725eb7d56Swiz.It Em c
31820d73299SalmAny character
31925eb7d56Swiz.Em c
32025eb7d56Swiznot listed below, including
32125eb7d56Swiz.Sq { ,
32225eb7d56Swiz.Sq } ,
32325eb7d56Swiz.Sq \&( ,
32425eb7d56Swiz.Sq \&) ,
32501869ca4Swiz.Sq < ,
32625eb7d56Swizand
32701869ca4Swiz.Sq >
32820d73299Salmmatches itself.
32925eb7d56Swiz.It Em \ec
33020d73299SalmAny backslash-escaped character
33125eb7d56Swiz.Em c ,
33225eb7d56Swizexcept for
33325eb7d56Swiz.Sq { ,
33425eb7d56Swiz.Sq } ,
33525eb7d56Swiz.Sq \&( ,
33625eb7d56Swiz.Sq \&) ,
33701869ca4Swiz.Sq < ,
33825eb7d56Swizand
33901869ca4Swiz.Sq >
34025eb7d56Swizmatches itself.
34125eb7d56Swiz.It Em \&.
34220d73299SalmMatches any single character.
34325eb7d56Swiz.It Em [char-class]
34425eb7d56SwizMatches any single character in the character class
34525eb7d56Swiz.Em char-class .
346f7f6e6ffSalmSee
34725eb7d56Swiz.Sx CHARACTER CLASSES
34825eb7d56Swizbelow for further information.
34925eb7d56Swiz.It Em [^char-class]
35025eb7d56SwizMatches any single character, other than newline, not in the
35125eb7d56Swizcharacter class
35225eb7d56Swiz.Em char-class .
35325eb7d56Swiz.It Em ^
35425eb7d56SwizIf
35525eb7d56Swiz.Em ^
35625eb7d56Swizis the first character of a regular expression, then it
35720d73299Salmanchors the regular expression to the beginning of a line.
35820d73299SalmOtherwise, it matches itself.
35925eb7d56Swiz.It Em $
36025eb7d56SwizIf
36125eb7d56Swiz.Em $
36225eb7d56Swizis the last character of a regular expression,
36325eb7d56Swizit anchors the regular expression to the end of a line.
36420d73299SalmOtherwise, it matches itself.
36501869ca4Swiz.It Em \e<
36620d73299SalmAnchors the single character regular expression or subexpression
36720d73299Salmimmediately following it to the beginning of a word.
36825eb7d56Swiz(This may not be available.)
36901869ca4Swiz.It Em \e>
37020d73299SalmAnchors the single character regular expression or subexpression
37120d73299Salmimmediately following it to the end of a word.
37225eb7d56Swiz(This may not be available.)
37325eb7d56Swiz.It Em \e( Ns Ar re Ns Em \e)
37420d73299SalmDefines a subexpression
37525eb7d56Swiz.Ar re .
37620d73299SalmSubexpressions may be nested.
37725eb7d56SwizA subsequent backreference of the form
37825eb7d56Swiz.Em \en ,
37925eb7d56Swizwhere
38025eb7d56Swiz.Em n
38120d73299Salmis a number in the range [1,9], expands to the text matched by the
38225eb7d56Swiz.Em n Ns No th
38320d73299Salmsubexpression.
38425eb7d56SwizFor example, the regular expression
38525eb7d56Swiz.Em \e(.*\e)\e1
38625eb7d56Swizmatches any string consisting of identical adjacent substrings.
38714dfaa4bSwizSubexpressions are ordered relative to their left delimiter.
38825eb7d56Swiz.It Em *
38920d73299SalmMatches the single character regular expression or subexpression
39014dfaa4bSwizimmediately preceding it zero or more times.
39125eb7d56SwizIf
39225eb7d56Swiz.Em *
39325eb7d56Swizis the first character of a regular expression or subexpression,
39425eb7d56Swizthen it matches itself.
39525eb7d56SwizThe
39625eb7d56Swiz.Em *
39725eb7d56Swizoperator sometimes yields unexpected results.
39825eb7d56SwizFor example, the regular expression
39925eb7d56Swiz.Em b*
40025eb7d56Swizmatches the beginning of the string
40125eb7d56Swiz.Em abbb
40225eb7d56Swiz(as opposed to the substring
40325eb7d56Swiz.Em bbb ) ,
40425eb7d56Swizsince a null match is the only leftmost match.
40525eb7d56Swiz.Sm off
40670ea994eSjoerg.It Em \e{ No n,m Em \e}\ \e{ No n, Em \e}\ \& Em \e{ No n Em \e}
40725eb7d56Swiz.Sm on
40820d73299SalmMatches the single character regular expression or subexpression
40920d73299Salmimmediately preceding it at least
41025eb7d56Swiz.Em n
41120d73299Salmand at most
41225eb7d56Swiz.Em m
41320d73299Salmtimes.
41420d73299SalmIf
41525eb7d56Swiz.Em m
41620d73299Salmis omitted, then it matches at least
41725eb7d56Swiz.Em n
41820d73299Salmtimes.
41920d73299SalmIf the comma is also omitted, then it matches exactly
42025eb7d56Swiz.Em n
42120d73299Salmtimes.
42225eb7d56Swiz.El
42325eb7d56Swiz.Pp
424f7f6e6ffSalmAdditional regular expression operators may be defined depending on the
425f7f6e6ffSalmparticular
42625eb7d56Swiz.Xr regex 3
427f7f6e6ffSalmimplementation.
42825eb7d56Swiz.Ss CHARACTER CLASSES
429051f8737SwizA character class specifies a set of characters.
430051f8737SwizIt is written within square brackets
43125eb7d56Swiz.Pq []
43225eb7d56Swizand in its most basic form contains just the characters in the set.
43325eb7d56Swiz.Pp
43425eb7d56SwizTo include a
43525eb7d56Swiz.Sq \&]
43625eb7d56Swizin a character class, it must be the first character.
43725eb7d56SwizA range of characters may be specified by separating the end characters
43825eb7d56Swizof the range with a
43925eb7d56Swiz.Sq \&- ,
44025eb7d56Swize.g.,
44125eb7d56Swiz.Sq a-z
44225eb7d56Swizspecifies the lower case characters.
44325eb7d56Swiz.Pp
44425eb7d56SwizThe following literals can also be used within character classes as
44525eb7d56Swizshorthand for particular sets of characters:
44625eb7d56Swiz.Bl -tag -offset indent -compact -width [:blahblah:]
44725eb7d56Swiz.It [:alnum:]
44825eb7d56SwizAlphanumeric characters.
44925eb7d56Swiz.It [:cntrl:]
45025eb7d56SwizControl characters.
45125eb7d56Swiz.It [:lower:]
45225eb7d56SwizLowercase alphabetic characters.
45325eb7d56Swiz.It [:space:]
45425eb7d56SwizWhitespace (space, tab, newline, form feed, etc.)
45525eb7d56Swiz.It [:alpha:]
45625eb7d56SwizAlphabetic characters.
45725eb7d56Swiz.It [:digit:]
45825eb7d56SwizNumeric characters (digits).
45925eb7d56Swiz.It [:print:]
46025eb7d56SwizPrintable characters.
46125eb7d56Swiz.It [:upper:]
46225eb7d56SwizUppercase alphabetic characters.
46325eb7d56Swiz.It [:blank:]
46425eb7d56SwizBlank characters (space and tab).
46525eb7d56Swiz.It [:graph:]
46625eb7d56SwizGraphical characters (printing nonblank characters).
46725eb7d56Swiz.It [:punct:]
46825eb7d56SwizPunctuation characters.
46925eb7d56Swiz.It [:xdigit:]
47025eb7d56SwizHexadecimal digits.
47125eb7d56Swiz.El
47225eb7d56SwizIf
47325eb7d56Swiz.Sq \&-
47425eb7d56Swizappears as the first or last character of a character class, then
47525eb7d56Swizit matches itself.
47625eb7d56SwizAll other characters in a character class match themselves.
47725eb7d56Swiz.Pp
47825eb7d56SwizPatterns in
47925eb7d56Swiza character class
48025eb7d56Swizof the form
48125eb7d56Swiz.Em [.col-elm.]
48225eb7d56Swizor
48325eb7d56Swiz.Em [=col-elm=]
48425eb7d56Swizwhere
48525eb7d56Swiz.Em col-elm
48625eb7d56Swizis a
48725eb7d56Swiz.Em collating element
488051f8737Swizare interpreted according to the locale
489051f8737Swiz.\" .Xr locale 5
49025eb7d56Swiz(not currently supported).
49125eb7d56SwizSee
49225eb7d56Swiz.Xr regex 3
49325eb7d56Swizfor an explanation of these constructs.
49425eb7d56Swiz.Ss COMMANDS
49520d73299SalmAll
49625eb7d56Swiz.Nm
497881c2581Swizcommands are single characters, though some require additional parameters.
498f7f6e6ffSalmIf a command's parameters extend over several lines, then
49925eb7d56Swizeach line except for the last must be terminated with a backslash
50025eb7d56Swiz.Pq Ql \e .
50125eb7d56Swiz.Pp
50220d73299SalmIn general, at most one command is allowed per line.
50320d73299SalmHowever, most commands accept a print suffix, which is any of
50425eb7d56Swiz.Ic p
50520d73299Salm(print),
50625eb7d56Swiz.Ic l
50720d73299Salm(list),
50820d73299Salmor
50925eb7d56Swiz.Ic n
51025eb7d56Swiz(enumerate), to print the last line affected by the command.
51125eb7d56Swiz.Pp
51220d73299SalmAn interrupt (typically ^C) has the effect of aborting the current command
51320d73299Salmand returning the editor to command mode.
51425eb7d56Swiz.Pp
51525eb7d56Swiz.Nm
51614dfaa4bSwizrecognizes the following commands.
51714dfaa4bSwizThe commands are shown together with
51820d73299Salmthe default address or address range supplied if none is
51925eb7d56Swizspecified (in parentheses), and other possible arguments on the right.
52025eb7d56Swiz.Bl -tag -width Dxxs
52125eb7d56Swiz.It (.) Ns Ic a
52220d73299SalmAppends text to the buffer after the addressed line.
52320d73299SalmText is entered in input mode.
52420d73299SalmThe current address is set to last line entered.
52525eb7d56Swiz.It (.,.) Ns Ic c
52614dfaa4bSwizChanges lines in the buffer.
52725eb7d56SwizThe addressed lines are deleted from the buffer,
52825eb7d56Swizand text is appended in their place.
52920d73299SalmText is entered in input mode.
53020d73299SalmThe current address is set to last line entered.
53125eb7d56Swiz.It (.,.) Ns Ic d
53220d73299SalmDeletes the addressed lines from the buffer.
53320d73299SalmIf there is a line after the deleted range, then the current address is set
53414dfaa4bSwizto this line.
53525eb7d56SwizOtherwise the current address is set to the line before the deleted range.
53625eb7d56Swiz.It Ic e Ar file
53720d73299SalmEdits
53825eb7d56Swiz.Ar file ,
53920d73299Salmand sets the default filename.
54020d73299SalmIf
54125eb7d56Swiz.Ar file
54220d73299Salmis not specified, then the default filename is used.
54314dfaa4bSwizAny lines in the buffer are deleted before the new file is read.
54420d73299SalmThe current address is set to the last line read.
54525eb7d56Swiz.It Ic e Ar !command
54620d73299SalmEdits the standard output of
54725eb7d56Swiz.Ar command ,
5481357f155Salm(see
54970ea994eSjoerg.Ic \&! Ar command
5501357f155Salmbelow).
55120d73299SalmThe default filename is unchanged.
55220d73299SalmAny lines in the buffer are deleted before the output of
55325eb7d56Swiz.Em command
55420d73299Salmis read.
55520d73299SalmThe current address is set to the last line read.
55625eb7d56Swiz.It Ic E Ar file
55720d73299SalmEdits
55825eb7d56Swiz.Ar file
55920d73299Salmunconditionally.
56020d73299SalmThis is similar to the
56125eb7d56Swiz.Ic e
56214dfaa4bSwizcommand, except that unwritten changes are discarded without warning.
56320d73299SalmThe current address is set to the last line read.
56425eb7d56Swiz.It Ic f Ar file
56520d73299SalmSets the default filename to
56625eb7d56Swiz.Ar file .
56720d73299SalmIf
56825eb7d56Swiz.Ar file
56920d73299Salmis not specified, then the default unescaped filename is printed.
57025eb7d56Swiz.It (1,$) Ns Ic g Ns Ar /re/command-list
57120d73299SalmApplies
57225eb7d56Swiz.Ar command-list
57320d73299Salmto each of the addressed lines matching a regular expression
57425eb7d56Swiz.Ar re .
57514dfaa4bSwizThe current address is set to the line currently matched before
57625eb7d56Swiz.Ar command-list
57720d73299Salmis executed.
57820d73299SalmAt the end of the
57925eb7d56Swiz.Ic g
58020d73299Salmcommand, the current address is set to the last line affected by
58125eb7d56Swiz.Ar command-list .
58225eb7d56Swiz.Pp
58320d73299SalmEach command in
58425eb7d56Swiz.Ar command-list
58520d73299Salmmust be on a separate line,
58620d73299Salmand every line except for the last must be terminated by a backslash
58725eb7d56Swiz.Pq Sq \e .
58820d73299SalmAny commands are allowed, except for
58925eb7d56Swiz.Ic g ,
59025eb7d56Swiz.Ic G ,
59125eb7d56Swiz.Ic v ,
59220d73299Salmand
59325eb7d56Swiz.Ic V .
594eca46021SalmA newline alone in
59525eb7d56Swiz.Ar command-list
596eca46021Salmis equivalent to a
59725eb7d56Swiz.Ic p
598eca46021Salmcommand.
59925eb7d56Swiz.It (1,$) Ns Ic G Ns Ar /re/
60020d73299SalmInteractively edits the addressed lines matching a regular expression
60125eb7d56Swiz.Ar re .
60225eb7d56SwizFor each matching line, the line is printed, the current address is set,
603eca46021Salmand the user is prompted to enter a
60425eb7d56Swiz.Ar command-list .
60520d73299SalmAt the end of the
60625eb7d56Swiz.Ic G
60725eb7d56Swizcommand, the current address is set to the last line affected by (the last)
60825eb7d56Swiz.Ar command-list .
60925eb7d56Swiz.Pp
610eca46021SalmThe format of
61125eb7d56Swiz.Ar command-list
612eca46021Salmis the same as that of the
61325eb7d56Swiz.Ic g
61414dfaa4bSwizcommand.
61514dfaa4bSwizA newline alone acts as a null command list.
61625eb7d56SwizA single
61701869ca4Swiz.Sq &
61825eb7d56Swizrepeats the last non-null command list.
61925eb7d56Swiz.It Ic H
62020d73299SalmToggles the printing of error explanations.
62120d73299SalmBy default, explanations are not printed.
62225eb7d56SwizIt is recommended that
62325eb7d56Swiz.Nm
62425eb7d56Swizscripts begin with this command to aid in debugging.
62525eb7d56Swiz.It Ic h
62620d73299SalmPrints an explanation of the last error.
62725eb7d56Swiz.It (.) Ns Ic i
62820d73299SalmInserts text in the buffer before the current line.
62920d73299SalmText is entered in input mode.
63020d73299SalmThe current address is set to the last line entered.
63125eb7d56Swiz.It (.,.+1) Ns Ic j
63214dfaa4bSwizJoins the addressed lines.
63314dfaa4bSwizThe addressed lines are deleted from the buffer and replaced by a single
63420d73299Salmline containing their joined text.
63520d73299SalmThe current address is set to the resultant line.
63625eb7d56Swiz.It (.) Ns Ic k Ns Ar lc
63720d73299SalmMarks a line with a lower case letter
63825eb7d56Swiz.Ar lc .
63920d73299SalmThe line can then be addressed as
64025eb7d56Swiz.Ar \&'lc
64120d73299Salm(i.e., a single quote followed by
64225eb7d56Swiz.Ar lc )
64325eb7d56Swizin subsequent commands.
64414dfaa4bSwizThe mark is not cleared until the line is deleted or otherwise modified.
64525eb7d56Swiz.It (.,.) Ns Ic l
64620d73299SalmPrints the addressed lines unambiguously.
64725eb7d56SwizIf a single line fills more than one screen (as might be the case
64825eb7d56Swizwhen viewing a binary file, for instance), a
64925eb7d56Swiz.Dq --More--
6501357f155Salmprompt is printed on the last line.
65125eb7d56Swiz.Nm
65214dfaa4bSwizwaits until the RETURN key is pressed before displaying the next screen.
65314dfaa4bSwizThe current address is set to the last line printed.
65425eb7d56Swiz.It (.,.) Ns Ic m Ns No (.)
65514dfaa4bSwizMoves lines in the buffer.
65614dfaa4bSwizThe addressed lines are moved to after the
65720d73299Salmright-hand destination address, which may be the address
65825eb7d56Swiz.Em 0
65920d73299Salm(zero).
66014dfaa4bSwizThe current address is set to the last line moved.
66125eb7d56Swiz.It (.,.) Ns Ic n
66225eb7d56SwizPrints the addressed lines along with their line numbers.
66314dfaa4bSwizThe current address is set to the last line printed.
66425eb7d56Swiz.It (.,.) Ns Ic p
66514dfaa4bSwizPrints the addressed lines.
66614dfaa4bSwizThe current address is set to the last line printed.
66725eb7d56Swiz.It Ic P
66820d73299SalmToggles the command prompt on and off.
66925eb7d56SwizUnless a prompt was specified with the command-line option
67025eb7d56Swiz.Fl p Ar string ,
67125eb7d56Swizthe command prompt is by default turned off.
67225eb7d56Swiz.It Ic q
67325eb7d56SwizQuits
67425eb7d56Swiz.Nm .
67525eb7d56Swiz.It Ic Q
67625eb7d56SwizQuits
67725eb7d56Swiz.Nm
67825eb7d56Swizunconditionally.
67920d73299SalmThis is similar to the
68025eb7d56Swiz.Ic q
68114dfaa4bSwizcommand, except that unwritten changes are discarded without warning.
68225eb7d56Swiz.It ($) Ns Ic r Ar file
68320d73299SalmReads
68425eb7d56Swiz.Ar file
68514dfaa4bSwizto after the addressed line.
68614dfaa4bSwizIf
68725eb7d56Swiz.Ar file
68825eb7d56Swizis not specified, then the default filename is used.
68914dfaa4bSwizIf there was no default filename prior to the command,
69020d73299Salmthen the default filename is set to
69125eb7d56Swiz.Ar file .
69220d73299SalmOtherwise, the default filename is unchanged.
69320d73299SalmThe current address is set to the last line read.
69425eb7d56Swiz.It ($) Ns Ic r Ar !command
69514dfaa4bSwizReads to after the addressed line the standard output of
69625eb7d56Swiz.Ar command ,
6971357f155Salm(see the
69870ea994eSjoerg.Ic \&!
69925eb7d56Swizcommand below).
70020d73299SalmThe default filename is unchanged.
70120d73299SalmThe current address is set to the last line read.
70225eb7d56Swiz.Sm off
70370ea994eSjoerg.It (.,.) Ic s Ar /re/replacement/ , \ (.,.) \
70470ea994eSjoergIc s Ar /re/replacement/ Em g , Ar \ (.,.) \
70570ea994eSjoergIc s Ar /re/replacement/ Em n
70625eb7d56Swiz.Sm on
70714dfaa4bSwizReplaces text in the addressed lines matching a regular expression
70825eb7d56Swiz.Ar re
70920d73299Salmwith
71025eb7d56Swiz.Ar replacement .
71120d73299SalmBy default, only the first match in each line is replaced.
7121357f155SalmIf the
71325eb7d56Swiz.Em g
7141357f155Salm(global) suffix is given, then every match to be replaced.
71520d73299SalmThe
71625eb7d56Swiz.Em n
71720d73299Salmsuffix, where
71825eb7d56Swiz.Em n
719881c2581Swizis a positive number, causes only the
72025eb7d56Swiz.Em n Ns No th
72120d73299Salmmatch to be replaced.
722eca46021SalmIt is an error if no substitutions are performed on any of the addressed
723eca46021Salmlines.
72420d73299SalmThe current address is set the last line affected.
72525eb7d56Swiz.Pp
72625eb7d56Swiz.Ar re
727f7f6e6ffSalmand
72825eb7d56Swiz.Ar replacement
7291357f155Salmmay be delimited by any character other than space and newline
7301357f155Salm(see the
73125eb7d56Swiz.Ic s
7321357f155Salmcommand below).
733f7f6e6ffSalmIf one or two of the last delimiters is omitted, then the last line
734f7f6e6ffSalmaffected is printed as though the print suffix
73525eb7d56Swiz.Em p
736f7f6e6ffSalmwere specified.
73725eb7d56Swiz.Pp
73825eb7d56SwizAn unescaped
73901869ca4Swiz.Ql &
74025eb7d56Swizin
74125eb7d56Swiz.Ar replacement
74220d73299Salmis replaced by the currently matched text.
74320d73299SalmThe character sequence
74425eb7d56Swiz.Em \em ,
74520d73299Salmwhere
74625eb7d56Swiz.Em m
74720d73299Salmis a number in the range [1,9], is replaced by the
74825eb7d56Swiz.Em m Ns No th
74920d73299Salmbackreference expression of the matched text.
75020d73299SalmIf
75125eb7d56Swiz.Ar replacement
75225eb7d56Swizconsists of a single
75325eb7d56Swiz.Ql % ,
75425eb7d56Swizthen
75525eb7d56Swiz.Ar replacement
75620d73299Salmfrom the last substitution is used.
757eca46021SalmNewlines may be embedded in
75825eb7d56Swiz.Ar replacement
75925eb7d56Swizif they are escaped with a backslash
76025eb7d56Swiz.Pq Ql \e .
76125eb7d56Swiz.It (.,.) Ns Ic s
76220d73299SalmRepeats the last substitution.
76320d73299SalmThis form of the
76425eb7d56Swiz.Ic s
7651357f155Salmcommand accepts a count suffix
76625eb7d56Swiz.Em n ,
7671357f155Salmor any combination of the characters
76825eb7d56Swiz.Em r ,
76925eb7d56Swiz.Em g ,
77020d73299Salmand
77125eb7d56Swiz.Em p .
7721357f155SalmIf a count suffix
77325eb7d56Swiz.Em n
7741357f155Salmis given, then only the
77525eb7d56Swiz.Em n Ns No th
7761357f155Salmmatch is replaced.
77720d73299SalmThe
77825eb7d56Swiz.Em r
77920d73299Salmsuffix causes
780dcc13ddbSwizthe regular expression of the last search to be used instead of
78120d73299Salmthat of the last substitution.
78220d73299SalmThe
78325eb7d56Swiz.Em g
78420d73299Salmsuffix toggles the global suffix of the last substitution.
78520d73299SalmThe
78625eb7d56Swiz.Em p
787dcc13ddbSwizsuffix toggles the print suffix of the last substitution.
78820d73299SalmThe current address is set to the last line affected.
78925eb7d56Swiz.It (.,.) Ns Ic t Ns No (.)
79020d73299SalmCopies (i.e., transfers) the addressed lines to after the right-hand
79120d73299Salmdestination address, which may be the address
79225eb7d56Swiz.Em 0
79320d73299Salm(zero).
79414dfaa4bSwizThe current address is set to the last line copied.
79525eb7d56Swiz.It Ic u
79617714ca3SalmUndoes the last command and restores the current address
79717714ca3Salmto what it was before the command.
79820d73299SalmThe global commands
79925eb7d56Swiz.Ic g ,
80025eb7d56Swiz.Ic G ,
80125eb7d56Swiz.Ic v ,
80220d73299Salmand
80325eb7d56Swiz.Ic V
80420d73299Salmare treated as a single command by undo.
80525eb7d56Swiz.Ic u
80620d73299Salmis its own inverse.
80725eb7d56Swiz.It (1,$) Ns Ic v Ns Ar /re/command-list
80820d73299SalmApplies
80925eb7d56Swiz.Ar command-list
81020d73299Salmto each of the addressed lines not matching a regular expression
81125eb7d56Swiz.Ar re .
81220d73299SalmThis is similar to the
81325eb7d56Swiz.Ic g
81420d73299Salmcommand.
81525eb7d56Swiz.It (1,$) Ns Ic V Ns Ar /re/
81620d73299SalmInteractively edits the addressed lines not matching a regular expression
81725eb7d56Swiz.Ar re .
81820d73299SalmThis is similar to the
81925eb7d56Swiz.Ic G
82020d73299Salmcommand.
82125eb7d56Swiz.It (1,$) Ns Ic w Ar file
82220d73299SalmWrites the addressed lines to
82325eb7d56Swiz.Ar file .
82420d73299SalmAny previous contents of
82525eb7d56Swiz.Ar file
826dcc13ddbSwizare lost without warning.
82720d73299SalmIf there is no default filename, then the default filename is set to
82825eb7d56Swiz.Ar file ,
82914dfaa4bSwizotherwise it is unchanged.
83014dfaa4bSwizIf no filename is specified, then the default filename is used.
83120d73299SalmThe current address is unchanged.
83225eb7d56Swiz.It (1,$) Ns Ic wq Ar file
83320d73299SalmWrites the addressed lines to
83425eb7d56Swiz.Ar file ,
83520d73299Salmand then executes a
83625eb7d56Swiz.Ic q
83720d73299Salmcommand.
83825eb7d56Swiz.It (1,$) Ns Ic w Ar !command
83920d73299SalmWrites the addressed lines to the standard input of
84025eb7d56Swiz.Ar command ,
8411357f155Salm(see the
84270ea994eSjoerg.Ic \&!
84325eb7d56Swizcommand below).
84420d73299SalmThe default filename and current address are unchanged.
84525eb7d56Swiz.It (1,$) Ns Ic W Ar file
84620d73299SalmAppends the addressed lines to the end of
84725eb7d56Swiz.Ar file .
84820d73299SalmThis is similar to the
84925eb7d56Swiz.Ic w
85000423178Swizcommand, except that the previous contents of file are not clobbered.
85120d73299SalmThe current address is unchanged.
85225eb7d56Swiz.It Ic x
85314dfaa4bSwizPrompts for an encryption key which is used in subsequent reads and writes.
85414dfaa4bSwizIf a newline alone is entered as the key, then encryption is turned off.
85514dfaa4bSwizOtherwise, echoing is disabled while a key is read.
8568ea171b9SlukemEncryption/decryption is done using the
85725eb7d56Swiz.Xr bdes 1
8588ea171b9Slukemalgorithm.
85925eb7d56Swiz.It (.+1) Ns Ic z Ns Ar n
86020d73299SalmScrolls
86125eb7d56Swiz.Ar n
86214dfaa4bSwizlines at a time starting at addressed line.
86314dfaa4bSwizIf
86425eb7d56Swiz.Ar n
86520d73299Salmis not specified, then the current window size is used.
86620d73299SalmThe current address is set to the last line printed.
86725eb7d56Swiz.It ($) Ns Ic =
86825eb7d56SwizPrints the line number of the addressed line.
86925eb7d56Swiz.It (.+1) Ns Ic newline
87025eb7d56SwizPrints the addressed line, and sets the current address to that line.
87170ea994eSjoerg.It Ic \&! Ns Ar command
8722621a68cSalmExecutes
87325eb7d56Swiz.Ar command
87420d73299Salmvia
87525eb7d56Swiz.Xr sh 1 .
87620d73299SalmIf the first character of
87725eb7d56Swiz.Ar command
87825eb7d56Swizis
87970ea994eSjoerg.Ic \&! ,
88025eb7d56Swizthen it is replaced by text of the previous
88125eb7d56Swiz.Ic !command .
88225eb7d56Swiz.Nm
88320d73299Salmdoes not process
88425eb7d56Swiz.Ar command
88525eb7d56Swizfor
88625eb7d56Swiz.Sq \e
88725eb7d56Swiz(backslash) escapes.
88820d73299SalmHowever, an unescaped
88925eb7d56Swiz.Sq %
89020d73299Salmis replaced by the default filename.
89125eb7d56SwizWhen the shell returns from execution, a
892fd71eb70Sjoerg.Sq \&!
89320d73299Salmis printed to the standard output.
89420d73299SalmThe current line is unchanged.
89525eb7d56Swiz.El
89625eb7d56Swiz.Sh LIMITATIONS
89725eb7d56Swiz.Nm
89820d73299Salmprocesses
89925eb7d56Swiz.Em file
90020d73299Salmarguments for backslash escapes, i.e., in a filename,
90125eb7d56Swizany characters preceded by a backslash
90225eb7d56Swiz.Pq Ql \e
90325eb7d56Swizare interpreted literally.
90425eb7d56Swiz.Pp
9052621a68cSalmIf a text (non-binary) file is not terminated by a newline character,
9062621a68cSalmthen
90725eb7d56Swiz.Nm
90814dfaa4bSwizappends one on reading/writing it.
90914dfaa4bSwizIn the case of a binary file,
91025eb7d56Swiz.Nm
911eca46021Salmdoes not append a newline on reading/writing.
9127554e599Swiz.Sh ENVIRONMENT
9137554e599Swiz.Bl -tag -width iTMPDIR
9147554e599Swiz.It Ev TMPDIR
9157554e599SwizThe location used to store temporary files.
9167554e599Swiz.El
91725eb7d56Swiz.Sh FILES
91825eb7d56Swiz.Bl -tag -width /tmp/ed.* -compact
91925eb7d56Swiz.It Pa /tmp/ed.*
92025eb7d56Swizbuffer file
92125eb7d56Swiz.It Pa ed.hup
92225eb7d56Swizwhere
92325eb7d56Swiz.Nm
92425eb7d56Swizattempts to write the buffer if the terminal hangs up
92525eb7d56Swiz.El
92625eb7d56Swiz.Sh DIAGNOSTICS
92720d73299SalmWhen an error occurs,
92825eb7d56Swiz.Nm
92925eb7d56Swizprints a
930fd71eb70Sjoerg.Dq \&?
93125eb7d56Swizand either returns to command mode or exits if its input is from a script.
93214dfaa4bSwizAn explanation of the last error can be printed with the
93325eb7d56Swiz.Ic h
934eca46021Salm(help) command.
93525eb7d56Swiz.Pp
936eca46021SalmSince the
93725eb7d56Swiz.Ic g
938eca46021Salm(global) command masks any errors from failed searches and substitutions,
939eca46021Salmit can be used to perform conditional operations in scripts; e.g.,
94025eb7d56Swiz.Bd -literal -offset indent
94125eb7d56Swizg/old/s//new/
94225eb7d56Swiz.Ed
94325eb7d56Swiz.Pp
944eca46021Salmreplaces any occurrences of
94525eb7d56Swiz.Em old
946eca46021Salmwith
94725eb7d56Swiz.Em new .
94825eb7d56Swiz.Pp
949aef94df1SalmIf the
95025eb7d56Swiz.Ic u
951aef94df1Salm(undo) command occurs in a global command list, then
952aef94df1Salmthe command list is executed only once.
95325eb7d56Swiz.Pp
954eca46021SalmIf diagnostics are not disabled, attempting to quit
95525eb7d56Swiz.Nm
95614dfaa4bSwizor edit another file before writing a modified buffer results in an error.
95720d73299SalmIf the command is entered a second time, it succeeds,
9582621a68cSalmbut any changes to the buffer are lost.
95925eb7d56Swiz.Sh SEE ALSO
96025eb7d56Swiz.Xr bdes 1 ,
961051f8737Swiz.Xr patch 1 ,
96225eb7d56Swiz.Xr sed 1 ,
96325eb7d56Swiz.Xr sh 1 ,
96425eb7d56Swiz.Xr vi 1 ,
96525eb7d56Swiz.Xr regex 3
96625eb7d56Swiz.Pp
96725eb7d56SwizUSD:09-10
96825eb7d56Swiz.Rs
96925eb7d56Swiz.%A B. W. Kernighan
97025eb7d56Swiz.%A P. J. Plauger
97125eb7d56Swiz.%B Software Tools in Pascal
97225eb7d56Swiz.%I Addison-Wesley
97325eb7d56Swiz.%D 1981
97425eb7d56Swiz.Re
97525eb7d56Swiz.Sh HISTORY
97625eb7d56SwizAn
97725eb7d56Swiz.Nm
97825eb7d56Swizcommand appeared in
97925eb7d56Swiz.At v1 .
980