Emacs is a highly extensible text editor and development environment. It has good support for the tools that we use in 6.170, including CVS and Java-related tools. You can also use it to compose term papers, read your email, and much more.

This document tells you how to get started with Emacs, and gives a reference of useful commands. Other documents tell you how to perform a variety of important tasks related to editing and compiling code, (including how to work with multiple files at once, edit Java files, compile Java programs, run Java programs, and run JUnit) using CVS version control (including how to update, commit changes, and see local changes), and working at home.

Contents:

Getting started with Emacs, and getting help

To run Emacs, type

  emacs &

from the Athena command line. (The & runs the command in the background, so you can continue to use the command line even while Emacs is running. But in general, it is more convenient to use a command shell within Emacs, by typing Alt-x shell, because that will give you a number of additional features resulting from better Emacs integration.)

Emacs has extensive, high-quality documentation. Here are some sources of information on Emacs.

Editing in Emacs

This is a short list of some Emacs keyboard shortcuts. Many of these commands can also be invoked via menus. In a shortcut, "Ctrl" means hold down the Control key while pressing the stated key, and "Alt" means hold down the Alt key (or the Meta key, if your keyboard has one) while pressing the given key. In our documentation for this class, we tend to use "Control" and "Alt" for these keys (because that is what they are labeled on most modern keyboards), but most other Emacs documentation refers to them as "Control" and "Meta", and abbreviates their key commands in the form of "C-x" or "M-x".

Menu Key Quick Name Action
File > Open Ctrl-x Ctrl-f New / Open Create a new file or open an existing file.
File > Save Ctrl-x Ctrl-s Save Save the current file (only).
Ctrl-x s Save all Save all modified files. Typically, Emacs will ask you about saving files when you perform an action, like compiling or exiting Emacs, that might require it.
File > Exit Ctrl-x Ctrl-c Quit Exit/quit Emacs. You will be warned if files are not saved.
Edit > Undo Ctrl-/
Ctrl-_
Ctrl-x u
Undo Undo last command or typed text. Multiple undo works. All three key sequences have the same effect.
Ctrl-space Set 'mark' Set the 'mark' at your current cursor position. See below.
Edit > Copy Alt-w Copy Copy everything from 'mark' until cursor position.
Another way is to highlight the intended region with your mouse.
Edit > Cut Ctrl-w Cut Cut everything from the 'mark' until cursor position.
Edit > Paste Ctrl-y Paste Paste the last thing you previously cut or copied.
Another way is to press the middle button on the mouse at the intended location
Ctrl-s someString Find (forward)
Incremental search
Move the cursor forward to the first occurrence of someString.
Pressing Ctrl-s again will move to the next match.
This search is incremental: it searches as you type, so often you do not have to type the whole string to either find it or determine that it does not appear.
Ctrl-r someString Find (backwards) Move the cursor backwards to the first match of someString.
Pressing Ctrl-r again will move to the next match going backwards.
This search is incremental: it searches as you type, so often you do not have to type the whole string to either find it or determine that it does not appear.
tab Indent Re-indent the current line, relative to the code line above it.
To indent many lines at once, choose a region (either with the mouse, or by set the 'mark' then moving the cursor), then use Alt-x indent-region.
Alt-/ Expand abbreviation Complete the partial word before the cursor. Press Alt-/ again to see another possibility. This works in all Emacs buffers (Java, text, etc.

6.170 Emacs extensions

The 6.170 staff has prepared a set of add-ons to Emacs that should help you when you are writing Java code. These Emacs add-ons are loaded automatically if you ran the student-setup.pl script.

Here are some of customizations. (They are defined in the file /mit/6.170/etc/emacs/6170.el.)

Key Quick Name Action
Ctrl-c Ctrl-r
Alt-x comment-region
(Un)Comment Comment out the selected region, or uncomment if the command is preceded with Ctrl-u
Ctrl-c Ctrl-r
Alt-x jdk-lookup
Javadoc Displays, in a browser window, the JDK Javadoc documentation for a Java package, class, method, or field. You can type the package/class/method/field name with completion.
Middle-click Stack trace Middle-clicking on a Java stack backtrace takes you to the code for the clicked-upon frame. This is a good reason to run your tests within an Emacs shell instead of in an xterm window.