Emacs is a great and powerful editor for everyone dealing with various source file. I am an happy Mac OS X user and found most of my time editing piece of source code or TeX stuff, so I need an efficient editor. I don’t like very much complex GUI, nor do I advocate the use of a pure text editor for all my editing needs. I use the Aquamacs fork of GNU Emacs, but I am not a guru of Lisp or a fan of R. Stallman views on software. I just find Emacs actually fill all my need, and that’s all!

In what follows, I shall briefly explain why I use Emacs, and not another editor. Then, I describe some of its functionnalities (actually, the ones I use the most) and how we can customize Emacs to facilitate our work.

Overview

Some eight years ago, a colleague of mine introduced me to Emacs. At that time, I was running back and forth between alternative editor for Linux and Windows and found JEdit to be the more convenient across platform. What I very much appreciate among other things is listed below:

  • with just M-q the text is filled up to a specific number of characters (I generally setup my auto-fill-mode at 70)

  • whatever the coding language I am currently using, when I push the ⌦ (tab) I know I will always get the correct indentation

  • setting local variable (-*- in the header, or add-file-local-varable) greatly simplify the process of editing, highlighting, compiling source file

  • generally, I know that C-c C-c will compile my current buffer, and that it will yield the expected result providing I setup the correct major-mode and options (especially when I am using ConTeXt instead of LaTeX)

Customizing Emacs

The .emacs file is your friend. Many users put their personal .emacs customization file on the web, so that anyone can compose its own based on a combination thereof (I vaguely edited mine this way…). However, it is important to understand the very basic principles of Emacs and elisp spirit in order to customize it.

I you spend a lot of time in front of your screen, your eyes may benefit from customizing the default color theme (white background, especially aggressive on the Macbook with bright screen!). The color-mode is your friend. Color customization may also be done through Options≫Appearance for individual buffer and specific major mode, or through Options≫Customize Aquamacs for global settings.

The pictures below show Emacs running in CLI mode (through Terminal.app) and as Aquamacs. The background color may look somewhat very dark but it also fits with my screen background which is set to something like #363640 (but see Color Scheme Designer to get a rough feeling of what it looks like). My default font in the terminal is Apple Monaco 10pt (not smoothed) while in Aquamacs I use anti-aliased Bitstream Vera Sans Mono 12pt, which are freely available from Gnome’s website. In both case, I use the same font for all editing mode.

Emacs CLI
Figure 1: Emacs look and feel (1) Emacs CLI
Aquamacs
Figure 2: Emacs look and feel (2) Aquamacs

A bag of tricks to enhance your emacs experience

I like plain text, with minor formating like centered line (center-line), wrapping at a fixed line width (auto-fill-mode, and fill-region, usually set at 70 or 78 columns depending on what I feel most readable and if it is intended to be published on-line). I also like adding footnote: actually, it is a minor mode that can be activated using M-x footnote-mode. Looking at FN in the modeline would confirm that it is indeed activated. Now, just type C-c ! A and you get a footnote, which print [1] and

Footnotes:
[1]

at the end of the buffer. With C-c ! B you get back home to editing.

With M-x compile when preparing this document, I just get

asciidoc -a stylesheet=tex_hacks.css emacs.asc

because I added a local variable which looks like:

// Local Variables:
// compile-command: "asciidoc -a stylesheet=tex_hacks.css emacs.asc"
// End:

In Asciidoc, specific blocks like the one above are highlighted with a repetition of -, or .. In emacs, you just use C-u 70 - to repeat 70 times the character "-".

It is generally a good habit to date evryfile you are working on. You just have to put in the head of your file (one of the first 6th lines, in fact) Time-stamp: <>, and this will be filled as

Time-stamp: <yyyy-mm-dd hh:mm:ss username>

the next time the file is saved, provided you add the corresponding hook in your .emacs, i.e. (add-hook 'write-file-hooks 'time-stamp). You can also define any macros of your own.

I also use an auxiliary history file to track any major change when editing source code. A Changelog file may be created and updated using C-x 4 a (it will sit in the current working directory).

Rectangular selection is a useful add-on when working with formated file, like csv or tab-delimited text file. The usual way to select a rectangular region is:

  • C-x r r, copy-rectangle-to-register

  • C-x r k, kill-rectangle

  • C-x r y, yank-rectangle

Under Mac OS X, by default the C-space command is assigned to the Spotlight prompt in the menu bar which conflict with the command for setting a mark in Emacs. But you can also use the equivalent C-@ command.

Some useful editing mode

I must admit I mainly edit text, LaTeX/ConTeX, R, Python and Ruby files during my daily activities so my viewpoint may be of limited interest for those working in web development.
[*TeX logos are rendered with correct kerning thanks to Edward O’Connor’s CSS fix.]
Thereafter, I will refer to *TeX as any of TeX, LaTeX, ConTeX, or XƎTeX derivative of D. Knuth’s work.

AUCTeX

The AUCTeX mode offers great support when editing *TeX files. Although it may look poorly refined to those accustomed to pretty GUI with a lot of buttons everywhere, it is in fact really efficient for editing source file. Below is the menu bar as seen on Mac OS X, for TeX and ConTeX modes. In the bottom part of the Figure, I reproduce three of the submenus that I found especially useful for editing source file, although I generally directly enter M-x or C-c commands.

AUCTeX
Figure 3: Emacs and AUCTeX

So, what’s up with AUCTeX? First, we have perfect syntax highlighting, automatic indentation (not always interesting to my opinion, though), default templates readily available, and… through an unique interface you can manage your master tex file, your bibliographic entries, typographical errors, and compile with just C-c C-c!

ESS

The ESS package, which stands for Emacs Speeks Statistics, offers a clean and useable interface to R and JAGS

Actually, the support for Stata on Mac OS X is still lacking, but I am happy withan elisp function found on the ess-help mailing-list (the code looks rather ugly because of the embedded applescript call). You can bin it to any key you want or just call it with M-x ado-send-region-to-Stata.

If you don’t care about graphics under Stata, you can even run Stata in CLI mode, but this would not work within eshell. Personally, I use an alias in my .profile which looks like alias stata='/Applications/Stata10/StataSE.app/Contents/MacOS/stata-se' and run Stata in Terminal.app, as seen in the following picture. When finished, you simply have to type exit.

Stata
Figure 4: StataSE in CLI mode on Mac OS X

There are two minor mode that I find particularly useful when editing R code: Rox for Roxygen documentation, and ElDoc which provides dynamic help for function argument. A running R shell (M-x R) must be available for on-line help to be displayed.

Roxygen is a Doxygen like documentation system

Let’s try it: Copy/Paste the following code chunk:

foo <- function(x) {
  cat("Entering the foo function...\n")
  y <- x + 2
  return(y)
}

Then, move to the first line, and M-x ess-roxygen-fn; this will create the folllowing documentation header:

##' ...
##'
##' ...
##'
##' @param x
##' @return ...

We just have to fill in the fields (header, short description, description of input and output values, and we can add our own, like @author, @example, etc.

Note

There is another documentation system which I discovered very recently: fixr {mvbutils}. It is used by another package, debug, but I will not go into details with them for the moment.