Setup Ensime with Sublime in OS X: Scala Development with Sublime

This post shows how to setup Ensime with Sublime Text. In particular, we use Sublime 3 to demonstrate this setup process. Therefore, to follow along, we have to make sure that we have Sublime 3 already installed.

Among others, we are going to use the following tools:

Setting up Ensime-Sublime

To get started, first we have to clone Ensime-sublime, a Sublime plugin to interact with Ensime as follows.

$ cd  ~/Library/Application\ Support/Sublime\ Text\ 3/Packages
$ git clone https://github.com/ensime/ensime-sublime.git Ensime
$ git checkout ST3

Since we are using Sublime 3, we have to checkout ST3 branch of the github repository.

We refer to this directory as $ENSIME_SUBLIME:

$ export ENSIME_SUBLIME="/Users/adilakhter/Library/Application Support/Sublime Text 3/Packages/Ensime" 

An alternative to this approach is to clone it in any directory and afterwards, create a symbolic link as follows:

$ ln -s ensime-sublime ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Ensime

Configuring ensime-sbt

Next, we setup ensime-sbt, an SBT plugin. It can be configured at the project-level (by adding the following reference in project\plugins.sbt) or at a global level as outlined next.

$ cd ~/.sbt/0.13/plugins
$ echo "addSbtPlugin("org.ensime" % "ensime-sbt" % "0.1.7")" >> plugins.sbt

Running Ensime Server

First, we have to make sure coreutils is already installed. If not, we use brew to install it:

$ brew install coreutils

Now, to run Ensime server, we execute the following commands at that root of a SBT project:

$ sbt gen-ensime 

It generates all the relevant configuration (see the generated .ensime) to interact with the Ensime server from Sublime.

Consequently, we can start ensime-server as follows:

$ $ENSIME_SUBLIME/serverStart.sh .ensime
...
13:02:15.536 DEBUG None o.e.i.SearchService - committing index to disk...
13:02:15.536 DEBUG None o.e.i.SearchService - ...done committing index
13:02:15.539 DEBUG akka://ENSIME/user/project o.e.c.Project - indexed 0 and removed 0
13:02:16.489 DEBUG akka://ENSIME/user/project/analyzer o.e.c.Analyzer - received handled message FullTypeCheckCompleteEvent in state loading
13:02:16.490 DEBUG akka://ENSIME/user/project/analyzer o.e.c.Analyzer - Analyzer ready in 0.012 seconds.

Configuring Sublime

Last step of this setup is to configure Sublime editor for our project. To do so, first we have to modify the Ensime-related configuration that we can find by navigating at Sublime Text > Preferences > Package Settings > Ensime > Settings - User and update it as follows and restart Sublime:

{
    "connect_to_external_server": true,
    "external_server_port_file": "[project-root]/.ensime_cache/port"
}

Note that, in the future version of Ensime-sublime plugins, external_server_port_file probably will not be required (see discussion at this pull-request). However, at the time of writing, it was indeed required.

If we have followed so far, we are done with configuring Ensime with Sublime. Next, we start development with Sublime.

Beginning Development with Sublime

To start developing with Sublime, we open the root directory of the project for which we have started the server (see “Running Ensime Server” section) and invoke Ensime:Startup in Sublime’s Command Palette (i.e. cmd+shift+p). Afterward, we can also the project with Ensime:Build command, which leads to running relevant sbt command to compile the project.

With respect to features, among others, ensime-sublime supports syntax completions (as shown next), go to definition and etc.

syntax completion

These features are also accessible from the context menu, as illustrated below.

context menu

That’s it for today! If you have any remarks or suggestion, please post. I look forward to your feedback.

PS. Photo credits: Chris McFarland

Advertisements