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.
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.
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.
The command w (short for 'write') is used to save your data. The format of this command is:
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.
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:
The p command (for 'print') used to display lines in the file. The format of this command is:
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.
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
$ 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
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 .=.
The d command is used to delete lines. The format of this command is:
If no line number is given the current line will be deleted. It is possible to supply a range of lines. For example:
will delete the entire file.
Searches are carried out by including the search string in slashes ('/'):
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 are carried out by including the search string in question marks ('?'):
The search will start at the current line and search backwards through the file.
The s command is used to make substitutions. The format of this command is:
If no line number is given substitutions will be made only on the current line. For example:
will substitute the first occurrence of the string 'old' with 'new' on the current line. The command:
will substitute the first occurrence of the string 'old' with 'new' in every line from the current line to the end of the file.
The g command (for 'global') is used to make multiple substitutions on a line. For example:
will substitute all occurrences of the string 'old' with 'new' on the current line. The command:
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:
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:
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:
This moves the current line to a position after line 2.
This moves a block (line 1 to the current line) to the end of the text.
This copies the block at the end of the text, leaving the original block untouched.
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.