previous
section   next section   upper level

Section 10: Editing Text with EMACS

One of the most basic operations you will need to perform on a UNIX system is text editing. Whether you are preparing a document, writing a program, or sending email to a colleague, you will need a utility to allow you to enter and edit text.

There are many editors available for UNIX systems, but this discussion will focus on the emacs text editing program because of its power, flexibility, extensibility, customizability, and prevalence. No matter where you get a UNIX account, you are likely to have emacs at your disposal. Here is an index to the topics in this section:

General features of the emacs editor

Emacs is a visual editor. That means that you have a representation of your entire document on your screen, and you can move around freely, editing any part of the document you wish. Older editors, referred to as line editors, required all changes to the file to be made on a line-by-line basis. Each command to a line editor specified a line number, and the changes to be applied to that line. Line editors are truly horrible things, and you should feel lucky if you have never seen one.

Emacs uses control and escape characters to distinguish editor commands from text to be inserted in the buffer. In this document, the notation "Control-X" means to hold down the control key, and type the letter x. You don't need to capitalize the x, or any other control character, by holding down the shift key. "ESCAPE-X" means to press the escape key down, release it, and then type x.

Working with buffers

When you edit a file in emacs, you're not really editing the file itself, as it sits out on a disk somewhere. Instead, emacs makes a copy of the file, and stores the copy in a part of RAM memory called a buffer. All the changes you make to the file are applied to the buffer. When you save the file, emacs writes the contents of the buffer to the disk.

Because the buffer exists in RAM memory, it disappears if the power is turned off, or if the system crashes. Thus, you should use the save command often, flushing your current buffer to disk. Once the file is on disk, a power outage or system crash shouldn't harm it.

Basic operations in emacs

Here are some of the fundamental things you'll need to do when you edit a document in emacs.

Starting emacs

To start emacs, just type the command
emacs
to the UNIX shell. If you want emacs to start with a file already loaded into a buffer, type
emacs filename
where "filename" is the name of the file you want to edit.

Quitting emacs

To exit emacs and return to the UNIX shell, type Control-X-Control-C. If you have made changes to the buffer since the last time you saved it to disk, emacs will ask you if you want to save. Type y for yes, or n for no.

Getting help

Emacs has an on-line help system that can be invoked by typing Control-H. If you type the question mark (?), emacs will present a list of help topics you can choose.

The emacs display

The display in emacs is divided into three basic areas. The top area is called the text window. The text window takes up most of the screen, and is where the document being edited appears. At the bottom of the text window, there is a single mode line. The mode line gives information about the document, and about the emacs session. The bottom line of the emacs display is called the minibuffer. The minibuffer holds space for commands that you give to emacs, and displays status information.

Aborting a command

You can abort an emacs control or escape sequence by typing the command Control-G.

Working with files

To read a disk file into an emacs buffer, type the command Control-X-Control-F. Emacs will ask you for the name of the file. As you type the name of the file, it will be displayed in the minibuffer. When you have entered the file name, press the return key, and emacs will load the file into a buffer, and display it in the text window.

The command to save the contents of the buffer to a disk file is Control-X-Control-S. The save command overwrites the old version of the file. You may also write the contents of the buffer to a different file with the command Control-X-Control-W. Emacs will prompt you for the name of the file you want to create.

To create a new file, use Control-X-Control-F, just as if the file already existed. When emacs asks you for the file name, type in the name you want your new file to have, and emacs will create the file, and display an empty buffer for you to type in.

Emacs will perform file name completion for you. Type part of the name of the file you want, and press the spacebar or tab key to get emacs to complete a file name. If the partial name you've given matches more than one file, emacs will display a list of all potential matches. You can continue typing in more of the file's name, and pressing either file completion key, until you zero in on the file you want.

Cursor motion

On well-configured systems, you will find that the keyboard arrow keys will function correctly in emacs, moving you forward or backward one character at a time, and up or down one line at a time. If the arrow keys do not work, here's how to accomplish the same functions:

In addition to basic cursor motion, emacs provides some other handy cursor motion functions:

Inserting and deleting text

To insert text into a buffer, place the cursor where you want to start inserting text, and start typing away.

If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type Control-X-I. Emacs will ask you for the name of the file you wish to insert.

You may also insert text by cutting it from one place, and pasting it at the insertion point. See the next section for information on cutting and pasting.

Deleting text is easy. As you'd expect, the delete key deletes backward one character. Here are some other ways to delete text:

Cutting and pasting text regions

Emacs allows you to select a region of text, and perform cut and paste operations on the region. It uses a temporary storage area called the "kill buffer" to allow you to store and retrieve blocks of text. There is only one kill buffer in emacs, which means that you can cut text from one document, and paste it into another.

To define a region of text, place the cursor at one end of the region and press Control-spacebar. That sets the mark. Then, move the cursor to the other end of the region. The text between the mark and the cursor defines the region.

To cut a region of text, and place it in the kill buffer, use the command Control-W (think of Wipe).

The paste command is Control-Y. It Yanks the block of text from the kill buffer, and places it where the cursor rests. The Control-Y command only retrieves the most recently-cut block of text.

You can paste in earlier cuts by pressing ESCAPE-Y. The ESCAPE-Y command, used repeatedly, will take you back through several previous text blocks that were cut. The ESCAPE-Y command does not work unless you type Control-Y first.

You may copy a region of text into the kill buffer without cutting it. Define the text block by setting the mark at one end, and moving the cursor to the other end. Then type ESCAPE-W.

Undoing changes

It is possible to undo the changes you have made to a file by entering the command Control-_. (That's Control-underscore. On some keyboards, you'll have to hold down both the control and shift keys to enter the underscore character.)

Many word processing programs can only undo the most recent command, but emacs remembers a long history of commands, allowing you to undo many changes by repeatedly entering the Control-_ code.


previous
section   next section   upper level