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. ( make heavy use of insert, append, delete, and change commands.) And of course, you need commands to write 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
(The leading colon is the ex command character
used to issue an ex command
from vi; in a script, the colon would be omitted.)
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.
:1,10 co 50 :1,10t50
:/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
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
:g/Unix/ :g/Unix/p :g/Name:/s/tom/Tom/
[address] m destination Move the lines specified by address to the destination address.
:.,/Note/m /END/ Move block after line containing "END"
:100;+5p Show line 100 and the next five lines
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.
:0r $HOME/data Read file in at top of current file
[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
[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. Options
Prompt for confirmation before each change.
Substitute all instances of pattern on each line.
Print the last line on which a substitution was made.
: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
[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
[address] w !command Write lines specified by address, or write full contents of buffer if address is not specified, to theof command.
:1,10w !spell Send first 10 lines to the spell command :w !lpr Print entire buffer with lpr command