Setting up Emacs, Ensime, SBT for Scala code

Ensime is an amazing plugin for developing Scala code in Emacs – it is very similar to the way Slime for lisp works, and works on the same swank RPC system that slime uses. It stands for “ENhanced Scala Interaction Mode for Emacs”, and provides many features that are commonly found only in IDEs, such as live error-checking, symbol inspection, package/type browsing, and basic refactoring. It’s pretty cool!

Here’s a series of steps that should get you on the path to nirvana programming scala with Emacs. Leave a comment if something doesn’t work for you! These steps are for OS X.

Install Scala

The best way to install scala is to follow the instructions here : Download Scala

Install SBT

SBT is the scala build tool – an excellent tool that integrates very well with a bunch of other tools. On OS X, the best way to install SBT is to use either MacPorts or HomeBrew. A simple,

sudo port install sbt

Install the scala-mode for emacs

The best editing mode for Scala is scala-mode2 for emacs. To install, add the following to your init.el script and evaluate the buffer using C-x C-e

(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(unless (package-installed-p 'scala-mode2)
(package-refresh-contents) (package-install 'scala-mode2))

Install Ensime

Download the latest version of Ensime from here.

Once this is unpacked into a directory of your choice, add the following into an emacs buffer and evaluate.

;; load the ensime lisp code...
(add-to-list 'load-path "ENSIME_ROOT/elisp/")
(require 'ensime)

;; This step causes the ensime-mode to be started whenever
;; scala-mode is started for a buffer. You may have to customize this step
;; if you're not using the standard scala mode.
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)

At this point, ensime is installed.

Install the ensime-sbt plugin

Download from here. To add an sbt plugin, the best place to do so is your home directory. Add the following snippet to your plugins.sbt file in /Users//.sbt/plugins/plugin.sbt,

addSbtPlugin("org.ensime" % "ensime-sbt-cmd" % "VERSION")

(Replace VERSION with 0.1.1 or the current version)

Alright – at this point, we have ensime, emacs and sbt integration set up.

So how do we actually use this?

Create a new Scala project

Lets start a brand new project to use these resources. Create a directory called ~/myproject and add the following folder structure,
.
├── project
├── src
│   ├── main
│   └── test

Inside of main, create a file called Main.scala with some sample code,

package com.myproject

object Hello {
  def main(args : Array[String]) = {
    println("Hello World")
  }
}

Now, run sbt in the directory, and on the prompt, type

ensime generate

Next, open the Main.scala file in emacs.

Typing M-x ensime and press enter – this should start the ensime client within emacs.

You’re all set – use the ensime manual to see some sample commands.