This month I switched to emacs for scala programming. I was tired of the IntelliJ IDEA slowness and the fact that it consumes lots of CPU and memory. It works great for java, but with scala it’s unpleasant. Then I discovered spacemacs and ensime. I felt a relief. I find it much more ergonomic and faster IDE for scala. However, the beginnings are hard. I wrote this post to help you with the firsts steps in emacs.
I have never used emacs before. After the first launch, I did not even know how to turn it off. I felt the same way when I used vim for the first time. It is a powerful tool but requires some time investment.
Emacs has an enormous amount of plugins and it allows you to configure everything the way you want. However, I am using spacemacs which is a package of pre-configured plugins.
Spacemacs by default is using evil mode which is extensible vi layer for emacs. For me, it was easier to switch to emacs with this mode turned on as I could use the commands that I already know.
For scala, I am using ensime which provides most of the features that heavy IDEs like IntelliJ IDEA or Eclipse have.
To install emacs on OS X run
OSX has old version of emacs already installed in
/usr/bin/emacs. Add this line to your
To fetch the spacemacs configuration execute
Open new terminal window and run
emacs. Spacemacs will ask if you want to use vim or emacs editor mode. Then, it will download and install all the packages. Press
control+x and then
control+c to exit.
To enable scala plugin edit the
~/.spacemacs file and add
Now you need to add ensime plugin to sbt. Add this line to
For every scala project you need to generate
.ensime file. Run these in a root project directory
And that’s all. Now you can start programming in scala using emacs!
Let’s do some simple
hello-world tutorial now. Go to the root project directory and run
SPC p f. After each key you will see all possible commands and description at the bottom.
In emacs commands are described like this
C-x C-f. It means that first you need to press
ctrl+xkeys and then
SPC p pmeans that you need to press
pagain. If you do a mistake, you can cancel operation using
Now you should be able to choose a file to open. Choose something and press
↩. In the file editor, you can use commands like in vim. Press
i to enter insert mode, type quickly
fd to exit. Save the file using
ensime, and press
↩ to run ensime server and connect to it. Press
C-c C-c e to show all errors and warnings. It will create a window. You can switch between windows using
M-1 to come back to opened file. To close current window press
When emacs opens a new file, shows type errors or launches sbt it creates a new buffer for it. You can switch to another buffer in a window using
C-x b. Closing a window does not delete a buffer.
C-c C-b t to start sbt and run tests. Sbt is launched in a new buffer. You can switch to it and use it as a regular sbt shell.
This should be enough for an introduction. You opened and modified a file, type checked the project and run tests. Those are just a few basic commands, but I hope that it encourages you to try emacs.
Emacs provides tutorials. Press
SPC h T to launch the emacs evil tutor or press
C-h t to launch the standard emacs tutorial. Note that for the latter one you will need to disable the evil mode.
I also recommend to print some cheat sheets:
- how to learn emacs
- emacs reference card
- vim cheat sheet
- ensime cheat sheet
- projectile interactive commands
- magit cheat sheet
Emacs allows you to customize key bindings. For that, edit
These commands overwrite the default behaviuor of home, end, page up and page down keys. With these definitions it works like regular editor.
This snippet integrates osx
pbcut with emacs. To select a text use
C SPC. Then, press
C-c c to copy.
I hope that this post might encourage you to try emacs and help you to do the first steps in it. I am sure that the time invested in learning emacs is worth it.