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!
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.
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.
Open a commandline (windows-key -> cmd -> return) and type lein to use Leiningen.
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
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
- You can change the theme and the font using
defaultrespectively. 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 2splits the window into two vertically aligned windows **
C-x 3does the same horizontally **
C-x 1makes the current one the only one **
C-x 0closes 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.
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/ .
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.
You can mark a package to install it using
i or you can click it and choose
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:
And the following for Clojure:
- clojure-mode (Who would've thought THAT?)
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.
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.
Well, from here on everything is pretty straightforward and automatic; Here are some starters:
M-x eshellopens an emacs shell.
C-x ballows 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-<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.