4 Editing Text with Emacs

In this section we will discuss one of the most important tools you'll use: the text editor. By the end of this session you will be able to:

4.1 What is an Editor?

An editor is a program that helps you create and modify data. In this section, we are interested in text data; that is, characters that make up words, sentences, paragraphs, etc. In the future, we'll see that programs in languages like Java, Eiffel, C, and Modula-2 are also text. Whatever your purpose - creating a program, composing a term paper, or writing home for love, support, and money - you need a text editor.

Text editors come in many different varieties.

  1. Editors that accept commands and display their text in character form only
  2. Editors that have full-blown graphical interfaces, taking commands from mouse-chosen menu selections
  3. Editors that are a cross between these two, having a ``visual'' orientation but retaining the character-based interface.
In this document, we will look at emacs (type 3).

4.2 What is emacs?

The emacs program is a powerful editor (and so much more) developed for use with Unix.

One of the most useful features are modes which allow you to customize the editor for different tasks. The default is text mode which is used for normal text editing. However, we will also be using a Java mode that, in addition to editing the code, highlights reserved word in different colors and does autoindenting so your programs will conform to style standards.

Perhaps the most confusing thing about using emacs is that it often requires what at first seem to be complex and arcane keystrokes to perform even simple tasks. For example, to exit an editing session takes two double-keystrokes: Ctrl-X then Ctrl-C. To make things even more confusing, this is written as C-x C-c in emacs terminology.

However, emacs does have some simple pull-down menus, that act similarly to other graphical based editors you are used to such as Word. For example, you add text by just left-clicking where you want the text entered and then entering the text, and you remove text by highlighting it with the mouse and selecting the cut command in the Files pull-down menu. This allows you to use the editor immediately. However, you should learn the major key commands as you go since they are often faster and easier than pointing and clicking. A good way to do this is to work though chapter 23 in Student Guide to UNIX whenever you have some spare time.

4.3 Starting emacs

The discussion below assumes you have logged onto a workstation and have the standard windows and icons in place.

The easiest way to start emacs is to go to the directory you want to edit in and type the command

emacs &

Remember to press the Return key after the command name. This will cause the computer to run the emacs program.

Another way is to bring up the Workspace menu by right clicking on the desktop area, moving the cursor over the Programs to bring up the Programs menu then over the emacs item and releasing.

The major difference is that if you use the Workspace menu, you will have to navigate through the directory stucture to the directory you want to edit it, whereas with the first method you are already there.

Soon you will see a window such as the following appear[1].

Notice the word *scratch* at the bottom of the window. This indicates that this is a scratch area which has no name yet. If you were editing a file, the filename would appear in this spot.

4.4. The emacs Window

The emacs window has 4 major parts:
  1. The toolbar - pull-down menus that aid in editing (and other tasks).
  2. The buffer area - your workspace.
  3. The mode line - contains information about the buffer you are editing, e.g. the file name.
  4. The echo area / minibuffer - the bottom line serves two purposes:
For more details see Hahn.

The black rectangle in the upper left corner of the buffer is the cursor and indicates where you are working in the buffer.

NOTE: this is different from the mouse cursor. Move the mouse and you'll see the mouse cursor arrow move but the emacs black rectangle cursor will stay put.

ALSO NOTE: if the mouse cursor is inside the window the rectangle will be solid and operations will be performed there. If it is outside the window the rectangle will be hollow and, as you should suspect, emacs will be suspended.

For the rest of this section, the mouse cursor will be referred to as the mouse-cursor and the emacs black rectangle cursor simply as the cursor.

To reposition the cursor move the mouse-cursor to where you want it and left-click.

4.5 Exiting emacs

There are three ways to exit: by closing the window itself (as you would close any window), by using the Files menu, or by using keystrokes.

To exit using the Files menu:

To exit using keystrokes, simply type what you see next to the exit emacs command, i.e., C-x C-c (remember, C-x is emacs for CTRL-X).

