Unix Power ToolsUnix Power ToolsSearch this book

20.4. Useful ex Commands

Many line editor commands are not particularly useful in scripts. The two commands that you will use far and away the most often are s (substitute), to replace one pattern with another, and d (delete), to delete one or more lines. On occasion, though, you'll want to insert text from a script. (Editing scripts built by diff (Section 18.6) make heavy use of insert, append, delete, and change commands.) And of course, you need commands to write the file and quit the editor.

Here's the syntax of most of the commands you may encounter in ex editing scripts. (The ed editor understands the abbreviated versions of some, but not all, of these commands.) Elements in [brackets] are optional; don't type the [ or ]. The leading colon (:) shown in examples is the ex command character used to issue an ex command from vi; in a script, the colon would be omitted. The autoindent feature referred to below aids anyone writing structured text. Your editor can ease the burden of creating outlines and source code by positioning the cursor beneath the first character of the previous line.

append
[address] a[!] text .

Append text at specified address, or at present address if none is specified. Add a ! to switch the autoindent setting that will be used during input. For example, if autoindent was enabled, ! disables it.

change
[address] c[!] text .

Replace the specified lines with text. Add a ! to switch the autoindent setting during input of text.

copy
[address] co destination [address] t destination

Copy[55] the lines included in address to the specified destination address.

[55]Note that "t" is short for "to." The ed editor only has one-letter commands and since "c" was already taken for "change," they used "t" for "copy TO."

:1,10 co 50
:1,10t50
delete
[address] d [buffer]

Delete the lines included in address. If buffer is specified, save or append the text to the named buffer.

:/Part I/,/Part II/-1d   Delete to line above "Part II"
:/main/+d                Delete line below "main"
:.,$d                    Delete from this line to last line
global
[address] g[!]/pattern/[commands]

Execute commands on all lines that contain pattern, or if address is specified, on all lines within that range. If commands are not specified, print all such lines. (Exception: doesn't print when you use it from vi by typing : first. You'll need to add a p, as in the second example below). If ! is used, execute commands on all lines that don't contain pattern.

:g/Unix/
:g/Unix/p
:g/Name:/s/tom/Tom/
insert
[address] i[!] text .

Insert text at line before the specified address, or at present address if none is specified. Add a ! to switch the autoindent setting during input of text.

move
[address] m destination

Move the lines specified by address to the destination address.

:.,/Note/m /END/          Move block after line containing "END"
print
[address] p [count]

Print the lines specified by address. count specifies the number of lines to print, starting with address.

:100;+5p                  Show line 100 and the next five lines
quit
q[!]

Terminate current editing session. Use ! to discard changes made since the last save. If the editing session includes additional files in the argument list that were never accessed, quit by typing q! or by typing q twice.

read
[address] r file

Copy in the text from file on the line below the specified address. If file is not specified, the current filename is used.

:0r $HOME/data             Read file in at top of current file
read
[address] r !command

Read the output of Unix command into the text after the line specified by address.

:$r !cal                   Place a calendar at end of file
source
so file

Read and execute ex commands from file.

:so $HOME/.exrc
substitute
[address] s [/pattern/replacement/] [options] [count]

Replace first instance of pattern on each of the specified lines with replacement. If pattern and replacement are omitted, repeat last substitution. count specifies the number of lines on which to substitute, starting with address. The following can be used as options:

c
Prompt for confirmation before each change.

g
Substitute all instances of pattern on each line.

p
Print the last line on which a substitution was made.

c Section 17.9, \U Section 17.14

:1,10s/yes/no/g      Substitute on first 10 lines
:%s/[Hh]ello/Hi/gc   Confirm global substitutions
:s/Fortran/\U&/ 3    Uppercase "Fortran" on next 3 lines
write
[address] w[!] [>>] file]

Write lines specified by address to file, or write full contents of buffer if address is not specified. If file is also omitted, save the contents of the buffer to the current filename. If >> file is used, write contents to the end of an existing file. The ! flag forces the editor to write over any current contents of file.

:1,10w name_list    Copy first 10 lines to name_list
:50w >> name_list   Now append line 50
write
[address] w !command

Write lines specified by address, or write full contents of buffer if address is not specified, to the standard input (Section 43.1) of command.

:1,10w !spell       Send first 10 lines to the spell command
:w !lpr             Print entire buffer with lpr command

-- TOR and DG



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.