Move everything from /var/adm to /var/log
[unleashed.git] / share / man / man1 / ed.1
blobd1c7d2e810204711ea1868164530c37087ee09ba
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2003, Sun Microsystems, Inc.
46 .\"
47 .TH ED 1 "Oct 25, 2017"
48 .SH NAME
49 ed, red \- text editor
50 .SH SYNOPSIS
51 .LP
52 .nf
53 \fB/usr/bin/ed\fR [\fB-s\fR | \fB-\fR] [\fB-p\fR \fIstring\fR] [\fB-x\fR] [\fB-C\fR] [\fIfile\fR]
54 .fi
56 .LP
57 .nf
58 \fB/usr/bin/red\fR [\fB-s\fR | \fB-\fR] [\fB-p\fR \fIstring\fR] [\fB-x\fR] [\fB-C\fR] [\fIfile\fR]
59 .fi
61 .SH DESCRIPTION
62 .sp
63 .LP
64 The \fBed\fR utility is the standard text editor. If \fIfile\fR is specified,
65 \fBed\fR simulates an \fBe\fR command (see below) on the named file. That is,
66 the file is read into \fBed\fR's buffer so that it can be edited.
67 .sp
68 .LP
69 The \fBed\fR utility operates on a copy of the file it is editing. Changes made
70 to the copy have no effect on the file until a \fBw\fR (write) command is
71 given. The copy of the text being edited resides in a temporary file called the
72 \fIbuffer\fR. There is only one buffer.
73 .sp
74 .LP
75 The \fBred\fR utility is a restricted version of \fBed\fR. It will only allow
76 editing of files in the current directory. \fBred\fR prohibits executing shell
77 commands via \fB!\fR\fIshell command\fR. Attempts to bypass these restrictions
78 result in an error message (\fIrestricted shell\fR).
79 .sp
80 .LP
81 Both \fBed\fR and \fBred\fR support the \fBfspec\fR(4) formatting capability.
82 The default terminal mode is either \fBstty\fR \fB-tabs\fR or \fBstty tab3\fR,
83 where tab stops are set at eight columns (see \fBstty\fR(1)). If, however, the
84 first line of \fIfile\fR contains a format specification, that specification
85 will override the default mode. For example, tab stops would be set at 5, 10,
86 and 15, and a maximum line length of 72 would be imposed if the first line of
87 \fIfile\fR contains
88 .sp
89 .in +2
90 .nf
91 \fB<:t5,10,15 s72:>\fR
92 .fi
93 .in -2
94 .sp
96 .sp
97 .LP
98 Commands to \fBed\fR have a simple and regular structure: zero, one, or two
99 \fIaddresses\fR followed by a single-character \fIcommand\fR, possibly followed
100 by parameters to that command. These addresses specify one or more lines in the
101 buffer. Every command that requires addresses has default addresses, so that
102 the addresses can very often be omitted.
105 In general, only one command may appear on a line. Certain commands allow the
106 input of text. This text is placed in the appropriate place in the buffer.
107 While \fBed\fR is accepting text, it is said to be in \fIinput mode\fR. In this
108 mode, \fBno\fR commands are recognized; all input is merely collected. Leave
109 input mode by typing a period (\fB\&.\fR) at the beginning of a line, followed
110 immediately by a carriage return.
113 If \fBed\fR executes commands with arguments, it uses the default shell
114 \fB/usr/bin/sh\fR (see \fBsh\fR(1)).
115 .SS "Regular Expressions"
118 The \fBed\fR utility supports a limited form of \fIregular expression\fR
119 notation. Regular expressions are used in addresses to specify lines and in
120 some commands (for example, \fBs\fR) to specify portions of a line that are to
121 be substituted. To understand addressing in \fBed\fR, it is necessary to know
122 that at any time there is a \fIcurrent line\fR. Generally speaking, the current
123 line is the last line affected by a command. The exact effect on the current
124 line is discussed under the description of each command.
127 Internationalized Basic Regular Expressions are used for all system-supplied
128 locales. See \fBregex\fR(5).
129 .SS "ed Commands"
132 Commands may require zero, one, or two addresses. Commands that require no
133 addresses regard the presence of an address as an error. Commands that accept
134 one or two addresses assume default addresses when an insufficient number of
135 addresses is given; if more addresses are given than such a command requires,
136 the last one(s) are used.
139 Typically, addresses are separated from each other by a comma (\fB,\fR). They
140 may also be separated by a semicolon (\fB;\fR). In the latter case, the first
141 address is calculated, the current line (\fB\&.\fR) is set to that value, and
142 then the second address is calculated. This feature can be used to determine
143 the starting line for forward and backward searches (see Rules 5 and 6, above).
144 The second address of any two-address sequence must correspond to a line in the
145 buffer that follows the line corresponding to the first address.
146 The address can be omitted on either side of the comma or semicolon
147 separator, in which case the resulting address pairs are as follows:
152 box;
153 c | c
154 l | l .
155 Specified       Resulting
157 ,       1 , $
159 , addr  1 , addr
161 addr ,  addr , addr
163 ;       1 ; $
165 ; addr  1 ; addr
167 addr ;  addr ; addr
172 Any <\fIblank\fR>s included between addresses, address separators, or address
173 offsets are ignored.
176 In the following list of \fBed\fR commands, the parentheses shown prior to the
177 command are \fInot\fR part of the address. Rather, the parentheses show the
178 default address(es) for the command.
181 Each address component can be preceded by zero or more blank characters. The
182 command letter can be preceded by zero or more blank characters. If a suffix
183 letter (\fBl\fR, \fBn\fR, or \fBp\fR) is given, it must immediately follow the
184 command.
187 The \fBe\fR, \fBE\fR, \fBf\fR, \fBr\fR, and \fBw\fR commands take an optional
188 \fIfile\fR parameter, separated from the command letter by one or more blank
189 characters.
192 If changes have been made in the buffer since the last \fBw\fR command that
193 wrote the entire buffer, \fBed\fR warns the user if an attempt is made to
194 destroy the editor buffer via the \fBe\fR or \fBq\fR commands. The \fBed\fR
195 utility writes the string:
197 .in +2
199 "?\en"
201 .in -2
206 (followed by an explanatory message if \fIhelp mode\fR has been enabled via the
207 \fBH\fR command) to standard output and continues in command mode with the
208 current line number unchanged. If the \fBe\fR or \fBq\fR command is repeated
209 with no intervening command, \fBed\fR takes effect.
212 If an end-of-file is detected on standard input when a command is expected, the
213 \fBed\fR utility acts as if a \fBq\fR command had been entered.
216 It is generally illegal for more than one command to appear on a line. However,
217 any command (except \fBe\fR, \fBf\fR, \fBr\fR, or \fBw\fR) may be suffixed by
218 \fBl\fR, \fBn\fR, or \fBp\fR in which case the current line is either listed,
219 numbered or written, respectively, as discussed below under the \fBl\fR,
220 \fBn\fR, and \fBp\fR commands.
222 .ne 2
224 \fB\fB(.)a\fR\fR
228 \fB<\fItext\fR> \fR
232 \fB\fB\&.\fR\fR
234 .RS 11n
235 The \fBa\fRppend command accepts zero or more lines of text and appends it
236 after the addressed line in the buffer. The current line (\fB\&.\fR) is left at
237 the last inserted line, or, if there were none, at the addressed line. Address
238 0 is legal for this command: it causes the ``appended'' text to be placed at
239 the beginning of the buffer. The maximum number of characters that may be
240 entered from a terminal is 256 per line (including the new-line character).
244 .ne 2
246 \fB\fB(.,.)c\fR\fR
250 \fB<\fItext\fR>\fR
254 \fB\fB\&.\fR\fR
256 .RS 11n
257 The \fBc\fRhange command deletes the addressed lines from the buffer, then
258 accepts zero or more lines of text that replaces these lines in the buffer. The
259 current line (\fB\&.\fR) is left at the last line input, or, if there were
260 none, at the first line that was not deleted. If the lines deleted were
261 originally at the end of the buffer, the current line number will be set to the
262 address of the new last line. If no lines remain in the buffer, the current
263 line number will be set to 0. Address 0 is valid for this command. It is
264 interpreted as if the address 1 were specified.
270 .ne 2
272 \fB\fBC\fR\fR
274 .RS 11n
275 Same as the \fBX\fR command, described later, except that \fBed\fR assumes all
276 text read in for the \fBe\fR and \fBr\fR commands is encrypted unless a null
277 key is typed in.
281 .ne 2
283 \fB\fB(.,.)d\fR\fR
285 .RS 11n
286 The \fBd\fRelete command deletes the addressed lines from the buffer. The line
287 after the last line deleted becomes the current line. If the lines deleted were
288 originally at the end of the buffer, the new last line becomes the current
289 line. If no lines remain in the buffer, the current line number will be set to
294 .ne 2
296 \fB\fBe\fR \fIfile\fR\fR
298 .RS 11n
299 The \fBe\fRdit command deletes the entire contents of the buffer and then reads
300 the contents of \fIfile\fR into the buffer. The current line (\fB\&.\fR) is set
301 to the last line of the buffer. If \fIfile\fR is not given, the currently
302 remembered file name, if any, is used (see the \fBf\fR command). The number of
303 bytes read will be written to standard output, unless the \fB-s\fR option was
304 specified, in the following format:
306 \fB"%d\en"\fR <\fInumber of bytes read\fR>
308 \fIfile\fR is remembered for possible use as a default file name in subsequent
309 \fBe\fR, \fBE\fR, \fBr\fR, and \fBw\fR commands. If \fIfile\fR is replaced by
310 \fB!\fR, the rest of the line is taken to be a shell ( \fBsh\fR(1)) command
311 whose output is to be read. Such a shell command is \fInot\fR remembered as the
312 current file name.  See also DIAGNOSTICS below. All marks are discarded upon
313 the completion of a successful \fBe\fR command. If the buffer has changed since
314 the last time the entire buffer was written, the user is warned, as described
315 previously.
319 .ne 2
321 \fB\fBE\fR \fIfile\fR\fR
323 .RS 11n
324 The \fBE\fRdit command is like \fBe\fR, except that the editor does not check
325 to see if any changes have been made to the buffer since the last \fBw\fR
326 command.
330 .ne 2
332 \fB\fBf\fR \fIfile\fR\fR
334 .RS 11n
335 If \fIfile\fR is given, the \fBf\fR command changes the currently remembered
336 path name to \fIfile\fR. Whether the name is changed or not, the \fBf\fR
337 command then writes the (possibly new) currently remembered path name to the
338 standard output in the following format:
340 \fB"%s\en"\fR\fIpathname\fR
342 The current line number is unchanged.
346 .ne 2
348 \fB\fB(1,$)g/\fR\fIRE\fR\fB/\fR\fIcommand list\fR\fR
350 .RS 26n
351 In the \fBg\fRlobal command, the first step is to mark every line that matches
352 the given \fIRE\fR. Then, for every such line, the given \fIcommand list\fR is
353 executed with the current line (\fB\&.\fR) initially set to that line. When the
354 \fBg\fR command completes, the current line number has the value assigned by
355 the last command in the command list. If there were no matching lines, the
356 current line number is not changed. A single command or the first of a list of
357 commands appears on the same line as the global command. All lines of a
358 multi-line list except the last line must be ended with a backslash
359 (\fB\e\fR\|); \fBa\fR, \fBi\fR, and \fBc\fR commands and associated input are
360 permitted. The \fB\&.\fR terminating input mode may be omitted if it would be
361 the last line of the \fIcommand list\fR. An empty \fIcommand list\fR is
362 equivalent to the \fBp\fR command. The \fBg\fR, \fBG\fR, \fBv\fR, \fBV\fR, and
363 \fB!\fR commands are \fInot\fR permitted in the \fIcommand list\fR. See also
364 the NOTES and the last paragraph before FILES below. Any character other than
365 space or newline can be used instead of a slash to delimit the \fIRE\fR. Within
366 the \fIRE\fR, the \fIRE\fR delimiter itself can be used as a literal character
367 if it is preceded by a backslash.
371 .ne 2
373 \fB\fB(1,$)G/\fR\fIRE\fR\fB/\fR\fR
375 .RS 26n
376 In the interactive \fBG\fRlobal command, the first step is to mark every line
377 that matches the given \fIRE\fR. Then, for every such line, that line is
378 written to standard output, the current line (\fB\&.\fR) is changed to that
379 line, and any \fIone\fR command (other than one of the \fBa\fR, \fBc\fR,
380 \fBi\fR, \fBg\fR, \fBG\fR, \fBv\fR, and \fBV\fR commands) may be input and is
381 executed. After the execution of that command, the next marked line is written,
382 and so on. A new-line acts as a null command. An \fB&\fR causes the
383 re-execution of the most recent non-null command executed within the current
384 invocation of \fBG\fR. \fBNote:\fR  The commands input as part of the execution
385 of the \fBG\fR command may address and affect \fBany\fR lines in the buffer.
386 The final value of the current line number is the value set by the last command
387 successfully executed. (Notice that the last command successfully executed is
388 the \fBG\fR command itself if a command fails or the null command is
389 specified.) If there were no matching lines, the current line number is not
390 changed. The \fBG\fR command can be terminated by a \fBSIGINT\fR signal. The
391 \fBG\fR command can be terminated by an interrupt signal (ASCII DEL or BREAK).
392 Any character other than space or newline can be used instead of a slash to
393 delimit the \fIRE\fR. Within the \fIRE\fR, the \fIRE\fR delimiter itself can be
394 used as a literal character if it is preceded by a backslash.
398 .ne 2
400 \fB\fBh\fR\fR
402 .RS 26n
403 The \fBhelp\fR command gives a short error message that explains the reason for
404 the most recent \fB?\fR diagnostic. The current line number is unchanged.
408 .ne 2
410 \fB\fBH\fR\fR
412 .RS 26n
413 The \fBHelp\fR command causes \fBed\fR to enter a mode in which error messages
414 are written for all subsequent \fB?\fR diagnostics. It also explains the
415 previous \fB?\fR if there was one. The \fBH\fR command alternately turns this
416 mode on and off; it is initially off. The current line number is unchanged.
420 .ne 2
422 \fB\fB(.,.)i\fR\fR
426 \fB<\fItext\fR>\fR
430 \fB\fB\&.\fR\fR
432 .RS 26n
433 The \fBinsert\fR command accepts zero or more lines of text and inserts it
434 before the addressed line in the buffer. The current line (\fB\&.\fR) is left
435 at the last inserted line, or, if there were none, at the addressed line. This
436 command differs from the \fBa\fR command only in the placement of the input
437 text. The maximum number of characters that may be entered from a terminal is
438 256 per line (including the new-line character). Address 0 is valid for this
439 command. It is interpreted as if the address 1 were specified.
445 .ne 2
447 \fB\fB(.,.+1)j\fR\fR
449 .RS 26n
450 The \fBj\fRoin command joins contiguous lines by removing the appropriate
451 new-line characters. If exactly one address is given, this command does
452 nothing. If lines are joined, the current line number is set to the address of
453 the joined line. Otherwise, the current line number is unchanged.
457 .ne 2
459 \fB\fB(.)k\fR\fIx\fR\fR
461 .RS 26n
462 The mar\fBk\fR command marks the addressed line with name \fIx\fR, which must
463 be an ASCII lower-case letter (\fBa\fR\fB-\fR\fBz\fR). The address \fI a\'x\fR
464 then addresses this line. The current line (\fB\&.\fR) is unchanged.
468 .ne 2
470 \fB\fB(.,.)l\fR\fR
472 .RS 26n
473 The \fBl\fR command writes to standard output the addressed lines in a visually
474 unambiguous form. The characters ( \fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR,
475 \fB\er\fR, \fB\et\fR, \fB\ev\fR) are written as the corresponding escape
476 sequence. The \fB\en\fR in that table is not applicable. Non-printable
477 characters not in the table are written as one three-digit octal number (with a
478 preceding backslash character) for each byte in the character, with the most
479 significant byte first.
481 Long lines are folded, with the point of folding indicated by writing
482 backslash/newline character. The length at which folding occurs is unspecified,
483 but should be appropriate for the output device. The end of each line is marked
484 with a \fB$\fR. The end of each line is marked with a 
485 \fB$\fR due to folding, and \fB$\fR characters within the
486 text are written with a preceding backslash. An \fBl\fR command can be appended
487 to any other command other than \fBe\fR, \fBE\fR, \fBf\fR, \fBq\fR, \fBQ\fR,
488 \fBr\fR, \fBw\fR, or \fB!\fR. The current line number is set to the address of
489 the last line written.
493 .ne 2
495 \fB\fB(.,.)m\fR\fIa\fR\fR
497 .RS 26n
498 The \fBm\fRove command repositions the addressed line(s) after the line
499 addressed by \fIa\fR. Address 0 is legal for \fIa\fR and causes the addressed
500 line(s) to be moved to the beginning of the file. It is an error if address
501 \fIa\fR falls within the range of moved lines. The current line (\fB\&.\fR) is
502 left at the last line moved.
506 .ne 2
508 \fB\fB(.,.)n\fR\fR
510 .RS 26n
511 The \fBn\fRumber command writes the addressed lines, preceding each line by its
512 line number and a tab character. The current line (\fB\&.\fR) is left at the
513 last line written. The \fBn\fR command may be appended to any command other
514 than \fBe\fR, \fBE\fR, \fBf\fR, \fBq\fR, \fBQ\fR, \fBr\fR, \fBw\fR, or \fB!\fR.
518 .ne 2
520 \fB\fB(.,.)p\fR\fR
522 .RS 26n
523 The \fBp\fRrint command writes the addressed lines to standard output. The
524 current line (\fB\&.\fR) is left at the last line written. The \fBp\fR command
525 may be appended to any command other than \fBe\fR, \fBE\fR, \fBf\fR, \fBq\fR,
526 \fBQ\fR, \fBr\fR, \fBw\fR, or \fB!\fR. For example, \fBdp\fR deletes the
527 current line and writes the new current line.
531 .ne 2
533 \fB\fBP\fR\fR
535 .RS 26n
536 The \fBP\fR command causes \fBed\fR to prompt with an asterisk (\fB*\fR) (or
537 \fIstring\fR, if \fB-p\fR is specified) for all subsequent commands. The
538 \fBP\fR command alternatively turns this mode on and off; it is initially on if
539 the \fB-p\fR option is specified, otherwise off. The current line is unchanged.
543 .ne 2
545 \fB\fBq\fR\fR
547 .RS 26n
548 The \fBq\fRuit command causes \fBed\fR to exit. If the buffer has changed since
549 the last time the entire buffer was written, the user is warned. See
550 DIAGNOSTICS.
554 .ne 2
556 \fB\fBQ\fR\fR
558 .RS 26n
559 The editor exits without checking if changes have been made in the buffer since
560 the last \fBw\fR command.
564 .ne 2
566 \fB\fB($)r\fR \fIfile\fR\fR
568 .RS 26n
569 The \fBr\fRead command reads the contents of \fIfile\fR into the buffer. If
570 \fIfile\fR is not given, the currently remembered file name, if any, is used
571 (see the \fBe\fR and \fBf\fR commands). The currently remembered file name is
572 \fBnot\fR changed unless \fIfile\fR is the very first file name mentioned since
573 \fBed\fR was invoked. Address 0 is legal for \fBr\fR and causes the file to be
574 read in at the beginning of the buffer. If the read is successful and the
575 \fB-s\fR option was not specified, the number of characters read is written to
576 standard output in the following format:
578 .in +2
580 \fB%d\en\fR, <\fInumber of bytes read\fR>
582 .in -2
585 The current line (\fB\&.\fR) is set to the last line read. If \fIfile\fR is
586 replaced by \fB!\fR, the rest of the line is taken to be a shell command (see
587 \fBsh\fR(1)) whose output is to be read. For example, \fB$r !ls\fR appends the
588 current directory to the end of the file being edited. Such a shell command is
589 \fBnot\fR remembered as the current file name.
593 .ne 2
595 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/\fR\fR
599 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/\fR\fIcount\fR,
600 \fIcount\fR=[\fB1-2047\fR]\fR
604 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/g\fR\fR
608 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/l\fR\fR
612 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/n\fR\fR
616 \fB\fB(.,.)s/\fR\fIRE\fR\fB/\fR\fIreplacement\fR\fB/p\fR\fR
618 .sp .6
619 .RS 4n
620 The \fBs\fRubstitute command searches each addressed line for an occurrence of
621 the specified \fIRE\fR. Zero or more substitution commands can be specified. In
622 each line in which a match is found, all (non-overlapped) matched strings are
623 replaced by the \fIreplacement\fR if the global replacement indicator \fBg\fR
624 appears after the command. If the global indicator does not appear, only the
625 first occurrence of the matched string is replaced. If a number \fIcount\fR
626 appears after the command, only the \fIcount\fR-th occurrence of the matched
627 string on each addressed line is replaced. It is an error if the substitution
628 fails on \fBall\fR addressed lines. Any character other than space or new-line
629 may be used instead of the slash (\fB/\fR) to delimit the \fIRE\fR and the
630 \fIreplacement\fR. The current line (\fB\&.\fR) is left at the last line on
631 which a substitution occurred. Within the \fIRE\fR, the \fIRE\fR delimiter
632 itself can be used as a literal character if it is preceded by a backslash. See
633 also the last paragraph before FILES below.
635 An ampersand (\fB&\fR) appearing in the \fIreplacement\fR is replaced by the
636 string matching the \fIRE\fR on the current line. The special meaning of
637 \fB&\fR in this context may be suppressed by preceding it by \fB\e\fR\|. As a
638 more general feature, the characters \fB\e\fR\fIn\fR, where \fIn\fR is a digit,
639 are replaced by the text matched by the \fIn\fR-th regular subexpression of the
640 specified \fIRE\fR enclosed between \fB\e(\fR and \fB\e)\fR\&. When nested
641 parenthesized subexpressions are present, \fIn\fR is determined by counting
642 occurrences of \fB\e(\fR starting from the left. When the character \fB%\fR is
643 the only character in the \fIreplacement\fR, the \fIreplacement\fR used in the
644 most recent substitute command is used as the \fIreplacement\fR in the current
645 substitute command. If there was no previous substitute command, the use of
646 \fB%\fR in this manner is an error. The \fB%\fR loses its special meaning when
647 it is in a replacement string of more than one character or is preceded by a
648 \fB\e\fR\|. For each backslash (\e) encountered in scanning \fIreplacement\fR
649 from beginning to end, the following character loses its special meaning (if
650 any). It is unspecified what special meaning is given to any character other
651 than \fB&\fR, \fB\e\fR, \fB%\fR, or digits.
653 A line may be split by substituting a new-line character into it. The new-line
654 in the \fIreplacement\fR must be escaped by preceding it by \fB\e\fR\&. Such
655 substitution cannot be done as part of a \fBg\fR or \fBv\fR command list. The
656 current line number is set to the address of the last line on which a
657 substitution is performed. If no substitution is performed, the current line
658 number is unchanged. If a line is split, a substitution is considered to have
659 been performed on each of the new lines for the purpose of determining the new
660 current line number. A substitution is considered to have been performed even
661 if the replacement string is identical to the string that it replaces.
663 The substitute command supports the following indicators:
665 .ne 2
667 \fB\fIcount\fR\fR
669 .RS 9n
670 Substitute for the \fIcount\fRth occurrence only of the \fIRE\fR found on each
671 addressed line. \fIcount\fR must be between \fB1\fR-\fB2047\fR.
675 .ne 2
677 \fB\fBg\fR\fR
679 .RS 9n
680 Globally substitute for all non-overlapping instances of the \fIRE\fR rather
681 than just the first one. If both \fBg\fR and \fIcount\fR are specified, the
682 results are unspecified.
686 .ne 2
688 \fB\fBl\fR\fR
690 .RS 9n
691 Write to standard output the final line in which a substitution was made. The
692 line is written in the format specified for the \fBl\fR command.
696 .ne 2
698 \fB\fBn\fR\fR
700 .RS 9n
701 Write to standard output the final line in which a substitution was made. The
702 line is written in the format specified for the \fBn\fR command.
706 .ne 2
708 \fB\fBp\fR\fR
710 .RS 9n
711 Write to standard output the final line in which a substitution was made. The
712 line will be written in the format specified for the \fBp\fR command.
718 .ne 2
720 \fB\fB(.,.)t\fR\fIa\fR\fR
722 .sp .6
723 .RS 4n
724 This command acts just like the \fBm\fR command, except that a \fIcopy\fR of
725 the addressed lines is placed after address \fBa\fR (which may be 0). The
726 current line (\fB\&.\fR) is left at the last line copied.
730 .ne 2
732 \fB\fBu\fR\fR
734 .sp .6
735 .RS 4n
736 The \fBu\fRndo command nullifies the effect of the most recent command that
737 modified anything in the buffer, namely the most recent \fBa\fR, \fBc\fR,
738 \fBd\fR, \fBg\fR, \fBi\fR, \fBj\fR, \fBm\fR, \fBr\fR, \fBs\fR, \fBt\fR,
739 \fBu\fR, \fBv\fR, \fBG\fR, or \fBV\fR command. All changes made to the buffer
740 by a \fBg\fR, \fBG\fR, \fBv\fR, or \fBV\fR global command is undone as a single
741 change.If no changes were made by the global command (such as with \fBg/\fR
742 \fIRE\fR\fB/p\fR), the \fBu\fR command has no effect. The current line number
743 is set to the value it had  immediately before the  command being undone
744 started.
748 .ne 2
750 \fB\fB(1,$)v/\fR\fIRE\fR\fB/\fR\fIcommand list\fR\fR
752 .sp .6
753 .RS 4n
754 This command is the same as the global command \fBg\fR, except that the lines
755 marked during the first step are those that do \fBnot\fR match the \fIRE\fR.
759 .ne 2
761 \fB\fB(1,$)V/\fR\fIRE\fR\fB/\fR\fR
763 .sp .6
764 .RS 4n
765 This command is the same as the interactive global command \fBG\fR, except that
766 the lines that are marked during the first step are those that do \fBnot\fR
767 match the \fIRE\fR.
771 .ne 2
773 \fB\fB(1,$)w\fR \fIfile\fR\fR
775 .sp .6
776 .RS 4n
777 The \fBw\fRrite command writes the addressed lines into \fIfile\fR. If
778 \fIfile\fR does not exist, it is created with mode \fB666\fR (readable and
779 writable by everyone), unless your file creation mask dictates otherwise. See
780 the description of the \fBumask\fR special command on \fBsh\fR(1). The
781 currently remembered file name is \fBnot\fR changed unless \fIfile\fR is the
782 very first file name mentioned since \fBed\fR was invoked. If no file name is
783 given, the currently remembered file name, if any, is used (see the \fBe\fR and
784 \fBf\fR commands). The current line (\fB\&.\fR) is unchanged. If the command is
785 successful, the number of characters written is printed, unless the \fB-s\fR
786 option is specified in the following format:
788 .in +2
790 \fB"%d\en",\fR<\fInumber of bytes written\fR>
792 .in -2
795 If \fIfile\fR is replaced by \fB!\fR, the rest of the line is taken to be a
796 shell (see \fBsh\fR(1)) command whose standard input is the addressed lines.
797 Such a shell command is \fInot\fR remembered as the current path name. This
798 usage of the write command with \fB!\fR is to be considered as a ``last \fBw\fR
799 command that wrote the entire buffer''.
803 .ne 2
805 \fB\fB(1,$)W\fR \fIfile\fR\fR
807 .RS 19n
808 This command is the same as the \fBw\fRrite command above, except that it
809 appends the addressed lines to the end of \fIfile\fR if it exists. If
810 \fIfile\fR does not exist, it is created as described above for the \fBw\fR
811 command.
815 .ne 2
817 \fB\fBX\fR\fR
819 .RS 19n
820 An educated guess is made to determine whether text read for the \fBe\fR and
821 \fBr\fR commands is encrypted. A null key turns off encryption. Subsequent
822 \fBe\fR, \fBr\fR, and \fBw\fR commands will use this key to encrypt or decrypt
823 the text. An explicitly empty key turns off encryption. Also, see the \fB-x\fR
824 option of \fBed\fR.
828 .ne 2
830 \fB\fB($)=\fR\fR
832 .RS 19n
833 The line number of the addressed line is written to standard output in the
834 following format:
836 .in +2
838 \fB"%d\en"\fR<\fIline number\fR>
840 .in -2
843 The current line number is unchanged by this command.
847 .ne 2
849 \fB\fB!\fR\fIshell command\fR\fR
851 .RS 19n
852 The remainder of the line after the \fB!\fR is sent to the UNIX system shell
853 (see \fBsh\fR(1)) to be interpreted as a command. Within the text of that
854 command, the unescaped character \fB%\fR is replaced with the remembered file
855 name. If a \fB!\fR appears as the first character of the shell command, it is
856 replaced with the text of the previous shell command. Thus, \fB!!\fR repeats
857 the last shell command. If any replacements of \fB%\fR or \fB!\fR are
858 performed, the modified line is written to the standard output before
859 \fIcommand\fR is executed. The \fB!\fR command will write:
861 \fB"!\en"\fR
863 to standard output upon completion, unless the \fB-s\fR option is specified.
864 The current line number is unchanged.
868 .ne 2
870 \fB\fB(.+1)\fR<new-line>\fR
872 .RS 19n
873 An address alone on a line causes the addressed line to be written. A new-line
874 alone is equivalent to \fB\&.+1p\fR. It is useful for stepping forward through
875 the buffer. The current line number will be set to the address of the written
876 line.
881 If an interrupt signal (ASCII DEL or BREAK) is sent, \fBed\fR writes a
882 "\fB?\en\fR" and returns to \fBits\fR command level.
885 The \fBed\fR utility takes the standard action for all signals with the
886 following exceptions:
888 .ne 2
890 \fB\fBSIGINT\fR\fR
892 .RS 10n
893 The \fBed\fR utility interrupts its current activity, writes the string
894 "\fB?\en\fR" to standard output, and returns to command mode.
898 .ne 2
900 \fB\fBSIGHUP\fR\fR
902 .RS 10n
903 If the buffer is not empty and has changed since the last write, the \fBed\fR
904 utility attempts to write a copy of the buffer in a file. First, the file named
905 \fBed.hup\fR in the current directory is used. If that fails, the file named
906 \fBed.hup\fR in the directory named by the \fBHOME\fR environment variable is
907 used. In any case, the \fBed\fR utility exits without returning to command
908 mode.
913 Some size limitations are in effect: 512 characters in a line, 256 characters
914 in a global command list, and 255 characters in the path name of a file
915 (counting slashes). The limit on the number of lines depends on the amount of
916 user memory. Each line takes 1 word.
919 When reading a file, \fBed\fR discards \fBASCII\fR and \fBNUL\fR characters.
922 If a file is not terminated by a new-line character, \fBed\fR adds one and puts
923 out a message explaining what it did.
926 If the closing delimiter of an \fBRE\fR or of a replacement string (for
927 example, \fB/\fR) would be the last character before a new-line, that delimiter
928 may be omitted, in which case the addressed line is written. The following
929 pairs of commands are equivalent:
931 .ne 2
933 \fB\fBs/s1/s2\fR\fR
935 .RS 11n
936 \fBs/s1/s2/p\fR
940 .ne 2
942 \fB\fBg/s1\fR\fR
944 .RS 11n
945 \fBg/s1/p\fR
949 .ne 2
951 \fB\fB?s1\fR\fR
953 .RS 11n
954 \fB?s1?\fR
959 If an invalid command is entered, \fBed\fR writes the string:
962 \fB"?\en"\fR
965 (followed by an explanatory message if \fIhelp mode\fR has been enabled by the
966 \fBH\fR command) to standard output and continues in command mode with the
967 current line number unchanged.
968 .SH OPTIONS
970 .ne 2
972 \fB\fB-C\fR\fR
974 .RS 13n
975 Encryption option. The same as the \fB-x\fR option, except that \fBed\fR
976 simulates a \fBC\fR command. The \fBC\fR command is like the \fBX\fR command,
977 except that all text read in is assumed to have been encrypted.
981 .ne 2
983 \fB\fB\fR\fB-p\fR\fIstring\fR \fR
985 .RS 13n
986 Allows the user to specify a prompt string. By default, there is no prompt
987 string.
991 .ne 2
993 \fB\fB-s\fR |  \fB-;\fR\fR
995 .RS 13n
996 Suppresses the writing of character counts by \fBe\fR, \fBr\fR, and \fBw\fR
997 commands, of diagnostics from \fBe\fR and \fBq\fR commands, and of the \fB!\fR
998 prompt after a \fB!\fR\fIshell command\fR.
1002 .ne 2
1004 \fB\fB-x\fR\fR
1006 .RS 13n
1007 Encryption option. When \fB-x\fR is used, \fBed\fR simulates an \fBX\fR command
1008 and prompts the user for a key. The \fBX\fR command makes an educated guess to
1009 determine whether text read in is encrypted or not. The temporary buffer file
1010 is encrypted also, using a transformed version of the key typed in for the
1011 \fB-x\fR option. See NOTES.
1014 .SH OPERANDS
1017 The following operand is supported:
1019 .ne 2
1021 \fB\fIfile\fR\fR
1023 .RS 8n
1024 If \fIfile\fR is specified, \fBed\fR simulates an \fBe\fR command on the file
1025 named by the path name \fIfile\fR before accepting commands from the standard
1026 input.
1029 .SH ENVIRONMENT VARIABLES
1032 See \fBenviron\fR(5) for descriptions of the following environment variables
1033 that affect the execution of \fBed\fR: \fBHOME\fR, \fBLANG\fR, \fBLC_ALL\fR,
1034 \fBLC_CTYPE\fR, \fBLC_COLLATE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
1035 .SH EXIT STATUS
1038 The following exit values are returned:
1040 .ne 2
1042 \fB\fB0\fR\fR
1044 .RS 6n
1045 Successful completion without any file or command errors.
1049 .ne 2
1051 \fB\fB>0\fR\fR
1053 .RS 6n
1054 An error occurred.
1057 .SH FILES
1059 .ne 2
1061 \fB\fB$TMPDIR\fR\fR
1063 .RS 12n
1064 If this environment variable is not \fINULL\fR, its value is used in place of
1065 \fB/var/tmp\fR as the directory name for the temporary work file.
1069 .ne 2
1071 \fB\fB/var/tmp\fR\fR
1073 .RS 12n
1074 If \fB/var/tmp\fR exists, it is used as the directory name for the temporary
1075 work file.
1079 .ne 2
1081 \fB\fB/tmp\fR\fR
1083 .RS 12n
1084 If the environment variable \fBTMPDIR\fR does not exist or is \fINULL,\fR and
1085 if \fB/var/tmp\fR does not exist, then \fB/tmp\fR is used as the directory name
1086 for the temporary work file.
1090 .ne 2
1092 \fB\fBed.hup\fR\fR
1094 .RS 12n
1095 Work is saved here if the terminal is hung up.
1098 .SH ATTRIBUTES
1101 See \fBattributes\fR(5) for descriptions of the following attributes:
1102 .SS "/usr/bin/red"
1107 box;
1108 c | c
1109 l | l .
1110 ATTRIBUTE TYPE  ATTRIBUTE VALUE
1112 CSI     Enabled
1115 .SS "/usr/bin/ed"
1120 box;
1121 c | c
1122 l | l .
1123 ATTRIBUTE TYPE  ATTRIBUTE VALUE
1125 CSI     Enabled
1127 Interface Stability     Standard
1130 .SH SEE ALSO
1133 \fBedit\fR(1), \fBex\fR(1), \fBgrep\fR(1), \fBksh\fR(1),
1134 \fBsed\fR(1), \fBsh\fR(1), \fBstty\fR(1), \fBumask\fR(1), \fBvi\fR(1),
1135 \fBfspec\fR(4), \fBattributes\fR(5), \fBenviron\fR(5)
1136 \fBregex\fR(5), \fBstandards\fR(5)
1137 .SH DIAGNOSTICS
1139 .ne 2
1141 \fB\fB?\fR\fR
1143 .RS 9n
1144 for command errors.
1148 .ne 2
1150 \fB\fB?\fR\fIfile\fR\fR
1152 .RS 9n
1153 for an inaccessible file. Use the \fBh\fRelp and \fBH\fRelp commands for
1154 detailed explanations.
1159 If changes have been made in the buffer since the last \fBw\fR command that
1160 wrote the entire buffer, \fBed\fR warns the user if an attempt is made to
1161 destroy \fBed\fR's buffer via the \fBe\fR or \fBq\fR commands. It writes
1162 \fB?\fR and allows one to continue editing. A second \fBe\fR or \fBq\fR command
1163 at this point will take effect. The \fB-s\fR command-line option inhibits this
1164 feature.
1165 .SH NOTES
1168 The \fB-\fR option, although it continues to be supported, has been replaced in
1169 the documentation by the \fB-s\fR option that follows the \fBCommand Syntax
1170 Standard\fR (see \fBIntro\fR(1)).
1173 A \fB!\fR command cannot be subject to a \fBg\fR or a \fBv\fR command.
1176 The \fB!\fR command and the \fB!\fR escape from the \fBe\fR, \fBr\fR, and
1177 \fBw\fR commands cannot be used if the editor is invoked from a restricted
1178 shell (see \fBsh\fR(1)).
1181 The sequence \fB\en\fR in an \fBRE\fR does not match a new-line character.
1184 If the editor input is coming from a command file (for example, \fBed\fR
1185 \fIfile\fR \fB<\fR \fIed_cmd_file\fR), the editor exits at the first failure.
1188 Loading an alternate \fBmalloc()\fR library using the environment variable
1189 \fBLD_PRELOAD\fR can cause problems for \fB/usr/bin/ed\fR.