4.6 Creating, Editing, and Saving a Simple Text File

Since emacs will throw away stuff in the scratch buffer upon exiting emacs without asking for confirmation, it is a good idea to not be in the scratch buffer when you do something that you want to save. Creating a new file and opening an old file work in the same way. Make sure you are in the right directory. Choose Open File from the Files menu. The bottom line will show something like Find file: ~/cs1/lab1/ Now simply type the name of the file you want to open. If the file exists, this file will be opened. If not, you have created a new file with the name you typed in. If you decide that you don't want a new file after all, type C-g.

Exercise

Start up emacs, create a new file named merchant, position the mouse anywhere inside the window and start entering the following quote from Shakespeare's The Merchant of Venice by simply typing.
The quality of mercy droppeth as the gentle rain from heaven.
NOTE: the text will be entered where the cursor is in the buffer area, not at the mouse-cursor.

If you make a mistake, just use the backspace or delete key to back over it. [2].

Rats, we left off a part! Right after 'mercy' should be "is not strain'd, It".

To enter the omitted text, just position the mouse-cursor after the word 'mercy' and right click. This repositions the cursor and you can enter the omitted part.

To save the file, open the Files menu and select the item Save Buffer. If you want to save the file under a different name, choose Save Buffer As ... instead and type the desired file name.

4.7 Buffers, We Got too Many Stinkin' Buffers

Emacs can be tricky and occassionally extra windows will appear or a buffer area will split in two. This is because you have somehow done something to create a new buffer. Emacs was designed before windowing systems and has a built in system of its own. It was designed to let you work with more than one buffer at a time. At this point, however, you probably don't want this.

Suppose you are editing your survey and you choose List All Buffers from the Buffer menu. You will see the following.

There are four buffers and two windows. (Since the buffer area is split into two windows only two buffers are displayed.) The buffer menu shows all four names.

To remove an unwanted buffer, position the cursor in the unwanted buffer (by moving the mouse-cursor into it and clicking) then use the mouse-cursor to select the Kill Current Buffer item in the Files menu. If you want to go back to viewing one buffer, select the buffer you want to keep in your window and choose One Window in the Files menu.

4.8 Cutting and Pasting Text

As with most editors, emacs supports the standard cut, copy and paste operations. They can be found in the Edit menu. To highlight a region:

4.9 Finding and Replacing Text

The simplest way to find text in a file is to select the Search option in the Search menu. This will cause "Search for string:"-search: to appear in the echo area. Enter the text you are looking for by simply typing it. There is no need to move the mouse-cursor, as you do this. What you type will appear in the echo area.

To repeat the search choose Repeat Search in the Search menu. If you select the Search menu item again, you will have to re-enter the search string.

There are several ways to replace text.

  1. A simple way is to
    1. Select the text to be replaced by left-clicking twice on it (or dragging the mouse over it while holding down the left-button). This will highlight it in gray.
    2. Then cut it out by pressing the Del key.
    3. Then type in the replacement
  2. Another way is to use the Replace operation.
    1. Select the Query Replace item in the Search menu. Query replace: will appear in the echo area
    2. Enter the text you wish to replace and then press Return Query replace text with : will appear in the echo area
    3. Enter the replacement text
    4. Type space or y to replace the first match. or ! to replace all remaining matches.

4.10 Printing

Select Print in the Tools menu, then Print Buffer. This will print the contents of the selected buffer to the default printer. If you have more than one window in the buffer area be careful that the cursor is in the buffer you want printed.

4.11 Accelerators

The key-commands are often referred to as accelerators since they typically take less time to use than the graphical versions. It is often easier to use the key-commands than 'point and click' and you should learn as many as you can - it will pay off in the long run. To facilitate this the key-commands are written in the menu items for their graphical versions in the hope that this will make them easier to pick up. For example, notice that in the File menu the Exit Emacs item has C-x C-c on the right side. This stands for CTRL-X CTRL-C and is the standard way to exit emacs.
November 20, 1998 at 3:30 PM