emacs-tutorial


A Tutorial For Using Emacs with Scheme

This page tries to explain how to use the editor Emacs with Scheme. Emacs is widely regarded as the best choice for developing Lisp-based programming languages. If you already know how to use Emacs, jump straight to the sections "Basic Configuration" and "Working With Scheme".

This does not replace a good tutorial on Emacs. If you don't know Emacs, you are well advised to read one of those. Emacs comes with a tutorial, which can be started by using C-h t - i.e. hold down control, hit h (for help), release both, and hit t (for tutorial).

Notes On Understanding This tutorial

Emacs uses a special notation for describing keys, where C-x means "hold down the control key, hit x, and release both", while C-x o means "hold down the control key, hit x, release both, hit o".

A very frequent command is M-x, where M means the Meta key. PC keyboards usually have an Alt key instead of the Meta key, though some map the windows key to Meta. So, on PC systems, you probably can use [Alt]+[x]. On Mac OS X, it's a bit more difficult. You should use an Emacs specifically built for the Carbon user interface, which maps the Command key to Meta. If you can't find a Meta key on any platform, you can instead hit ESC once.

Introduction to Emacs

Installing Emacs.

Debian

apt-get install emacs

GoboLinux

Compile Emacs

FreeBSD

sudo cd /usr/ports/editors/emacs; make install clean; rehash; emacs &

Fedora

su -c "yum install emacs"

OpenSUSE

sudo zypper install emacs

ArchLinux

sudo pacman -S emacs

Gentoo

emerge emacs

Alpine

apk install emacs

Please add other distributions.

Running Emacs

Emacs runs in GUI mode by default. To run Emacs in a terminal window, use the -nw option (no window).

Basic configuration

The following code, pasted into your ~/.emacs file, will configure Emacs for use with Scheme. Do this only if you are not using quack (and you should just use quack).

;;; Always do syntax highlighting
(global-font-lock-mode 1)

;;; Also highlight parens
(setq show-paren-delay 0
      show-paren-style 'parenthesis)
(show-paren-mode 1)

;;; This is the binary name of my scheme implementation
(setq scheme-program-name "scm")

Be sure to replace "scm" with the name of the Scheme interpreter you are using.

Cursor Movement Commands

Some basic cursor movement commands:

C-a

Beginning of line.

C-e

End of line.

Besides the cursor keys, you might want to use the following movement commands which can be faster to type:

C-p

Previous line.

C-n

Next line.

C-f

Forward one character.

C-b

Backward one character.

Working with buffers, files and windows

One of the basic concepts of Emacs are the buffers. Every file is opened in its own buffer, which Emacs can then display in a window (don't confuse emacs' window with the window of your graphical user interface). The following commands are relevant:

Buffers/Files

C-x C-f

Open a file in a new buffer.

C-x b

Switch to a different buffer in this window.

Windows

C-x 2

Split the window in two.

C-x o

Go to the other window.

C-x 1

Make the current window the only one.

Working with Scheme

When you open a file, file names ending with .scm or .ss will be assumed to be Scheme files, and will be opened in the scheme mode of Emacs.

You will notice syntax highlighting, automatic indentation when you hit TAB, and of course highlighting of matching parens.

Starting Scheme in Emacs

You can run a Scheme interpreter within Emacs. This allows you to control send input from within Emacs.

First, we'll create a window for the Scheme process to be shown in: type C-x 2, followed by C-x o to go to that new window. Now you can run a Scheme process by typing M-x run-scheme. This will run the Scheme process that you configured above in your .emacs.

Go back to the other window where you opened your Scheme file (C-x o). You can use key shortcuts to send any sexpr, the whole buffer, or even the region (cf. the tutorial) to your Scheme process:

C-x C-e

Send the last sexpr to your Scheme process

C-x h C-c C-r

Send the whole buffer. This first marks (cf. the tutorial) the whole buffer (C-x h), and then sends the region (C-c C-r). If you can't seem to remember C-x h like the present author, you can use M-< M-> as well, which moves to the beginning of the buffer, and then to the end.

!! Note that these commands assume your key bindings for :C-x C-e: and :C-x h C-c C-r: are correct!!

Let's try this. Type the following expression in the buffer for your file:

 (define (mult6 x) 
   (* x 4)) 

When the cursor is at the end of the expression (after you typed it), hit C-x C-e. You might notice another prompt showing up in the Scheme process buffer. Emacs sent this sexpr to the process.

You can now test your expression by going to the other window (C-x o), and interacting normally with your Scheme process:

> > (mult6 3)
12
>

In this buffer, you can use M-p and M-n to cycle through your typing history.

In either window, you can use C-h m (help with the mode) to get a list of all relevant key bindings that the Scheme modes added.

Quack

If you are using Emacs for Scheme, you almost certainly want to install quack after undoing whatever changes you did above to make Emacs work with your Scheme implementation (since Quack will do it better).


category-learning-scheme