Skip to main content

My Emacs For Scala Development – Part 1

After years of using Intelij IDEA both as Java and then Scala developer, I’ve finally said enough is enough. I could somehow live with the fact that with each version dramatically changes the way you configure the Scala plugin. It was pain (yet bearable) that majority of features I was so accustomed to as Java developer, were simply not represented in the Scala experience. But the thing that crossed the line was it’s slowness. Horrifying, horrendous slowness! I stopped running tests in IDE as SBT was way faster (by the time the tested passed in SBT, Intelij was still compiling sources). Code completion did not work (or worked with 5 seconds latency). I could go on with examples, but the bottom line is that Intelij became enormous, gigantic process that consumed 1 GB of RAM to provide features of a sluggish, leisurely, languid text editor.

That’s when I discovered Emacs. Yeah, I know, it took me a while. But better late then never. I was hoping to get fast text editor with colour highlighting, but I got much much more richer tool. Today I can not think of not using Emacs for any kind of development: Scala, Haskell, Erlang – you name it. Once you are sucked in, you are there forever.

Emacs is all about configurability. However it took me a few weeks to get things the way I want them to be. In the following posts I plan to document all that I’ve learned so far.

Warm up

Installing Emacs is as simple as typing `sudo apt-get install emacs` (Ubuntu) or `brew install emcas` (Mac). There are some issues with running Emacs on Mac from Application launcher (issues that I have not yet resolved), thus for now what I do is I run Emacs from terminal (Cmd-Space -> Terminal -> `emacs &`).

When we run Emacs for the first time, it does not look much impressive.

emacs_new

Before we make any changes to it, take a look at the link provided on the front page called “Emacs Tutorial”. If you are as new to Emacs as I was, invest your time in reading this tutorial. Follow all examples provided as you read it. This tutorial is only 200 lines long and it literally take less then an hour to finish it. It gives all needed foundations. Rest of this post assumes you’ve read the tutorial and that you understand what `M-x` or `C-x C-f` mean.

Things you should know

My guts are telling me that you’ve just completely ignored what I wrote in a paragraph above.You did not familiarise yourself with ‘Emacs Tutorial’ and just continued reading. Haven’t you?  I still encourage you to check that tutorial, but if you want to follow this post now, you must at least understand  some naming conventions and understand what special keys are used for.

Nomenclature

Emacs has a pluggable architecture. All of its features are implemented as plugins and you can extend its functionality by adding additional plugins to it. The only difference is they are not called plugins but modes. When you open a new file, in Emacs we say that we’ve opened a buffer (buffer does not have to necessary be a file but in most cases it is). What we now call a frame, Emacs calls a window. What we call a window nowadays, Emacs calls a frame.

Wow, that’s confusing right? The reason is simple, Emacs was developed long time ago (first versions was crafted in late 70s). It’s so old that by the time it was developed, names that we now use and take for granted, were not even invented yet. But don’t worry, it’s not really that confusing (once you get used to it 🙂 ).

Control, Meta and Super

When we use Emcas, we tend to use a number of key shortcuts to activate specific features. For example to open new file you first press `Ctrl` and then (while still pressing `Ctrl`) press `x` followed by `f`. In all Emacs documentation / blogs / books / tutorials around the web this sequence is represented as `C-x C-f`. Other example could be for instance function `split-window-right`, which is activated by `C-x 2` (you first press together Ctrl and `x` followed by `2`).

Because control key was not enough (with so many features available) other keys are used as well. Most common ones will be : `meta` and `super`. “Now wait a minute” you might say “I have no meta or super on my keyboard!”. Well, you are right. Inventors of Emacs in the late 70s, early 80s were using keyboards like the one below.

emacs_keyboard

Freaky, ain’t it? Fear not, fortunately you can still use Emacs with your modern keyboard. Just remember, whenever you see `Meta` think `Alt`, whenever `super` is mentioned think `Cmd` (on Mac) or `windows button` (on Linux or Windows).

So if I tell you that to copy a text you must `M-w` and to paste it you have to `C-y`, you now know that the sequence is Alt with `w` followed by Ctrl with `y`.

All about look and feel

Ok. So far we’ve established that some Emacs conventions are weird but you keep on to my promise that one can get used to it. Now, let’s move to UI. UI looks bizzare, let’s do something about it. Following paragraph is what I call “Stripping Emacs”.

Init file

All configuration is done in file called `init.el`. That file will live in folder `~/emacs.d`. We don’t have neither file nor folder, so let’s create them

> mkdir emacs.d
> cd emacs.d/
> touch init.el

Stripping

Ok, having `~/.emacs.d/init.el` created it’s time to edit it. Open Emacs (if you haven’t done that already) and `C-x C-f` to open the init.el. File should be empty (since we’ve just created it a moment ago). Paste the following into it (to paste use `C-y`):

