[WPI] [cs2223] [cs2223 text] [News] [Notes] 

cs2223, D97/98 Emacs

Emacs is a programmer's text processor. Unlike a word processor, which is concerned about how the text will appear on the page, a text processor is concerned with high-powered manipulation of text. Appreciating emacs is an acquired taste and many find it's arcane command structure daunting. On some UNIX systems, emacs comes with a command menu system and mouse, similar to most windows-type word processors. If you have that option, use it (at least at the beginning). In addition, emacs can always be operated using only key strokes. That's what we'll concentrate on here.

The best way to get started with emacs is to run the tutorial, which will take an hour or less. Just start emacs by typing emacs. The opening screen should tell you how to begin the tutorial: type C-h (that means hold down the "control" key and press "h" then release "h" then release "control") followed by "t". That is abbreviated C-h t.

If you want to learn more about emacs, printed and on-line manuals are available; see the Notes page. And, within emacs, you can access the on-line help system by typing C-h plus an option. C-h k means "tell me about the following keystroke command". For example, C-h k C-x C-c returns the following:

C-x C-c runs the command save-buffers-kill-emacs:

That combination, C-x C-c is the usual way to quit emacs (the standard UNIX convention C-c by itself won't work).

Some commands use the ESC key. On most keyboards, this is just the "escape" key. The "go to end of file" command is ESC-> (press the "escape" key then hold down the "shift" key and press the "." key then release the "shift" key). Emacs rewards dexterity.

Emacs Concepts

Emacs works with files and buffers. The file is just the usual UNIX file. The buffer is a temporary space in which you manipulate text. You can fill the buffer from a file and you can save the buffer to a file, which is the usual way that a file is edited.

To start emacs and copy a file to a buffer, just include the file name (absolute or relative address). The

emacs test.C

starts emacs with the contents of the file test.C (from my working directory) in the buffer. If you don't include a file name, emacs starts with an empty buffer. Before you quit, you have to remember to tell emacs where to save the buffer.

In window-type environments, text to be moved or altered is often selected by click-dragging the mouse. In emacs, the equivalent is to place a "mark" at the beginning of a selection area and then move the cursor to the other end of the selection region. Then you type in the command you want emacs to apply to the "marked" region. On some, but not all, UNIX systems, the "marked" region will be visibly highlighted on the monitor.

Emacs Commands

There are a zillion emacs commands, maybe more. The twenty (or so) I find most useful are:

File Control

C-x C-f - open file
C-x i - insert a file into the buffer (where the cursor is located)
C-x Cw - write file
C-x s - save buffer to the last file used
C-x C-c - quit and save buffer to the last file used; throw out buffer if no last file

Moving Around in a buffer

 [Figure illustrating next four commands]

C-p - previous line (up)
C-b - back one space (left)
C-f - forward one space (right)
C-n - next line (down)
C-a - start of line
C-e - end of line
ESC-< - start of buffer
ESC-> - end of buffer

On many systems, a keyboard which has arrow keys can be used for the previous, back, forward, and next commands.

Error Recovery

C-x u - undo the last command
C-g - clear keystroke command (give up?). In emacs, key command sequences are so long that sometimes it is easiest to wipe the entire command and start over. This is also useful if you don't understand what you're typing.

Copy, Cut, and Paste

In emacs, these are called, respectively "copy", "kill", and "yank". You mark a region using C-@ (that means hold down both "control" and "shift" as you press "2") and move the cursor to the other end of the region. Then "copy" or "kill" the region. move the cursor to where you want to then "yank" the copied or killed text.

C-@ - mark where the region starts at (perhaps you can think of a better way to remember )
ESC-w - copy the marked region
C-w - kill the marked region
C-y - yank (paste) the previously copied/cut text

Is there a mnemonic for remembering ESC-w and C-w? I haven't found one yet. Isn't it easy to confuse ESC-w and C-w? Yes. If you make a mistake, yank back the text immediately!

--------------------
[WPI Home Page] [cs2223 home page]  [cs2223 text] [News] [Notes] 

Contents ©1994-1998, Norman Wittels
Updated 14Mar98