Editing and Printing Files

Commands covered in this section: emacs xemacs jove vi lpr lpq lprm


The two main text editors available on most UNIX systems are emacs and vi. In recent times, emacs has become the most popular, due to its power and flexibility, whereas vi can seem confusing to use, as it is rather unusual, although once mastered many find it very quick to use.

The Emacs Family

Emacs is a powerful, extensible text editor. The name stands for Editor Macros, although some pretend it stands for Emacs Makes A Computer Slow (it is so large it can be slow compared with more simple editors), or for Escape, Meta, Alt, Control, Shift (in reference to the complex key sequences often required). Full versions of emacs come with packages written in Emacs-Lisp, which provide all manner of additional features, such as e-mail and news modes, and even a web browser mode. Also, different modes are usually supplied to aid the editing of different types of files, such as plain text, programming language source code, and TeX files. They can highlight different parts of the file in different colours or fonts, which can make the file easier to work with.

Due to its immense popularity in the UNIX world, emacs has spawned numerous flavours and clones. Three of the main ones are:

All versions should come with extensive documentation and tutorials, which are obviously worthy of careful study if you intend using the program. However, a few basics will be covered here, to start you off. They should apply to all versions of emacs.

To start up the editor, simply type its name. If you type a filename after it, it will attempt to load this file, and it if doesn't exist it may create it. You can then enter some text straight into the editor window.

Commands are given to the editor through key combinations, although in some cases a mouse can be used to select operations from a menu bar. In describing key combinations, the following abbreviations will be used: C- means hold the Control key down and press the following key, so C-g means press and hold the Control key and press g while Control is still held down. Similarly the abbreviation M- is used to indicate the Meta key, which is often next to or close to the space-bar and may be labelled as a diamond. Users of Jove (and some other versions) should use the Escape key instead, labelled "Esc", and this should pressed and released, before pressing the next key.

MovementKey Combination
Forward one wordM-f
Back one wordM-b
Up one screenM-v
Down one screenC-v
To start of lineC-a
To end of lineC-e
To start of sentenceM-a
To end of sentenceM-e
To start of fileM-<
To end of fileM->

Cursor Movement

There are numerous ways of moving the cursor around a text file, aside from the cursor keys which move one character at a time. The more common ways are shown in the table. If there are no cursor keys on your keyboard the following key combinations can be used instead. The letters used stand for Previous, Next, Forward and Back.


ActionKey Combination
Delete character
after cursor
Delete word
before cursor
Delete word
after cursor
Delete to end
of line
Delete to end
of sentence
Insert previous
kill (Yank)
UndoC-X u

Deleting and Pasting

In addition to the standard Delete or Backspace key, which deletes the character before the cursor, there are numerous commands for deleting and moving text, as shown in the table.

The Yank command is very useful when used in conjunction with the Delete to end of line operation. Multiple lines can be deleted, and then pasted back in a different place, using the Yank command. This provides an easy way of moving text around. If you are running a graphical version of emacs you can also use the mouse to select and paste text.

ActionKey Combination
Search forwardC-s
Search backwardC-r
Cancel operationC-g
Save current fileC-x C-s
Edit a fileC-x C-f
QuitC-x C-c

Searching and Saving

The current text can be searched by pressing the appropriate key combination and then entering the string to search for. In some versions of emacs the search is performed interactively; the cursor jumps to the next position which currently matches the entered text. You can search for the next match by pressing the key combination again. The search can be cancelled by pressing C-g, which can also be used to cancel many other operations.

If the Edit file command is given you will be prompted for a the name of a file, which will then be loaded into the editor. But any file you were previously editing will remain in memory. You can open as many files as you like, and each is placed in its own buffer, and there are various ways of changing buffers, depending on your version of emacs.

The Vi Editor

Insert text in front
of cursor
Insert text after
Insert text after
current line
Insert text before
current line
Overwrite textR

The main feature which distinguishes vi from most editors is its separate modes: text entry mode and command mode. The editor can be started by typing vi, with a optional filename. On startup it enters command mode. To change to text entry mode one of the keys listed in the table is pressed, depending of the nature of the entry mode required.

You can now enter text, but you cannot use any of the cursor keys to move around or perform any other operations. To do so, you must leave the mode and enter command mode, by pressing the Escape key. You can then perform the operations described in the following sections.

MovementKey Combination
To start of line^
To end of line$
Forward one wordw
Back one wordb
Up one screenC-b
Up half a screenC-u
Down one screenC-f
Down half a screenC-d
To line n:n

Cursor Movement

The keys shown in the diagram below can be used to move the cursor around. In addition, cursor keys can be used, if they are present on your keyboard.


The last command listed, to jump to a given line number, shows use of the extended command entry facility. The Enter key should be pressed after the command is entered.

ActionKey Combination
Delete characterx
Delete worddw
Delete entire linedd
Delete to end of lineD
Delete block:<range>d
Replace characerr
Replace wordcw (then Esc)
Replace linecc (then Esc)
Yank lines into buffer:<range>y
Paste buffer after current line:pu
Paste buffer after line n:npu

Deleting and Pasting

Vi provides numerous commands for manipulating text, many of which are shown in the table.

To replace a word or line, press the keys listed, then enter the replacement word or line, pressing Escape to finish.

Two of the commands listed contain a <range> parameter which specifies the lines the command is to be applied to. This can be a single line number, or a start and end line number, separated by a comma. There are two special characters that can be used: $ for the last line, and . for the line containing the cursor.

ActionKey Combination
Search forward/
Search backward?
Repeat last searchn
Replace first occurrence of
str on each line with rep
Replace every occurrence of
str on each line with rep
Save current file:w
Save as <name>:w <name>
Edit <name> instead of current file:e <name>
Quit if saved:q
Quit without save:q!

Searching and Saving

To search for a string in the current buffer, press the key shown in the table, type in the search string, and then press Enter.

The replace commands operate on the entire buffer, unless a range parameter is given.

You must save the buffer before quitting, unless you use the quit without save option.

Printing files

Most networked systems have at least one printer attached to the network, which can be used to obtain a hard copy of your work. For most purposes fast dot matrix printers are adequate, although laser printers can be used for higher quality output.

To send a file to a printer type lpr followed by the name of the file to be printed. This will send the file to the default printer, whatever that may be set to. If you wish to use a different printer, you need to know the name of the printer, as it is known to the system, and then you can use the -P option to lpr. For example a laser printer may be known as ps (for Post-Script), in which case lpr -Pps text will send the file to it.

Files sent to a printer are held in a queue until the printer is free to print them. On a busy system this could take some time, especially if the printer runs out of paper or breaks down. You can monitor the print queue, and the progress of your file in it, using the lpq command. It will list the job number, name, size, and owner of all the items on the queue.

Files can be removed from the print queue before they are printed by using the lprm command. Provide this command with the job number or numbers you wish to cancel, obtained using lpq, or use lprm - to cancel all of the items on the queue belonging to you. Make sure you use this command if you make a mistake or change your mind; don't waste resources, and remember you are responsible for files you print, which you may have to pay for. Find out first!

< Previous: Making a Difference ^ Next: Finding Out >

Matt Chapman
Last modified: Sun Aug 10 15:15:49 BST 1997