Emacs for Scala Functional programming course

4 min read Original article ↗

This is a brief guide to setting up Emacs for Coursera’s Functional programming principles in Scala course by Martin Odersky. Tools setup videos in the course explain how to setup Scala, SBT (Scala Build Tool) and Eclipse. Use this guide as an add-on for Emacs setup.

Prerequisites:
Ensure you have scala and sbt in your path. You need atleast scala 2.10+ for Ensime plugin to work in Emacs
$ scala -help
$ sbt -help

Emacs + Scala + Ensime setup
1. Install Emacs 24

2. Emacs starter kit:
Emacs starter kit is a good place to start if you don’t already have an Emacs setup

3. Emacs color theme
Choose a color theme that works for you that makes Eclipse look dull. Solarized and Zenburn are popular options.

To configure color-scheme, add themes to ~/.emacs.d/themes directory and add the following lines to ~/.emacs:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'solarized-dark t)

4. scala-mode
Emacs starter kit comes with a package manager that allows you to install custom packages. To install scala-mode for syntax highlighting:

4.1 Add marmalade to the list of package repositories:

(require 'package)
(add-to-list 'package-archives 
    '("marmalade" .
      "http://marmalade-repo.org/packages/"))
(package-initialize)

4.2 M-x package-list-packages to list all available packages
4.3 Find scala-mode and press I to mark for installation
4.4 Press X to install scala-mode package.
4.5 Add the following lines to ~/.emacs file for scala-mode:

(require 'scala-mode)
(require 'scala-mode-auto)
(add-hook 'scala-mode-hook
          '(lambda ()
           (scala-mode-feature-electric-mode)))
(add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))

4.6 C-x C-c to restart Emacs for scala-mode to take effect

This also activates scala electric mode for inserting parenthesis ([{ in pairs

5. Ensime
Ensime is like Slime for Scala. Ensime enhances scala editing in Emacs with syntax errors, compilation errors and autocompletion.

To setup Ensime:
5.1. Download latest version of ensime from: https://www.dropbox.com/sh/ryd981hq08swyqr/V9o9rDvxkS/ENSIME%20Releases
5.2 Extract ensime to ~/.emacs.d/site-lisp/ensime
5.3 Add the following lines to ~/.emacs to enable ensime in scala-mode:

(add-to-list 'load-path "~/.emacs.d/site-lisp/ensime/elisp/")
(require 'ensime)
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)

5.4 Ensime config can be generated for any project by making it an sbt plugin. Add this line to ~/.sbt/plugins/plugin.sbt to generate ensime config for any scala project using sbt:

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

Ensime manual is a good place to start for other Ensime options like refactoring, debugging etc.

6. Scala REPL
Add scala to emacs exec-path to run scala repl in inferior mode from Emacs. Change /usr/local/scala to your scala installation path.

(push "/usr/local/scala-2.10.1/bin/" exec-path)

7. SBT console
Add sbt to exec-path to run sbt console from Emacs. Change /usr/local/sbt to your sbt installation path.

(push "/usr/local/sbt/bin/" exec-path)

Using Ensime in a Scala project:

Now you’re all set for using Emacs for scala development.

1. Create new project
If you’re using a project downloaded from Coursera site like example.zip or recfun.zip, skip to Step 2.

Older versions of sbt used to have an option to generate project structure from scratch. This is no longer available in the new version. Generate basic project structure:

1.1 Create PROJ/build.sbt with the below contents:

name := "recfun"

version := "1.0"

scalaVersion := "2.10.1"

1.2. Create the following directories:
PROJ/src/main/scala/recfun
PROJ/src/test/scala/recfun

1.3 Create PROJ/src/main/scala/recfun/Main.scala:

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

2. Generate ensime config

Run ensime generate in sbt console:

recfun$ sbt
> ensime generate

This generates .ensime file which is used by Emacs.

3. Ensime server

M-x ensime starts ensime server. Select .ensime file and project to use.

In ensime mode:
Saving file highlights compilation/syntax errors.
Tab autocompletes scala code.

4. Scala REPL

C-c C-v z starts Scala REPL inside Emacs

To load .scala file in console:
:load /path/to/code.scala

Alternatively, you can use sbt’s scala console to load all project files automatically.

5. SBT console

C-c C-v s starts SBT console inside Emacs

sbt commands:

  • compile
  • test – to run unit tests
  • console – to start scala REPL
  • submit – to submit coursera assignments

Have fun learning scala from Emacs…
Please leave comments for any suggestions/issues.