Last Updated: February 25, 2016
·
4.434K
· zilti

For beginners: Get Emacs with Clojure up and running!

Goal

After reading this article you will have an up-and-running Emacs setup ready for all your Clojure needs! I'll guide you through the most important basics, provide you a few links to dive deeper into Emacs and then show you how to install and use all that awesome Clojure stuff available!

Prerequisites

There are basically just two pieces of software you need to get manually: Leiningen and Emacs. And, of course, a JVM.
I assume you have a running version of Java; A JVM is enough, you don't need a JDK.

Leiningen

You can get Leiningen from Leiningen.org. Installation is straightforward, on Linux and other UNIX-like systems just put the shell-script into ~/bin, on Windows put it somewhere you like (probably ~/bin as well) and add that directory to your PATH.

Using Leiningen

Open a commandline (windows-key -> cmd -> return) and type lein to use Leiningen.

Emacs

Once again, it's easier on Linux ;) Just install some Emacs 24 and make sure it's not 24.2 since that version has a breaking bug for Clojure development.
For Windows you can download the most recent Emacs version from gnu.org/s/emacs with a click on "Obtain Emacs". Extract it wherever you want.

Getting started with Emacs

Basic stuff

Fire up Emacs.
Phew. That wasn't even that scary, was it? Don't worry, I won't leave you standing here...
Actually, it's pretty simple to use Emacs as soon as you grasp a few core concepts.

In tutorials you often see shortcuts like C-c M-n and similar. C stands for the control key, M for the meta-key (labelled alt on your keyboard) and S, if it appears, for shift.

For the very most basic stuff there is a 10-minute built in interactive tutorial you can fire up using C-h t. It really does its job well, so for that little sequence I'll leave you alone.

...done? Great! Let's jump to the concepts of Emacs that are important for you.

  • You can use C-h ? to get help, or go to http://www.emacswiki.org/
  • If you want to change settings of a mode, you can do so by using M-xcustomize-group
  • You can change the theme and the font using M-xcustomize-theme and M-xcustomize-face default respectively. You probably want to replace the default font with something like Adobe's "Source Code Pro" and I really like the "solarized-theme" which you can install later on using packages.
  • Split windows using shortcuts: ** C-x 2 splits the window into two vertically aligned windows ** C-x 3 does the same horizontally ** C-x 1 makes the current one the only one ** C-x 0 closes the current one ** Resize them using your mouse.

Installing the packages

Luckily Emacs got a package manager starting with version 24, so this gets a lot easier for us.
At first we have to add an additional package source.

Use M-x customize-group package. Keep in mind you can just press TAB for auto-completion.

Click the little arrow beside "Package Archives" to show the options, click INS and add an archive 'marmalade' with the URL http://marmalade-repo.org/packages/ .
Click Apply and Save (or simply use C-x C-s) to store the settings and kill the buffer with C-x C-k <return>.

Now let's see what's available! Keep in mind that you can use C-s to search.

  • M-x package-list-packages

You can mark a package to install it using i or you can click it and choose install. Use x to install marked packages, d to mark for removal.<br/>
You'll need the following to make Emacs itself a more user-friendly environment:

  • idomenu
  • smex
  • sml-modeline
  • ido-ubiquitous
  • pretty-mode-plus

And the following for Clojure:

  • auto-complete
  • ac-nrepl
  • auto-indent-mode
  • clj-refactor
  • cljdoc
  • clojure-mode (Who would've thought THAT?)
  • nrepl
  • paredit
  • rainbow-delimiters

If you want, you can also install "yasnippet" to get snippets, but it doesn't have any Clojure snippets predefined and is somewhat a PITA to set up. I won't cover this in this tutorial.

Configuring Emacs

C-x C-f ~/.emacs <br/>
Welcome to the world of elisp! After you've survived that shock, let's get started!<br/>
Ok, first, place this at the end of the file:

(defun my-init ()
  ;; Your code will go here
)
(add-to-list 'after-init-hook 'my-init)

Explanation: The installed packages are loaded after this file gets parsed, so to configure those packages, we have to ensure our code also loads afterwards.
Now you can reference this gist I made and copy the stuff you like (probably everything; I consider the stuff in this gist fairly minimal) at the place I wrote ;; Your code will go here.

My setup is pretty field-tested; Let me know if you find even better shortcuts.

Usage

Well, from here on everything is pretty straightforward and automatic; Here are some starters:

  • M-x eshell opens an emacs shell.
  • C-x b allows you to switch the content of a window, you'll probably need it when opening an nREPL.
  • Get used to using the awesome nREPL using this doc.
  • Paredit is great stuff, too. C-<leftarrow> and C-<rightarrow> are probably the most useful functions; see this cheatsheet for all available functions. You don't have to use them all, but experiment a bit. You'll love it!
  • If you want some serious debugging stuff (for which I didn't find any use yet) look for ritz-nrepl.