Wednesday, December 1, 2010

Emacs + Clojure in 60 seconds

Time after time I see questions on how to start using Clojure within Emacs. There's plenty of guides, but most of them are out-of-date or involve unnecessary dependencies. Others are just too difficult for beginner and expect knowing of Emacs Lisp for configuration. Here's my minimalistic guide, described in several easy steps.

Step 1. Install Emacs, if you still haven't.

On Debian-like systems run in terminal:

$ sudo aptitude intsall emacs

on RPM:

$ yup install emacs

for more options see this.

Step 2. Customize it.

If you're new to Emacs world, default keybindings may confuse you. It may be convenient to make some configurations before messing with Clojure itself.

On startup Emacs looks up 2 places for configurations files:
  1. ~/.emacs.el
  2. ~/.emacs.d/init.el
I'll show example for the 2nd case, because we will need some directory to store additional files anyway.

$ cd ~
$ mv .emacs.el .emacs.el.backup                    # backup old config if any
$ mv .emacs.d/init.el .emacs.d/init.el.backup  # same, but for another possible Emacs init file
$ emacs .emacs.d/init.el  # you can any other editor if you're not comfortable with Emacs yet

In editor write ('C' stands for Control, 'S' - for Shift, 'M' - for Alt and 'RET' - for Enter keys):


(global-set-key "\C-q" 'suspend-emacs) ;; this will rebind key Ctrl+Q to close Emacs
(cua-mode) ;; CUA-mode, it will provide keybidnings for Ctrl+C, Ctrl+V, Ctrl+X, Ctrl+Z, and also selecting with Shift key
(show-paren-mode) ;; parenthesis highlighting
Step 3. Install ELPA (Emacs Lisp Package Archive).

Instructions are here.

Step 4. Install required packages.

Press M-x package-list-packages. ELPA module will download archive list and show it in new window:


(Already installed packages are in red.)
You will need these packages: clojure-mode, slime, slime-repl and swank-clojure. Put cursor near each of them and press 'i' to select them. Finally, press 'x' to install selected.
Packages may depend on each other and be installed twice. In this case manager will give an error message. Just remove duplicate file (all downloaded files are stores in ~/.emacs.d/elpa) and repeat installation with the next uninstalled package.

Step 5. Install Clojure.

Just press M-x slime. You will see a message that Clojure is not installed and proposal to do it. Agree and wait until Clojure REPL is opened:


Step 6. Check REPL.

Type 

user> (+ 5 4)
If the answer is '9', then you're done.

Also consider using Leiningen for using all swank/slime features like compilation from editor and evaluation with project-scope packages. For integration with 'lein swank' command see this tutorial.


Possible problems

The thing that confuses many people is using proxy in Emacs. If ELPA cannot list available packages, or slime cannot download required jar's, proxy settings is the most possible reason.
By default, Emacs uses system proxy configuration, i.e. 'http_proxy' environment variable. You can override this behavior to use precise proxy list by adding
(setq url-proxy-services '(("http" . "proxy.yourdomain.com:8080")))
 to your ~/.emacs.d/init.el.

No comments:

Post a Comment

Post a Comment