Friday, August 14, 2015

Setting up Emacs for Scala development

Introduction

This article shows the steps necessary to setup Emacs as your development editor for Scala projects. The recommended Emacs version is 2.4.

Basics

There are a few things I can't live without in emacs. The most important for me is speedbar and Sr Speedbar. Let's see how to install it.

Speedbar & Sr Speedbar

Speedbar comes installed with emacs, Sr speedbar requires installation. So to get Sr Speedbar installed, download http://www.emacswiki.org/emacs/download/sr-speedbar.el into your loadpath, normally ~/.emacs/, and add the following to your emacs file:

(require 'sr-speedbar)
(setq sr-speedbar-right-side nil)
(sr-speedbar-open)
(speedbar-toggle-show-all-files)
(sr-speedbar-refresh-turn-off)

This will automatically create a buffer providing navigation facilities everytime you open emacs; quite handy indeed.

Scala development

For scala development we will want to add a couple of features to our emacs:
  • Syntax coloring
  • Indentation
  • ...

MELPA

All good scala packages are hosted in MELPA. To be able to use this repository you need to add the following to your emacs file:

(when (>= emacs-major-version 24)
  (require 'package)
    (add-to-list
       'package-archives
          '("melpa" . "http://melpa.org/packages/")
         t)
           (package-initialize))

After that you execute the following command inside emacs:

ALT + x list-packages

Install the following packages:

scala-mode2
sbt-mode

After this you are good to go. Emacs will color your scala files, and will ident them in a scala fashion. To compile your sources the easiest way is to crate a shell inside Emacs and switch back and foth between them. To create a shell buffer execute:

ALT-x shell

To switch between buffers use:

crtl-b

Wrapping up

There a few packages that will provide better scala support, like autocompletion, but they require the creation of project files, and that topic is outside of the scope of this post. I will explain those more advanced matters in a future post