9.6. ex Basics

 < Day Day Up > 

The ex line editor serves as the foundation for the screen editor vi. Commands in ex work on the current line or on a range of lines in a file. Most often, you use ex from within vi. In vi, ex commands are preceded by a colon and entered by pressing ENTER.

You can also invoke ex on its own from the command line just as you would invoke vi. (You could execute an ex script this way.) Or you can use the vi command Q to quit the vi editor and enter ex.

9.6.1. Syntax of ex Commands

To enter an ex command from vi, type:

 : [address ] command  [options ]

An initial : indicates an ex command. As you type the command, it is echoed on the status line. Execute the command by pressing the ENTER key. address is the line number or range of lines that are the object of command. options and addresses are described below. ex commands are described in the section "Alphabetical Summary of ex Commands."

You can exit ex in several ways:

:x

Exit (save changes and quit).

:q!

Quit without saving changes.

:vi

Switch to the vi editor on the current file.


9.6.2. Addresses

If no address is given, the current line is the object of the command. If the address specifies a range of lines, the format is:

 x,y

where x and y are the first and last addressed lines (x must precede y in the buffer). x and y may each be a line number or a symbol. Using ; instead of , sets the current line to x before interpreting y. The notation 1,$ addresses all lines in the file, as does %.

9.6.3. Address Symbols

1,$

All lines in the file.

x,y

Lines x through y.

x;y

Lines x through y, with current line reset to x.

0

Top of file.

.

Current line.

num

Absolute line number num.

$

Last line.

%

All lines; same as 1,$.

x-n

n lines before x.

x+n

n lines after x.

-[num]

One or num lines previous.

+[num]

One or num lines ahead.

'x

Line marked with x.

''

Previous mark.

/pattern/

Forward to line matching pattern.

?pattern?

Backward to line matching pattern.


See Chapter 7 for more information on using patterns.

9.6.4. Options


!

Indicates a variant form of the command, overriding the normal behavior. The ! must come immediately after the command.


count

The number of times the command is to be repeated. Unlike in vi commands, count cannot precede the command, because a number preceding an ex command is treated as a line address. For example, d3 deletes three lines beginning with the current line; 3d deletes line 3.


file

The name of a file that is affected by the command. % stands for the current file; # stands for the previous file.

     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net