Getting Started with GNU Emacs

Overview - What is Emacs?

Emacs is very hard to characterize. It is usually thought of as an extensible text editor. It is actually much, much, more. It can be used as an email client, an appointment book, or a file manager. More importantly for our biostats department, it can be used as

Why is Emacs Cool?

Emacs is cool as an editor, because it has

Major editing modes
Major modes customize the methods of editing particular types of files. So... the LaTeX mode 'knows' how to work well with LaTeX files, the S mode 'knows' how to work with S files.
Context-sensitive highlighting
This allows keywords and special constructs within each mode to be drawn differently, typically in different colors. Examples of this would be begin and end codes within LaTeX, or the color of quoted strings in C++. The feedback from such highlighting prevents many common typos.
Context-sensitive formatting
This typically allows proper indentation within blocks of code, such as within if-then-else constructs. This also prevents the common typo of forgetting to end blocks.
Sub-shells allow running applications from within Emacs while using Emacs' major modes to speed the editing of commands. The primary examples of this here are running S (or R) and Matlab from within Emacs.

Sure, there are other editors which can accomplish these tasks for particular languages or applications. The classification of Emacs goes from cool to kewl, though, because it provides a single interface with a single (albeit strange) set of keyboard shortcuts and mouse clicks to learn. It is also extensible. When I used to program Stata very heavily, I yearned for an editor which would format and highlight for me... so I wrote a mode to handle Stata coding.

What Runs Emacs?

Emacs runs on pretty much any flavor of Unix (including GNU/Linux and Mac OS X), on the classic Mac OS, and on pretty much all types of MS Windows which still get used. Here are locations for getting Emacs for yourself, regardless of what OS you like. Note to folks using R at Hopkins: To use the subprocesses, such as R, you will need to use Emacs on the biostat server itself.

Learning Emacs

Getting Started

It's gonna be pretty hard to learn Emacs without starting up the application. To fire up emacs on our system,

  1. Start a terminal session
  2. If you wish to just start Emacs, type
    emacs &
    at the prompt. (The & symbol runs Emacs as a separate process, allowing to use of the terminal window for other things while Emacs is running.)
Enquiring minds might be asking about command line flags or other such unix 'fun'. Be patient - this is dealt with later.

You will be greeted by a window which looks like this:

screen shot of the startup screen

What now?

Take the advice given on the screen and run through the tutorial. As it sez on the screen - the tutorial can be found under the help menu.

Before you start, there are some things to note:

Come back to this page when you're finished. It'll take, oh, about a half hour.

Don't even think of skipping the tutorial!


As you can see, Emacs could very well be an acronym for Excessive Meta and Control Sequences. Don't get too worried. The strength of emacs is its ability to quickly edit without forever grabbing the mouse.

Note:The page up, page down, and arrow keys function as expected for moving around. They are generally easier to use than the Emacs scrollbar, especially if you don't have a three-button mouse.

Getting Help

There are many ways to get help when using Emacs. As you'll note, they all begin with C-h (Control-h):

C-h i (short for Info)
brings up the Info window for learning everything but everything about Emacs.
C-h a (short for Apropos)
takes you down to the mini-buffer so that you can type in a word describing what you would like to know. Emacs will open an *Apropos* buffer which contains all the functions containing the word you typed. So... if, say, searching is what you'd like to do, typing
C-h a<RET>search
will bring up a bunch of commands all of which have to do with searching:

apropos search output

