Comp212: Emacs tutorial

Introduction

In Comp212, you are given the choice of using DrJava or Emacs to edit your Java code. While DrJava is a pretty decent editing environment, Emacs surpasses it in nearly every way imaginable. Even though Emacs might be a little hard to learn at first, the work you put in now will pay off in later programming courses and as you become more familiar with Emacs. This tutorial is intended to introduce you to some basic editing features of Emacs and show you how it can speed up the coding process.

Preliminaries

Emacs has a design philosophy that sets it apart from a lot of programs that you've probably used before: everything in Emacs is accessible via the keyboard. That doesn't mean you can't use the mouse, though! Accordingly, throughout the rest of this document, I'll be talking about keypresses that you can use to make Emacs do your bidding. Here's what some of the abbreviations mean:

Starting up Emacs

The simplest way to begin Emacs is, at the shell prompt, by entering:

emacs &

This will bring up a new Emacs window, ready for editing.

Perhaps you'd like to edit a particular file with Emacs? No problem!

emacs [file-name] &

Of course, you'll need to replace [file-name] with the name of your file.

Let's say that you're hard at work on your Comp212 homework assignment and you have a lot of Java files to edit, but you don't feel like opening all the files one-by-one. Emacs understands your frustration. Simply cd to the directory containing the files and type:

emacs *.java &

Voila! All of the files you wanted to edit are now loaded into Emacs, ready for editing. You can locate them all under the "Buffers" menu (the farthest left menu on the menubar).

The only problem with this idea is that Emacs now comes up with the main editing window split into two pieces. The piece on the bottom contains one of your Java files, while the piece on top lists all the files that you've opened (or the other way 'round). I'm not quite sure how to instruct Emacs not to do this, but you can easily get rid of the top window. Simply press the q key when your cursor is in the listing panel (this should get rid of the list) and then press C-x 1 to make Emacs return to the normal one-pane view of the world.

Note that the listing will only be displayed if there are three or more Java files that you opened; otherwise, Emacs will simply show both of your files at once and you can use the aforementioned C-x 1 method to make it show only one.

If you prefer to ssh into an Owlnet machine and use Emacs remotely, then you will probably want to use the command

emacs -nw [file-name] &
or
emacs -nw *.java &

This instructs Emacs to not try to open a window for itself, but to instead use the terminal display (the ssh window).

Moving around

Once you've opened a few files, you'll probably need to move the cursor around in order to do some editing. You're also probably used to using the up and down arrows, Page Up and Page Down, etc. to move around. You can do that in Emacs, too, but there are keystrokes that don't involve moving your hands that enable you to do the very same things. Let's look at some of them.

Do not be afraid to experiment a little bit and become comfortable with these commands; moving around does not edit the file or change it in any way!

A little bit of editing

As you've probably figured out already, simply typing will insert words into the file you're currently editing (Emacs calls this a "buffer"). Here are some other equally useful keystrokes:

Tab completion

Between typing in filenames when you want to open them and typing in buffer names when you want to switch to them, you're probably getting sick of remembering what you called your files! Fear not, Emacs has a mechanism to save your tired fingers. It's called "tab completion" and it's extremely useful.

Let's say that you have four files open in Emacs: Foo.java, FooBar.java, FooBarBaz.java, and FooBarBazQuux.java. You're editing Foo.java and realize that you need to edit one of the other files. Since you want to try out tab complete, you type C-x b and begin to type Foo. Now hit Tab and Emacs will display a list of files that you have open beginning with "Foo". You can type . and hit Tab to complete the filename to Foo.java or you can type B and type Tab to have Emacs further complete the filename. See how easy this is?

Of course, Emacs will do tab completion when you're opening files via C-x C-f as well; handy when you have a lot of files that begin with I. When opening files and typing in the first part of the file, then hitting Tab, Emacs will tell you that there are multiple files beginning with that prefix. Press Tab again to see a list of files. Just remember that Emacs will only complete up to the shortest common prefix and can show you the list of files it thinks you might want to open.

Again, experiment with this feature! Once you get used to it, it's hard to go back.

Searching

You can also search for particular pieces of text (such as when the compiler tells you about an error in a particular function); C-s will bring up a prompt that asks you for the word you're looking for. Begin to type it in and notice that Emacs incrementally searches for what you've typed in so far. Once you find a match, it may not be what you're looking for, since you might find an instance of a function call, rather than the function definition itself. You can press C-s again to skip to the next match. If you get to the end of the file without finding the function definition (Emacs will tell you that the search failed), you can press C-s to "wrap" the search to the beginning of the buffer.

C-r will do the same thing, except it will search backwards through the buffer. And of course, once you reach the beginning, pressing C-r again will "wrap" to the end of the buffer.

Miscellaneous

Keybindings that don't fit anyplace else, but might be useful to know about:

More info

This document tells you the basic stuff you need to know about Emacs. There are many resources on the web; some even come with Emacs itself. Emacs has a tutorial built-in, type C-h t to access it. It goes over some of the things you've been shown here and also talks about a few more advanced features. Googling for "emacs key bindings" comes up with a few useful pages, like this one, which lists nearly everything in this document and much more.

Experiment and explore! People can use Emacs productively after knowing just the basic commands here, but you can use Emacs for ten years and still be finding out new things about it. Next to the programming language you're using, it's probably the most useful tool in your programming toolbox.

Good luck!


Please don't hesitate to mail any suggestions or questions about this page. They're quite welcome and future Comp212 students will thank you for it!

N. Froyd, 02 Feb 2003