(tool-bar-mode 0) 
(menu-bar-mode 0)
(toggle-frame-fullscreen) 
(scroll-bar-mode 0)
(fset `yes-or-no-p `y-or-n-p)

Little insight of what’s going on:
1. we’re removing tool bar – it’s ugly, it’s big, it’s redundant as all it’s features you will know by heart using key shortcuts
2. we’re removing menu bar – you don’t need it, trust me, see the above, more space for code
3. we’re entering full screen mode – while you code you don’t need any distractions, do you
4. we’re removing scrollbars – no mouse usage => no scrollbars needed = more space for code
5. this will let us answer all questions asked by Emacs with simple `y` or `n`

Themes

Now when we have our Emacs naked it’s time to put some make up on. But before we do, we need to learn few things about installing packages (modes, themes etc.) in Emacs.

Modes

Emacs since version 24.x has a built-in mechanism for installing modes from external repositories. First we need to add some repositories to our configuration file (init.el). We will add the three most commonly used: melpa, marmalade and gnu. Paste the following (best at the beginning of the file):

(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
(add-to-list 'package-archives '("marmalade" . "http://marmalade.ferrier.me.uk/packages/"))
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
(package-initialize)

To list all available modes:

  1. `M-x` to execute command
  2. type `list-packages` followed by RET

You should see something similar to:

list-packages

Number of modes that you can install to extend your Emacs is enormous! Browser through (best using `C-v` & M-v)

Monokai Theme

We will install my favorite theme: Monokai and set is as default

  1. `M-x list-packages`
  2. `C-s` to enter search mode
  3. type monokai-theme and hit RET to exit search mode
  4. having cursor on monokai-theme press key `i` to mark it as ‘to be installed’
  5. press `x` to execute installation

Now since we have it installed, we can use it (and make it load every time we run Emacs by default). In your `init.el` file place yet another line

(load-theme 'monokai t)

Now all you need to do is either restart Emacs or reload init.el file. To do the latter just `M-x load-file` and then provide path to your init.el.

Our Emacs should now looks something like this:

emacs_part1_done

Still not impressed? Fear not. This only ends Part 1 of Emacs series. More soon will come… Stay tuned!

  • David Barri

    Cool. I’ve never really used emacs and IDEA regularly wastes SO much of my time and effort. Looking forward to part 2; teach me!

  • Carlos Mendez

    you can try spacemacs which includes vim keybindings and it’s more simple to configure than vanilla emacs (just write scala in the package configuration and you get scala color and ensime autocomplete without need complex installations)

    I keep using intellij because I notice than for a medium size project ensime generates some files than consume a lot of space, nearly 500 mb of disk or even 1 gb..also it’s a bit buggy and fail sometimes, I think than typesafe must contribute to ensime even over eclipse, go is a good demostration of how an editor can be a better tool and replacement for a full ide

    • I’ve seen spacemacs and it’s source of inspiration, but I tend to go with my own customizations.
      And I prefere vanilia Emacs keybindings upgraded with my own using key-chord mode and hydra-mode

  • Leszek Gruchała

    Can I ask why not Sublime or Atom?

    • Three reasons:
      1. I’ve started using it because one of the best hackers I know personally are using it
      2. I’ve fell in love with the enormous amount of extensions that you can add to it; the customizability; ability to make it “your Emacs”
      3. I’m x2 much more productive with it then with any other tool I’ve used before

  • greg nwosu

    the only thing i intellij does better than ensime is search sources, M-x rgrep is good, but intellij indexes the source in memory so lookups a lightening fast

    • There is an awesome mode for that helm-do-ag which combines helm and ag (installed as external program). I will be describing it in my follow blog posts

      • Mateusz Górski

        But it still doesn’t “understand” the code.

        • To “understand” the code there is an awesome Ensime mode (please be patient, more parts of this series are coming!)

          For “grep” however why would it need understand the code? Grep is a grep (no matter how awful that sounds 🙂 )

          • Mateusz Górski

            I am familiar with it, had been using Spacemacs with Ensime for ~4-5 months, still came back to Intellij.

            As for `grep` — it depends on the size of the project. If there’s multiple modules where names can repeat then having a tool that differentiate between those is enormous help.

  • Maxim Novak

    To install on mac –
    $ brew install emacs –with-cocoa

    $ brew linkapps emacs

    • Or http://emacsformacosx.com if the above fails with weird gcc/xcode errors 😉
      Also note that when using the above it writes some config to `~/.emacs`, which has precendence over `~/.emacs.d/init.el`.

      Waiting for part 2 🙂

  • Francisco

    Looking forward to the next post. Part 2 coming soon?