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.
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.
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.
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.
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”.
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
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`
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.
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:
- `M-x` to execute command
- type `list-packages` followed by RET
You should see something similar to:
Number of modes that you can install to extend your Emacs is enormous! Browser through (best using `C-v` & M-v)
We will install my favorite theme: Monokai and set is as default
- `M-x list-packages`
- `C-s` to enter search mode
- type monokai-theme and hit RET to exit search mode
- having cursor on monokai-theme press key `i` to mark it as ‘to be installed’
- 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:
Still not impressed? Fear not. This only ends Part 1 of Emacs series. More soon will come… Stay tuned!