home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 8.10 Setting Up Vi Chapter 9 Next: 9.2 Alphabetical Summary of Ex Commands
 

9. The Ex Editor

Ex is a line editor that serves as the foundation for the screen editor vi. Ex commands 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 RETURN.

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

This section presents the following topics:

  • Syntax of ex commands

  • Alphabetical summary of commands

For more information, see the Nutshell Handbook Learning the vi Editor .

9.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. Enter the command by pressing RETURN. 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 alphabetical summary.

You can exit ex in several ways:

:x

Exit (save changes and quit).

:q!

Quit without saving changes.

:vi

Quit and enter the vi editor.

9.1.1 Options

!

Indicates a variant command form, overriding the normal behavior.

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 current file; # stands for previous file.

9.1.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 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.1.3 Address Symbols

1,$ All lines in the file.
% All lines; same as 1,$ .
x , y Lines x through y .
x ; y

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

0 Top of file.
. Current line.
n Absolute line number n .
$ Last line.
x - n n lines before x .
x + n n lines after x .
-[ n ] One or n lines previous.
+[ n ] One or n lines ahead.
'x Line marked with x .
'' Previous mark.
/ pattern / Forward to line matching pattern .
? pattern ? Backward to line matching pattern .

See Section 6 for more information on using patterns.


Previous: 8.10 Setting Up Vi UNIX in a Nutshell: System V Edition Next: 9.2 Alphabetical Summary of Ex Commands
8.10 Setting Up Vi Book Index 9.2 Alphabetical Summary of Ex Commands

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System