Emacs Help

C311, Spring 1995

Contents

The first section is an introduction to Emacs. If you're familiar with it, you may skip to the section on Scheme programming in emacs. The last section talks about Emacs Customization.

Introduction to Emacs

We suggest using the emacs editor, especially for Scheme programming. It is very powerful and allows Scheme to be run within the editor.

To enter emacs, at the Unix shell prompt enter emacs followed by the name of the file you would like to edit. For example, type

emacs test.ss
Giving emacs a filename, such as test.ss causes the given file to be loaded for editing if it exists. Otherwise, emacs creates a buffer for composing the file. Since the file test.ss does not yet exist, you now see a blank window with the cursor at the upper left corner and a banner at the bottom displaying Emacs and some other information such as (Scheme), indicating that the editor is in its Scheme mode.

Now type

(car '(one two three))
again and observe that when you close each pair of parentheses, the cursor bounces to show you the matching opening parentheses. The expression is not being entered in Scheme itself, so you will not see the result of its evaluation, but it can be sent to Scheme at any time, as we shall see. Press DEL (the delete key) and notice what happens: it erases the character to the left of the cursor.

We shall next learn to use the editing commands of Emacs. In Scheme mode emacs bounces the cursor for parenthesis matching and some other tricks that are great for Scheme programming, but not when you are typing prose (say in English). For that you want to be in text mode. We switch from the Scheme mode to text mode by pressing ESC (the escape key), releasing it, then pressing x. Note that M-x appears at the left end of the banner at the bottom of the screen. Now type text-mode (and then RET). In the future, typing such as sequence will be indicated by M-x text-mode. (M-x is read "meta-x". On some keyboards M-x may also be entered by holding down a meta or alt key while pressing x.)

Notice that now the word (Text) appears in the banner at the bottom of the screen. When you call up Emacs with a filename ending with the extension .txt, Emacs goes directly into Text mode, whereas when the filename has the extension .ss, it automatically goes into Scheme mode.

When entering emacs command names after the M-x (read "meta x") prompt, you can hit SPC (the space bar) to cause Emacs to show you all the current completions of the command you are entering. This can save you from typing the whole command.

Getting Emacs Help

The command C-h (control-h) invokes the emacs help facility. Type C-h again for a brief list of help options, or a third time for a full page description of help options. As always when emacs is prompting for more input at the bottom of the screen you can abort the command that is prompting for information by typing C-g.

Moving Around

Now type a few lines of text. To move the cursor up, press C-p, (p for "previous"). Repeat this until the cursor is on the first line of your text. Press C-n to move the cursor down again (n for "next"). Move the cursor up again to the first line of your text. If it is not at the beginning of the line, press C-a. Now press C-f and note that the cursor moves "forward" one space. Next press C-b, the cursor moves "back" one space.

To move forward over one word, use the command M-f. (Recall this is read "meta-f" and may be entered by pressing ESC and then f.) Next try M-b and watch the cursor move backward one word. You can abort a command or sequence of keystrokes with C-g. For example if you have typed ESC, but then decide that you want to move forward a character, enter C-g to abort the ESC keystroke, and then enter C-f. Try it.

Now enter C-e. The cursor moves to the right end of the current line. Entering C-a moves it back to the beginning of the current line. Two additional commands that you will find useful are C-v which moves ahead to the next screen and M-v, which moves back to the previous screen.

Deleting Text

We saw that DEL deleted the character to the left of the cursor. Now press C-d and note that the character directly under the cursor is deleted. Retype the letter that was deleted. Now press C-k. This deletes (kills) whatever is to the right of the cursor on that line and stores it in a "kill buffer". If you press C-y, the last thing stored in the kill buffer is copied into the position starting at the cursor. Try it.

Saving a File

Observe that there are two asterisks (stars) at the left end of the banner. These indicate that the buffer you have been working in has new material that has not been saved to a file. To save what you have done in a file called mytext.txt, enter C-x C-w (control-x followed by control-w, the "w" stands for "write file"). You will see a prompt in a mini-buffer at the bottom left of the screen under the banner asking for the name of the file. Type mytext.txt and wait until a message is displayed in the mini-buffer indicating that the file has been written. After it has been written, the two asterisks disappear. Note that the filename with its extension appears in the banner line just after the word Emacs:. If you want to save the buffer to the same filename, you can enter C-x C-w and then RET instead of typing a filename. If you are using a PC and not a Mac, you can also save a buffer to a file using C-x C-s, which saves the buffer to the current filename. On the Mac, C-s causes the whole Telnet window to pause, and you must hit C-q to get it going again.

Inserting a File

The command C-x i is used to insert the contents of a file at the current buffer position. It prompts for the name of the file to insert using the mini-buffer, in the same manner as the C-x C-w command. Trying this now using the file you just saved will give you two copies of your material in the buffer.

Undoing Mistakes

If you make a mistake, such as accidentally deleting text, you can undo it with the command C-x u. This is particularly handy if you accidentally type a control-key combination that you have not learned and it does something completely unexpected!

Running Scheme in Emacs

From within Emacs, you can get a UNIX shell with the emacs command M-x shell. You will then get a buffer with a shell prompt in it and can do any shell command you want, including running Scheme. Try it.

It works best to run Scheme within Emacs directly, with the command M-x run-scheme. This creates a buffer in which Scheme runs directly, and you can use Scheme just as you would outside of Emacs.

Sending Mail

To send mail from within emacs, enter C-x m. A new buffer will appear with the cursor after To:. Enter the email address that is to receive your mail on this line (or multiple addresses separated by commas). Enter a brief description of your message following Subject: on the next line. Then move the cursor to the end of the buffer and compose your message. When you are ready to send the message, enter C-c C-c. It will be sent and the buffer will disappear.

Exiting Emacs

To quit Emacs, enter C-x C-c. If there are unsaved files or sub-processes (like Scheme) running you will be prompted to decide what to do with them. Be careful to save any work you want to keep.

Below is a summary of the Emacs commands used in this tutorial. You will have a chance to use them in the assignment which follows the summary.

Summary of Selected Emacs Commands

C-f
Move cursor Forward one character. May use right arrow key.
C-b
Move cursor Backward one character. May use left arrow key.
C-n
Move cursor down to Next line. May use down arrow key.
C-p
Move cursor up to Previous line. May use up arrow key.
C-e
Move cursor to End of line.
C-a
Move cursor to beginning of line.
C-h
Enter the Emacs help facility.
DEL
Delete the character before the cursor.
C-d
Delete the character at the cursor.
C-k
Kill the rest of the line from the cursor to the end of the line.
C-y
Unkill (restore) the last thing killed.
C-v
Display the next screen.
M-v
Display the previous screen.
C-x C-v
Load a new file into the current buffer. Prompts for a file name.
C-x C-w
Write the whole buffer to a file. Prompts for a file name.
C-x i
Insert file. Prompts for the file name.
C-x u
Undo the last text modification.
M-x run-scheme
Run Scheme in its own emacs window.
M-x text-mode
Enter text mode.
M-x scheme-mode
Enter Scheme mode.
M-x shell
Start a UNIX shell in an emacs window.
C-x m
Send an email message.
C-x C-c
Exits from Emacs.

Scheme programming in Emacs

When editing Scheme programs, you always want to be in Scheme mode. We have seen that this happens automatically if you visit a file with the extension .ss, or you can switch to Scheme mode at any time using the command M-x scheme-mode. There is no Scheme prompt in Scheme mode, since we are simply editing text that will later be sent to Scheme. Let's start by typing
(define cat 'cheshire) 
When you typed the right parenthesis, it bounced back and matched the left parenthesis. In this way, you will always be able to tell which pairs of parentheses have been properly closed. At this point, let's save the Emacs buffer to the file pets.ss by entering C-x C-w. You can tell that it has been saved by noting that the two asterisks are no longer at the left end of the banner line.

Using Two Buffers

We want to start up a Scheme process within Emacs, but we also want to be able to see our original file. We can do this because Emacs allows us to view more than one buffer at a time. Type C-x 2, and watch what happens. We now have two buffers on the screen. When we move our location in one, the other stays the same. You can switch between windows using the command C-x o (for other window). Move back and forth a few times, and try moving the cursor in one window and not in the other.

To start up a Scheme process in one of the two windows, enter M-x run-scheme. We should now have our file pets.ss in one half of the screen, and Scheme in the other. This is probably the way in which you will prefer to do your work. Try switching between the Scheme window and the file window using C-x o.

We load the file pets.ss into Scheme by typing the expression (load "pets.ss") at the Scheme prompt. This causes Scheme to read and evaluate the expressions in the file just as if they had been typed in at the prompt. We call this loading the file. Now enter cat in response to the Scheme prompt. (Do not quote the symbol cat this time.) Instead of a message indicating an unbound variable, cheshire is returned. We have bound the variable cat to the value cheshire and when Scheme evaluated the atom cat, it returned its value.

Now to get back to the file pets.ss, type C-x o again. Use C-n or the down-arrow to skip a line below what has been typed, go to the beginning of the following line and then type

(define dog 'beagle) 
Save the buffer again using C-x C-w and return to Scheme using C-x o. In order for Scheme to know the new definition, we must load the file again. You may load the file pets.ss again by either typing the statement again, or by using the arrow keys to move up to the previous line where you loaded the file. Move the cursor to the end of the line (did you remember C-e?) and then press RET. Emacs should have copied the load expression to the last prompt, causing Scheme to evaluate the load expression again, just as if you had typed it again. Respond to the Scheme prompt with dog. We get the answer beagle. Now go back to the pets.ss buffer using C-x o.

Indenting

Emacs will automatically indent Scheme expressions to make them easier to read. This indentation is accomplished by using the RET key, so when you enter an expression that spans more than one line, it will automatically indent the new line appropriately. On the next line, type the following:

(define couple
  (cons cat (cons dog '())))
and observe that when you pressed RET to end the first line the second line was indented automatically. Also, as each right parenthesis was typed, the cursor bounced momentarily to the matching left parentheses. When typing expressions into Scheme directly (within Emacs), the expressions will also be indented. The TAB key may be used to re-indents the current line if the indentation is not right. To reindent every line of a definition, use the command C-x C-i with the cursor immediately after or anywhere within the definition. If there is any possibility that the parenthesis in a definition may not be right, use this command; if there is a problem with parenthesis the indentation will often look wrong.

Now save the file, return to the Scheme buffer, and load the file pets.ss. When you get the Scheme prompt, enter couple. You should get the answer (cheshire beagle). Now while in Scheme, enter

> (cons 'a couple) 
and you get the answer (a cheshire beagle) (What happens if you now type couple again?). Now kill the Scheme process by entering (exit). To get rid of the empty buffer on the screen, change to the other buffer with C-x o and type C-x 1 which will make the current buffer the only one on the screen.

We now return to Scheme to make a point. Type M-x run-scheme again. When you get the Scheme prompt, >, enter cat. You get an error message since the previous Scheme process was terminated and the variable cat is not bound to a value in the current process. Next enter

> (load  "pets.ss")
If we now enter cat, we get the answer cheshire.

Editing in Scheme

We next look at a few features of Chez Scheme within Emacs that we have not yet discussed. Enter the following code after the prompt.

> (define mylist
    (cons 'one
          (cons 'three
                (cons 'five '()))))
If you type mylist in response to the next prompt, you should see the list (one three five). Now press the up-arrow key enough times to get back to the definition of mylist. Using C-p, move the cursor up to the end of (cons 'one. Press C-o. This opens up a blank line below the line (cons 'one. You move to the beginning of that blank line by pressing C-n and pressing Tab will move the cursor under the ' in 'one. (You may also use C-j to open a new line and move to the correct indentation automatically). Now type (cons 'two. We move to the next line using C-n and re-indent the line using Tab. Pressing SPC to add spaces moves the line to the right, but TAB or DEL will bring it back. Now move to the next line and edit the 'five to be 'four and indent the last line to be correct (go to that line and hit <TAB>). You also have to add one right parenthesis at the end of the last line to close all of the parentheses. Your screen should now look like this:

> (define mylist
    (cons 'one
          (cons 'two
                (cons 'three
                      (cons 'four '())))))
With the cursor at the end of the last line, press <CR> to evaluate this expression. Now type mylist and you should see the answer (one two three four). This exercise was designed to show you how to edit in Scheme itself instead of going into an Emacs buffer. The commands for moving around in both are essentially the same, when Scheme is run within Emacs. When Scheme is run outside of Emacs from the shell prompt, it does not have any fancy editing commands.

Cut-and-Paste

We can also move the code we enter at the command line to a file. Press the up-arrow key enough times to get back to the last definition of mylist. Set the cursor at the beginning of the expression and press C-@. Mark set should appear at the bottom of the screen. If that doesn't work, M-x set-mark will.

Now move the cursor to the end of the expression, and press C-w. The lines between where the mark was set and where you pressed C-w should disappear, but they are not gone forever. They are in something called the Kill Buffer. If you now type C-y (yank from kill buffer) the lines will be restored.

Now go to the other buffer, pets.ss, and position the cursor at the bottom of the file. Press C-y again, and another copy of the expression will appear. You may copy expressions either way using this method, from one buffer to another, from Scheme and to Scheme, and within one file. This is analogous to the cut and paste operations many word processors provide.

We are now finished, and you may exit Emacs by typing C-x C-c.

Switching Buffers

If you would like to change the buffer associated with the current window, use the command C-x b. It prompts for a buffer name. Often you will want the most recenly used buffer that is not currently being displayed. In this case, you do not have to know what it is called; just enter RET. Entering TAB will temporarily display a list of all the current buffers if you have forgotten the name of the one you want.

Summary of New Commands

In Emacs

C-j
Go to next line and indent a Scheme expression (in Scheme mode).
C-o
Opens a blank line below the current line.
Tab
Indents a Scheme expression without the newline (in Scheme mode).
C-c C-i
Re-indent the current definition.
C-x 2
Split the window into two buffers.
C-x b
Switch the current window to another buffer.
C-x o
Move the cursor to the other buffer.
C-x 1
Make the current buffer the only buffer.
C-@
Set a mark: a place from which to cut or copy.
C-w
Cut the marked text and put in the Kill Buffer.
C-y
Yank the Kill Buffer to current cursor position.
C-x C-z
Pauses to shell. Return to Emacs by entering fg.
C-h m
Lists mode-specific commands.

In Scheme

(load "filename.ss")
Reads and evaluates the expressions in filename.ss.
(exit)
Terminates the current Scheme session.

Emacs Customization

Emacs can be customized. Before emacs runs, it looks for a file called .emacs in your home directory. If present, it loads all the customization commands from this file. Here is my .emacs file on silver that you may want to copy into your home directory. If you do not like the customization, you are of course, free to modify it in whatever way you like. When you use the customized .emacs file, you can use several new key bindings. Here is the complete list of bindings in scheme mode You can always get this list in emacs with the Emacs command C-h ? b. There is also an excellent on-line Emacs tutorial . You can invoke it from inside Emacs by the command C-h ? t .

Warning! Using the sample .emacs may redefine some of your old bindings. In such a situation, the best thing to do is to redefine the bindings at the end of the .emacs file.

Some useful key bindings

C-c ;
Comment-out-region: Useful for temporarily disabling your scheme program by commenting it out.
C-c ]
complete-parens-from-point: Automatically closes right parentheses.

Abbrevs

Abbrevs are a handy feature in Emacs. If you want to know more about them, please consult the online Emacs info pages by the command C-h i. This means that in scheme mode, for example, instead of typing (define , it is enough to type de (The letter `d', then `e', followed by a SPACE.) The current abbreviations in each mode are describedhere.

Again, questions and are welcome. This document was put together using material from an earlier tutorial for c211.

Last Modified: Jan 12, 1995

Venkatesh Choppella