[Next Chapter] [Top page] [Previous Chapter]

CHAPTER 9 - AN INTRODUCTION TO THE EX LINE EDITOR


What's ex for?

Editors available on Unix include:

ed basic line editor

ex line editor

vi screen editor

emacs screen editor

Ex is an enhanced and more friendly version of ed. Vi is a screen-based version of ex. Most users have no practical use for a line editor nowadays, and they are really a relic of an earlier age in computing. However, you may occasionally have to use ex, if for some reason you can't run a screen editor on your terminal. It is covered here mainly to teach something else, namely, the way that Unix handles texts. This is perhaps most transparent when you are using ex. Ex forces the user to use complicated pattern matching operations to do things that are comparatively easy with a screen editor, such as making correcting small typing errors in the text. While taking this approach may at times seem unnecessarily difficult, it should be remembered that what follows here is just a stepping stone to other Unix utilities, such as vi (which you are far more likely to want to use as an editor than ex), and commands that use regular expressions, such as grep, tr and awk. Learning to use ex involves skills necessary for getting the most out of these utilities.

Using ex

Starting ex

The command ex is used to invoke the editor. The format of this command is:

% ex [filename]

A filename can be supplied if you wish to edit an existing file.

% ex oldfile
"oldfile" 10 lines 465 characters
:

Alternatively the filename may be used as the name of a new file:

% ex newfile
"newfile" [Newfile]
:

notice that the prompt for ex commands is the ':' character.

Adding Text

To enter text simply type the command a (short for append), and then type in the text, as follows:

:a
This is the text

Input is terminated by typing a full stop ('.') on a new line:

:a
This is just one line of text
.
:

The command i is used to insert text before the current line.

Saving Your Data

The command w (short for 'write') is used to save your data. The format of this command is:

:w [filename]

If no filename is specified, the filename given when ex was invoked will be used. E.g.:

 :w test.f
 test.f  50 lines  576 characters
 :

The number of lines and characters in the file will be displayed.

Quitting the Editor

The command q (short for 'quit') is used to quit the editor. Note that if changes have been made to the file and have not been saved the editor will respond with a warning message:

 No write since last change (:quit! overrides)

The command quit! (or just q!) must be given if you wish to quit without saving your changes:

Displaying Lines in the File

The p command (for 'print') used to display lines in the file. The format of this command is:

:[line_range] p

If no range is supplied the current line is displayed.

Pressing <RETURN> is equivalent to moving on to and displaying the next line. With small files it is possible to display the entire file by pressing <RETURN> until the end of the file is reached.

Line Ranges

Ranges of lines that can be given to edit commands include:

Absolute line number

6 refers to line 6

1,6 refers to lines 1 to 6

Relative line numbers

-2 refers to 2 lines before the current line

+3 refers to 3 lines after the current line

-2,+3 refers to a range from 2 lines before the current line to 3 lines after the current line

Special symbols

$ refers to the last line in the file e.g. $p to display last line, 1,$p to display entire file

. refers to the current line e.g. .,$p to display from the current line to the end

Examples:

6d			- deletes lines the sixth line1,6d			- deletes the first six lines1,$d			- deletes all lines3a			- append text after line three.,+10w new	- saves the next ten lines to a file called new

The = operator gives the line number, with the last line the default, so typing = gives you the number of lines in a text. The number of the current line is obtained by typing .=.

Deleting Lines

The d command is used to delete lines. The format of this command is:

:[line_range] d

If no line number is given the current line will be deleted. It is possible to supply a range of lines. For example:

 :1,$d

will delete the entire file.

Searching

Searches are carried out by including the search string in slashes ('/'):

/string/

The search will start at the current line.

 :/Jane/
 This is Jane's file

The special characters '^' and '$' can be used to assist the search. For example:

/^This/		will find a line beginning with 'This'/file$/		will find a line ending in 'file'

The last string searched for is the default string. This means that you can repeat a search just by typing //.

Reverse Searches

Reverse searches are carried out by including the search string in question marks ('?'):

 :?string?

The search will start at the current line and search backwards through the file.

Making Substitutions

The s command is used to make substitutions. The format of this command is:

:[line_range]s/old_string/new_string/

If no line number is given substitutions will be made only on the current line. For example:

:s/old/new/

will substitute the first occurrence of the string 'old' with 'new' on the current line. The command:

:.,$s/old/new/

will substitute the first occurrence of the string 'old' with 'new' in every line from the current line to the end of the file.

Global Substitutions

The g command (for 'global') is used to make multiple substitutions on a line. For example:

:s/old/new/g

will substitute all occurrences of the string 'old' with 'new' on the current line. The command:

:1,$s/old/new/g

will substitute all occurrences of the string 'old' with 'new' in the file.

Search strings can also be used in conjuction with the s command in order to carry out more sophisticated global changes. The line range preceding a substitution string may include a search for the string to changed. For example:

:g/old/s//new/g

This means 'search globally for 'old', then replace every occurrence with 'new'. Remember the null string (in s//) stands for the last RE, in this case the RE 'old'. This is the same as:

:1,$s/old/new/g

Additional ex facilities

Additional commands available using the ex editor include:

c replaces lines

t transfers lines

m moves lines

j joins lines

l shows invisible characters

f gives the name of the file being edited

r inserts named file

e edits named file

u undo last change

The commands m and t above work in a similar way, in that they require two line addresses, one before and one after the command. The address in front refers to the source and the address after the destination. If either is omitted, the current line is assumed. Line addresses may be ranges, allowing blocks of text to be moved. Here are a few examples of commands:

:.m2

This moves the current line to a position after line 2.

:1,.m$

This moves a block (line 1 to the current line) to the end of the text.

:1,.t$

This copies the block at the end of the text, leaving the original block untouched.


Exercises

1. Create a file using ex. Put the text of a message in the file and then mail it to someone (see chapter on mail).


2. Use ex to explore the file /etc/passwd. Search for your own listing, and those of others in your group. (You won't be able to save changes to the file).

3. Find a text file to which you have access and copy it to your home directory. Try making some changes to it.


[Next Chapter] [Top page] [Previous Chapter]