C-h b (short for Bindings)
opens up a second window which shows you all the key bindings (a.k.a. keyboard shortcuts) for the buffer in which you are so diligently working. This is the quickest way to see if there is some good mode-specific binding which could save a bunch of effor.
C-h w (short for Where)
takes you to the mini-buffer, so that you can type in a function that you remember, such as undo, and get the key sequence to which the function is bound.
C-h k (short for Key)
asks you to type a key (like, say C-/). Emacs will open up a second window with an explanation of the function used by the key. This help is invaluable if a key does not seem to be behaving properly (often a key's purpose will be changed depending on the mode).
C-h f (short for Function)
takes you to the mini-buffer, so that you can find out what a particular function does.
OK, OK, maybe this is not as helpful to you as it is to me. I guess I've gotten lost in the tweaking of Emacs.

Working Within Emacs


Emacs will sense that you are working on a LaTeX file if you 'find' (i.e. open) any file whose extension is .tex, .TeX, .ltx, .sty, .cls, .clo, or .bbl. Once you have something like this open, you'll notice a couple of things happen:

shot of LaTeX mode

This picture was taken with customized settings, since the default colors are pitiful for my desktop setup in that awful CDE. KDE or Gnome, here I come!

Emacs will also correctly recognize the file extension .bib for BibTeX files. What are BibTeX files? BibTeX is the automatic bibliography generator for LaTeX, and is invaluable when writing papers.

If you wanna typeset mathematically interesting things, you need to learn to use LaTeX. If you're going to use LaTeX on the Unix server, you should learn Emacs - it'll save immense amounts of time.

R (or S)

Emacs will divine your overwhelming urge to work in R whenever you open any file which has an extension of .s, .S, or .q. It will figure that you are wanting R if a file has an extension of .R. If you wish to start working in S (or R) without having a specific file that you want to edit, simply typing M-x S will start S, and M-x R will start R. Both of these modes allow you to work in S (or R) with the usual context-sensitive highlighting.

No screen shot here! I think that you must be getting the picture by now


By now, you will have guessed that Emacs will detect SAS files (program files, that is), and will help you edit them with nice context-sensitive highlighting. I'm working on getting ESS (Emacs Speaks Statistics) to correctly run SAS from within Emacs, but at this point (Friday, July 28, 2000), it is not quite functional yet. Don't expect too much from the SAS mode as yet.


Emacs will even understand the need to work on Stata programs if you open any file which has an extension of .ado, .lbl, or .do. The Stata mode is not as slick as those above, since it is merely a device to edit Stata files. It is not integrated into a networked version of Stata. (Possibly because we don't have a networked version of Stata.) Still - it is useful for editing Stata programs.

Grabbing the Mouse

This is frowned upon by Emacs purists, even though there is a case to be made for both the mouse and the keyboard. For those who really want to grab the mouse, here are some tips for using a three-button mouse (along with the keyboard equivalents):

This works just fine with the left mouse button. Dragging works as expected. Note: There is something to be said for selecting from the keyboard, though, if you are selecting a definable chunk of text (such as a sentence, paragraph, LaTeX environment), because Emacs has slick ways for doing that. Here is the basic Emacs method for selecting text
  1. Set the mark at one end of the desired selection by hitting C-space
  2. Move to the other end of the desired selection by moving the point. (You could also click the mouse, but that wouldn't be good for purists.)
This is done via C-Mouse Button 2 (control-middle-button). The keyboard is simpler: C-w (for wipe).
The right button will copy once you've made a selection. If, on the other hand, you want to use the true Emacs way, you could just select the text, and then hit M-w.
The middle mouse button will paste (known in Emacs parlance as yank). The keyboard short cut is C-y. Emacs keeps a very large kill ring, and successive C-ys will pull out earlier kills. You can also go to the Edit menu to see what is on the kill ring.

I will probably lose my membership in the True Emacs Lovers Club, since I have even mentioned that a mouse can be used. I personally use the mouse very little, because I find remembering bindings to 3 mouse buttons harder than remembering keyboard shortcuts.

Tweaking Emacs

Why Bother?

  1. The defaults are really hideous.
  2. The default set-up negates the value of the context-sensitive highlighting by having a bad choice of colors.
  3. Color-blindness can wreak havoc on color coding.
  4. C'mon, now! Tweaking is the prime reason for Computers!

Stealing, uh, Borrowing from Others

This is the easiest way to start customizing! One of the wonderful things about Unix is the simplicity with which you can use others' outrageously complex code as though it were your own. To get other folks' customizations for free, just go to their home directories, and copy their .emacs file. There could be some things which don't quite work, but hey, you can't have lost anything!

For those of you who want to give it a shot, here is a link to my .emacs file. Open it, copy it, and put it into your home directory. Of course, if you are here in the biostat department, you should be able to just cp it to your home by typing

cp /home1/biostats/brising/.emacs ~/.emacs

Tweak by Hand?!?!??

Emacs would not be called an extensible editor if it did not have some mechanism for customizations. For most folks, the customizations can be rather intimidating, since everything in Emacs is written in a variant of LISP, known as Elisp. Lisp is wonderful if you understand it, but Lots of Irritating Single Parentheses if you don't.

Starting to Tweak

Luckily, there are some reasonable methods for simple customization which don't involve Elisp. Unfortunately, some of them require knowledge of something equally strange - using flags in Unix commands

Command-line flags

As you (might) know, Unix commands typically have a series of flags which can be set by the user to cause a command to run differently than its default. Here are some of some of the ones I find useful. Note: the first of these - the one which changes the color of the Emacs window is very useful.

-bg UnixColorName
The bg flag will change the background color of the Emacs window.
-fg UnixColorName
The fg flag will change the foreground color of the Emacs window. Hmm... I bet there is a way to do this in Unix, also.
-font UnixFontName
The font flag sets, uh, the default font for the pretty text characters.
-geometry charsWidexlinesLong
The geometry flag sets the size of a default Emacs window.

If you get sick of having to type all the durn command line flags when you just want to start Emacs easily, consider making an alias for the command line in your .cshrc file. Here is an example which

  1. Makes the background a nice light yellow like eye-saver paper.
  2. Makes the foreground (the actual type) black.
  3. Uses the real Emacs icon when Emacs is minimized.
  4. Uses a fixed-width font which I find very readable.
  5. Starts with a window which is 90 characters by 36 lines.
  6. Allows filenames to be passed through the alias to Emacs, so that typing niceemacs foo.tex would start up Emacs editing the file foo.tex.
  7. Runs as a separate window, leaving the terminal window free for more work.

alias niceemacs 'emacs -bg LightYellow -fg Black -i -font 8x13 -geometry 90x36 \!* &'

Now, wasn't that fun? (You can copy this and paste it to the bottom of your .cshrc file, if you like).

Editing Your .emacs File

Understanding How Emacs Starts Up

Emacs, just like other unix applications has initialization files. When Emacs starts, it first races off to see if there is a site-wide (for us: department-wide) start file from which it should pick up customizations. There is such a file, of course, so that each individual user don't have to waste time getting LaTeX to run when (s)he wants it to.

After Emacs has run through the site-wide initialization file, it looks for a .emacs file in the user's home directory. This, also, is typical behavior for a Unix application. Any other tweaking you'd like to do, you can put in this file.

Tweaking Emacs from Within Emacs

The most common customization that people need is that of the color of the window, and the color of the fonts in the context-sensitive highlighting. The order in which the customizations are made is important! Get a background color you like, and then get font colors.

Customizations can be done on the fly from within Emacs by typing M-x customize. A menu with various groups will be presented, and changes can be made by clicking on options(!) using the middle mouse button in Unix (or its equivalent when using an X-server to hook up to Athena).

If this seems complex, try the command M-x customize-apropos, and then entering an appropriate word. Emacs will then bring up a customize buffer containing those items in the customize window containing the word.

Now That You Are On Your Way

Now you should be happily on your way to using Emacs. If you do get unnecessarily worried, remember

Send Bill comments about this page or go to his main home page

Last Updated Wednesday, May 10, 2006, 13:40