http://www.viksit.com/tags/scala/setting-up-emacs-ensime-sbt-for-scala-code/Viksit Gaur
Armchair philosophy for a cut throat world.
About | Archive | Essays | Contact
Setting up Emacs, Ensime, SBT for Scala code
March 16, 2013
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.
Share this:
https://blog.ssanj.net/posts/2011-04-06-ensime-with-emacs.html
https://stackoverflow.com/questions/4112838/emacs-ensime-and-sbt
使用SBT构建Scala应用 https://www.cnblogs.com/vincent-hv/p/3309805.html
https://javadeveloperzone.com/scala/installing-scala-on-ubuntu-linux/
https://www.runoob.com/scala/scala-tutorial.html
http://elkpi.com/topics/2018/04/ubuntu-16-04-wine-wxwork.html
http://m.newsmth.net/article/Emacs/111386
https://flurdy.com/docs/ensime/sublime2.html
https://gist.github.com/Scorpil/3821118
https://www.scala-sbt.org/download.html
Emacs で Scala 開発環境を構築 (Ensime)
https://futurismo.biz/archives/2449
https://github.com/bbatsov/ensime ok
https://www.techrepublic.com/article/how-to-install-sbt-on-ubuntu-for-scala-and-java-projects/
Quick Start
1) Install scala-mode
ENSIME is designed to compliment scala-mode (or any other scala language mode). scala-mode can be found in the Scala distribution under ./misc/scala-tool-support/emacs/. The rest of the steps assume your scala-mode is installed and working correctly.
2) Install ensime-mode
Download the ENSIME distribution from the github downloads page. Unpack the ENSIME distribution into a directory of your choosing.
Add the following lines to your .emacs file:
;; 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)
;; MINI HOWTO:
;; Open .scala file. M-x ensime (once per project)
3) Verify Permissions
Verify that the startup script (usually bin/server.sh) has executable permissions.
4) Create Project
In Emacs, execute M-x ensime-config-gen. Follow directions in the mini-buffer to create a .ensime file for your project..
5) Start ENSIME
Execute M-x ensime You only need to do this once per project.
https://blog.ssanj.net/posts/2011-04-06-ensime-with-emacs.html
Ensime with Emacs
I’ve been toying with the idea of looking for an alternative to Intellij for Scala development for a while now. I tried to use Ensime with Emacs a few months ago and never got it going for one reason or another. More recently, I’ve got Ensime and Emacs working together and thought I’d blog about it for anyone else who had trouble getting everything to work together.
Here are the minimum requrements as stated on the Ensime user manual:
1.Unix(y) or Windows OS 2.JVM Version 6 3.Scala 2.8.1 compatible source and libraries 4.Emacs 22 or later (23 is recommended)
Configuring Emacs for Scala
-
Ensure you have a working installation of scala 2.8.1final.
-
Install emacs. On Ubuntu you can do this with
sudo apt-get install emacs
and on the Mac you can:
brew install emacs
Ensure you have at least version 22 or later.
- Go to your scala_installation_dir/misc/scala-tool-support/emacs/ directory. Copy all .el, .elc files and the Makefile into a location where you want to store these files.
Eg. ~/scalaemacs
-
Copy the contrib/dot-ctags file to your ~/.ctags file
-
Using a command shell, change to the above directory and run make to convert the .el files to .elc files.
-
Add the following to your ~/.emacs file:
(add-to-list 'load-path "/path_to_your_elc_files")
(require 'scala-mode-auto)
eg:
(add-to-list 'load-path "~/scalaemacs")
(require 'scala-mode-auto)
You may need to create this file if it does not exist.
- Open a .scala file in emacs to verify syntax highlighting works and other basic scala functionality works.
Installing Ensime
-
Ensime can be downloaded from here. Download the latest version. The current version is -> ensime_2.8.1-0.5.0.tar.gz
-
Extract the archive downloaded to a know location. This will be your ENSIME_ROOT.
Eg. ~/opt/ensime
-
Verify that the ENSIME_ROOT/bin/server.sh file has execute permissions.
-
Add the following to your ~/.emacs file substituting ENSIME_ROOT for where you extracted the archive:
;; 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)
;; MINI HOWTO:
;; Open .scala file. M-x ensime (once per project)
Your complete .emacs files should look something like:
(add-to-list 'load-path "~/scalaemacs")
(require 'scala-mode-auto)
;; Load the ensime lisp code...
(add-to-list 'load-path "~/opt/ensime/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)
;; MINI HOWTO:
;; Open .scala file. M-x ensime (once per project)
Creating a Project with SBT
You need to create an Ensime project for each project you want to use with Ensime. The project details are written to a .ensime file in your project root. Ensime currently has support for SBT, Maven and Ivy. If you don’t have a build system you can also generate a .ensime file through the wizard or by hand.
The following is how to create a project for an existing SBT project:
- Launch emacs
- Type M-x to open the mini-buffer and then type: ensime-generate-ensime-config-gen. A note on the Meta key (or M-) combinations: On linux M-x is Alt+x, while on the Mac it’s Esc+x. Play around until you find which meta key is used on your flavour of OS.
- Specify the root of your project.
- If your project is an SBT project, it automatically detects most settings and you should see a message like: “Your project seems to be of type ‘sbt’, continue with this assumption? (yes or no)”. Choose yes.
- Enter all the other information requested.
- At the end you will see something like “Your project config has been written to /xyz/.ensime. Use ‘M-x ensime’ to launch ENSIME.” Your ensime file has been written and you are ready to use ensime.
- Type M-x and in the mini-buffer type: ensime to launch the Ensime server for your project. Reconfirm the location of your project.
- That’s it! :)
Neat Features
- Type inspection - will dive into details of the type at the cursor
- Automatic member completion (eg. typing “blah”. followed by the Tab key will give you a list of the methods on String.
- Navigation between sources
- Refactoring (Renaming, Optimizing imports etc)
- Source formatting
- SBT support
- Dropping files into the Scala REPL
- Debugging (I haven’t had much luck getting this to work)
For a full list have a look at the online Ensime user manual.
screenshot of ensime’s autocomplete feature