Skip to content

Instantly share code, notes, and snippets.

@Scorpil
Created October 2, 2012 16:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scorpil/3821118 to your computer and use it in GitHub Desktop.
Save Scorpil/3821118 to your computer and use it in GitHub Desktop.
Using Emacs with Ensime for Scala development

This tutorial is tested on Debian linux. Should work on other platforms with minimal changes. This post really helped: http://jawher.net/2011/01/17/scala-development-environment-emacs-sbt-ensime/

  • Install jdk and sbt (more on that: https://class.coursera.org/progfun-2012-001/wiki/view?page=ToolsSetup)

  • Install scala-mode for Emacs:

    apt-get install subversion  # in case you don't have subversion already installed 
    svn export http://lampsvn.epfl.ch/svn-repos/scala/scala-tool-support/trunk/src/emacs scala-emacs
    mkdir ~/.emacs.d/scala  # creating folder for scala extensions
    mv scala-emacs ~/.emacs.d/scala  # moving scala-mode to new folder
  • Update your ~/.emacs file:

    ; -== scala-mode ==-
    (add-to-list 'load-path "~/.emacs.d/scala/scala-emacs")
    (require 'scala-mode-auto) 
    
    (add-hook 'scala-mode-hook
             '(lambda ()
           (scala-mode-feature-electric-mode)
              ))
  • Download last version of Ensime: https://github.com/aemoncannon/ensime/downloads It's an Emacs extension with awesome set of features for scala development.

  • Unarchive Ensime and move it to our scala folder:

    tar xvfz ensime_2.9.2-0.9.8.1.tar.gz  # your file may have other name
    mv ensime_2.9.2-0.9.8.1 ~/.emacs.d/scala/ensime
  • Update your ~/.emacs file to hook up Ensime:

    ; -== Ensime ==-
    (require 'scala-mode)
    (add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))
    (add-to-list 'load-path "~/.emacs.d/scala/ensime/elisp/")
    (require 'ensime)
    (add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
  • Create sbt plugins directory (if not present):Install ensime plugin for sbt:

    mkdir ~/.sbt/plugins
  • Add ensime plugin for sbt:

    echo 'addSbtPlugin("org.ensime" % "ensime-sbt-cmd" % "0.1.0")' >> ~/.sbt/plugins/plugins.sbt

Creating simple sbt project

  • Create and enter project directory
    mkdir ~/scala-hw
    sd ~/scala-hw
  • Create and edit build.sbt. Don't delete empty lines. More on build.sbt: https://github.com/harrah/xsbt/wiki/Getting-Started-Basic-Def
    name := "hello_scala"
    
    version := "1.0"
    
    scalaVersion := "2.9.1"
    
  • Create directory structure for project
    mkdir -p src/{main,test}/scala
  • Create file src/main/scala/Main.scala and edit it with Emacs
    emacs src/main/scala/Main.scala
  • Run sbt console and enter command 'ensime generate'
    sbt
    ensime generate
    
  • Run emacs command ensime-config-gen to configure ensime for this project. Set ~/scala-hw as project root (when asked). Say 'yes' to question if project-type is 'sbt'.
    M-x ensime-config-gen
    ~/scala-hw
    yes
    
  • Connect your project to ensime. Accept .ensime file proposition
    M-x ensime
    yes
    
  • Write and save example program
    object HelloScala {
        def main(args: Array[String]) = println("Hello, Scala!")
    }
  • Open sbt console:
    C-c C-b s
    
  • Run project, by typing 'run' in sbt console.
  • Learn how to use Emacs with ensime http://aemoncannon.github.com/ensime/index.html#tth_sEc4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment