“Emacs is probably best described as a thermonuclear text editor.”

Actually, it would be rude to call Emacs just a text editor. Emacs is to text editing as the command line is to unix: a unified interface to everything textual on the earth. The unified interface and limitless customizability via a lisp is why it still remains tremendously useful despite being more than 30 years old. If you hate repeating yourself, either in code or when typing, then emacs is the editor for you.

This mentality is why Emacs really shines when you are developing lisp code: you are using a programmable programming language (lisp) in a programmable programmer’s editor (emacs). This kind of flexibility — the ability to automate whatever you desire — is very appealing to a certain type of person.

But emacs is not particularly easy for beginners to learn. For those not familiar what to do when somebody says “type C-c C-d”, what the minibuffer is, what the .emacs file does, or what elisp is should do a little reading before continuing.

And now, for some of the customizations I use when hacking Clojure on emacs.

Firstly, there are some default annoyances of emacs that I want to remove.

(setq inhibit-startup-message t) ;; No splash screen
(setq initial-scratch-message nil) ;; No scratch message
(tool-bar-mode nil) ;; No toolbars
;; Create backup files in .emacs-backup instead of everywhere
(defvar user-temporary-file-directory "~/.emacs-backup")
(make-directory user-temporary-file-directory t)
(setq backup-by-copying t)
(setq backup-directory-alist
`(("." . ,user-temporary-file-directory)
(,tramp-file-name-regexp nil)))
(setq auto-save-list-file-prefix
(concat user-temporary-file-directory ".auto-saves-"))
(setq auto-save-file-name-transforms
`((".*" ,user-temporary-file-directory t)))

Next, I do my configuration related to Clojure, and tell it where I keep my elisp files.

(setq load-path (cons "/PATH/TO/YOURUSERNAME/clj/emacs" load-path))
(require 'clojure-mode)
(require 'slime)
(slime-setup)
(setq slime-use-autodoc-mode nil) ;; Workaround for Clojure 1.3. See http://groups.google.com/group/clojure/browse_thread/thread/692c3a93bbdf740c?tvc=2&pli=1

When actually editing code, the paredit mode is absolutely essential, and highlight-parenthesis really can be useful for helping non-lispers see how the parentheses match up.

(require 'paredit)
(require 'highlight-parentheses)
(add-hook 'emacs-lisp-mode-hook 'enable-paredit-mode)
(add-hook 'clojure-mode-hook
(lambda ()
(highlight-parentheses-mode t)
(paredit-mode t)
(slime-mode t)))
(setq hl-paren-colors
'("red1" "orange1" "yellow1" "green1" "cyan1"
"slateblue1" "magenta1" "purple"))

Switching back between Clojure’s REPL and the Clojure buffer you are editing happens often enough that you should probably map the slime-selector function to a key; I chose F12. The following lets me switch to the REPL by pressing F12 and then r, and back again using F12 and then j.


;; To go back and forth quickly between repl (r) and clojure (j)
(define-key global-map (kbd "") 'slime-selector)
(def-slime-selector-method ?j
"Go to the most recently visited clojure-mode buffer."
(slime-recently-visited-buffer 'clojure-mode))

Finally, you may like to change the default fonts and coloring to be more easy on the eyes:

(require 'color-theme)
(color-theme-tango)
(set-face-attribute 'default nil :family "courier" :height 140)

Now fire up emacs, start a Swank server, and start coding!

Workflow

It will be so much easier to code in Clojure if you know how to use Slime and paredit.

Start by reading the Slime documentation, particularly the excellent video by Marco Baringer describing how Slime can be used to work with Common Lisp code. The vast majority of the video is applicable also to writing Clojure code.

I also found it useful to work through the Editing Common Lisp examples, because the key commands for slime are the same regardless of whether you are writing CL or Clojure.