Haskell IDEs, Part 1: Emacs

I’ve been working with Haskell for about 4 months now and I already have a bit of experience with three Haskell development environments: Emacs, EclipseFP and Leksah. I wanted to give an overview of these three, but this turned out to be a lot of writing so I decided to split this up into three separate posts, each describing one IDE. We’ll begin with Emacs.

Before I go into details of developing Haskell using Emacs let me write about Emacs itself. Emacs is a text-based (i.e. no GUI i.e. not WYSIWYG) text-editor for *nix systems. I stayed away from it for years, but when I started with Haskell and Lisp I realised that a lot of people regard Emacs as the best editor for these languages. I decided that it will be best to learn it and see for myself if it’s really that good.

Emacs has extremely steep learning curve. It is (un)famous for its keyboard shortcuts and I must say that this fame is well-deserved. First of all they are not intuitive for people that are used only to modern GUIs. Forget about Ctrl+C/Ctrl+V to copy-paste. Prepare to use C-w / C-y1 instead. That’s very subjective matter and after couple of weeks with Emacs these become quite intuitive. The more objective problem with shortcuts is the fact that they are overcomplicated and extremely hard to type. Unfortunately there’s a lot of examples: C-x C-s is required to save a file and nobody is going to convince me that this is better than simply C-s (this is one of the basic commands, you do it once every few minutes). Moving the cursor is even more insane: C-f / C-b to move it left and right (f denotes forward, b denotes backward), while C-p / C-n moves it to previous and next line respectively. OK, mnemonics are nice, but look where these keys are on the keyboard! That’s completely ridiculous, especially that most of us doesn’t care about mnemonics because we type without looking at the keyboard anyway. That said, editing code in Emacs can be faster than in graphical text editor. How come? Thanks to the keyboard shortcuts, but not because they are good, but simply because they exist. Graphical editors usually don’t have that many shortcuts and even if they do they discourage us from using them since everything can be selected from the menu. The problem is that using a mouse is slower than even the most crazy key combinations (my favourite is C-M-Space-u – that’s four keys at once). Still, Emacs could be a whole lot better if someone made a bold step to redesign its prehistoric shortcuts to something more ergonomic and adjusted to keyboards of today (Emacs shortcuts were designed with different keyboards in mind).

Another distinguishing feature of Emacs is the variety of different plugins (called ‘extensions’). They enhance the basic features of the editor with anything you can possibly imagine, including reading and sending email. There are two extensions that enhance Haskell editing in Emacs: Haskell mode and ghc-mod. Initially I used only the former one. It gives you nice code highlighting (IMO better than EclipseFP and Leksah), provides some basic code templates (guard insertion, where keyword and few more), possibility of starting a Haskell interpreter and interacting with it, e.g. executing code from the editor or automatic insertion of type annotations. However, I wasn’t able to get some of the features running. I’m not an Emacs hacker and I don’t have idea why they didn’t work out of the box, though I tried to change keyboard shortcuts of the non-working commands hoping that they will work.

Haskell mode offers great indentation management: it has four of possible ‘indentation policies’ and you can cycle between them using TAB key. This works great in practice. There are a few more neat features of Haskell-mode, like support for automatic unit testing or Hoogle integration, but I haven’t used them. Here’s a screenshot of Haskell code in Emacs with Haskell-mode enabled:

A nice thing in Emacs is that it doesn’t have any toolbars (except for the menu at the top, but it can be switched off), so when you set your terminal emulator to fullscreen mode you can see quite a lot of code at once. Another feature of Emacs that comes in handy is ability to align text according to a given regexp. This is very useful for aligning haddock comments – you can see an example on the screenshot.

After using Haskell-mode for a while I decided to give ghc-mod a try. These two extensions complement one another. Ghc-mod adds the possibility of code checking using either GHC or HLint, completing keywords, inserting code templates and inferred types of expressions. Unfortunately, most of the features of ghc-mod didn’t work OOTB and I couldn’t make them work. One of the features that unfortunately worked was code checking. It turned out to be a serious problem when I realised that the offending code is highlighted with some horrible background and this highlighting doesn’t disappear until the problem is fixed. Here’s an example:

Well, this was the end of my adventure with ghc-mod.

To sum up, Emacs offers nice syntax highlighting, very good indentation management and support for basic operations. The biggest downside might be an extremely steep learning curve. I enjoyed Emacs when I was learning the basics and working with only one file at a time, but when I started a small project that consisted of multiple files working in Emacs became a nightmare. Switching between multiple files turned out to be very annoying, though this is probably something you can get used to providing you have sufficiently strong will. I don’t, so I started looking for a full featured IDE. Occasionally you’ll hear hackers claiming that IDEs are completely unnecessary and Emacs combined with *nix shell will do just fine. Perhaps this is true for hackers and while I appreciate power of my Linux shell, I still prefer to have a dedicated development environment as most ordinary mortals do. The next post will cover one of them: EclipseFP.

  1. In Emacs convention C denotes Ctrl key, M denotes Alt key. C-w means pressing Ctrl and ‘w’ simultaneously, C-M-q means Ctrl+Alt+q. []

8 Responses to “Haskell IDEs, Part 1: Emacs”

  1. Marko says:

    Emacs indeed has a high learning curve, and one important thing to note is that the default configuration of Emacs is not all that great. For example, by activating ido-mode and windmove, finding files and switching between frames is much easier. Even without it, I don’t see why you think it is annoying. C-x f to find a file, with auto completion, and C-x b to switch to a buffer (with already opened file), again with auto completion, is quite easy.

  2. Jan Stolarek says:

    I did a lot of work to adjust Emacs’ default settings. Aside from enabling some options disabled by default and installing some extensions, I also tried to swap Alt and Ctrl keys so I could type basic shortcuts more easily, the same way they were typed on the space cadet keyboard. I didn’t use extensions that you mention – I’ll give them a try, cause I still use Emacs’ for basic Haskell editing. As I said, switching between the frames is mostly a matter of getting used to it. To me it turned out to be very annoying and I couldn’t get over it. This is of course a subjective opinion, I’m not going to argue that this is general truth about Emacs.

  3. Emacs can be confusing at first and you are confused! :)

    (1) Emacs is available with both a GUI and a text front end. Highlighting in GUI mode is MUCH nicer than in text mode.

    (2) You don’t need to hit save often, or ever! Emacs automatically saves your work to a temporary file as you edit. Emacs asks you about saving any changes when you start a compile and when you leave.

    (3) Switching files is easier in Emacs than in any other editor I’ve ever learned, because buffers are cheap, tiled windows are excellent, etc. See http://emacswiki.org/emacs/CategoryWindows

    There are many things I would criticise about Emacs, but the problems you’ve identified are just part of the learning curve;
    not your fault but not Emacs’ fault either. It’s to your credit
    that you understand that Emacs was designed for a better keyboard than is available on the market today. The prefix keys are a poor and painful hack to deal with a generation of keyboard regressions – a better solution should have been adopted long ago but old habits die hard.

    _Greg

  4. Jan Stolarek says:

    Thanks for all the tips Greg.

    Ad. 1 I think I meant something else that I actually wrote. Indeed, Emacs has a GUI and I should have acknowledged that. What I meant it that Emacs is not a WYSIWYG text editor. I fixed that.

    Ad 2. I wasn’t aware of automatic saving, but still I think the point that I made holds: Emacs’ shortcuts could be a lot better

  5. Hartmut says:

    Thank You for you describing your experiences. Did not find a single citation of flymake ;-) You don’t use? with M-x package-list-packages (MELPA) i added auto-complete, flymake-cursor, flymake-easy, flymake-haskell-multi, ghc, ghci-completition, haskell-mode, highlight-symbol, tabbar, tabbar-ruler an I am very happy with these ;-)

  6. Jan Stolarek says:

    Nope, I don’t use flymake. I know there are probably tons of useful Emacs extensions out there but I don’t have enough time to fiddle with configuration. Anyway, after seeing that SPJ works even without syntax highlighting I started to think that all these extensions don’t really matter :)

  7. clemux says:

    I know I’m almost two years late, but I want to say something about the anti-ergonomical keyboards shortcuts for moving (C-b, C-f, C-p, C-n, etc.).

    Those are the reasons I’ve been unable to choose between vim and emacs for years, with vim having much better ergonomics, and emacs being more powerful overall.

    I finally solved that problem when I learnt about viper-mode and evil-mode[0]. They allow to use emacs in command and insertion modes, with vim keybindings in command mode, and emacs bindings in insertion mode.

    Now I can move with h, j, k, l; delete a ligne with dd, or 3 lignes with 3dd, search text with /pattern, go to line 42 with :42, etc. (evil-mode also adds something really awesome: rectangular selection with C-v)

    [0] http://www.emacswiki.org/emacs/Evil

  8. milan says:

    emacs has no learning curve! c.f.
    http://pj.freefaculty.org/guides/Rcourse/emacs-ess/emacs-ess.pdf

    Emacs is extensible, customizable, programmable, so if you don’t like it feel free to alter it, but also feel free to be altered by it…
    M-x describe-, M-x customize, and friends are really easy to use.

    Also, I actually do love the navigation keys C-b, C-f, C-p, C-n and their Alt(=Meta)-key equivalents and I prefer to use mnemonics from muscle memory in homerow-centred touch typing over inverted-T arrangement. Like many others around me, I’m using a non-qwerty layout, because qwerty is quite frankly very poorly designed for high speed, homerow-centric touch typing. I’d still prefer mnemonics even if I was using qwerty.

    Having prefix keys like C-x, C-c, C-u, etc. is also very useful… C-s should be search and not save, C-a should be move-beginning-of-line and not mark-whole-buffer; prefix keys (or their absence) represent different layers of logic, which to me is a much nicer workflow than, for instance, switching between edit- and view-modes.

    Ever wondered why in the famous learning curve picture, the Emacs curve is a spiral? Because if you’re not going to empty the glass that is filled with habits from the past (no one can do this easily!), you will have to go through a spiral of struggling with your own Editor: you’ll first hate it, then you try to re-configure it into something that it’s clearly not, but you just don’t know any better yet, then you will fail at some point and you will know why, then you will begin to learn and understand: you will hate emacs, you will love emacs, emacs will hate you, but it will also love you… Finally, you will succeed and in the end, when you think you have mastered it, Emacs has already turned you into a different person and all your initial premises are null and void and you’ll find yourself back at the starting point with only a handful customizations that you know by heart and you will be able to get anything you want running within minutes from emacs -q . For some this process takes years, for others only a few weeks, others simply ignore it and use the GUI… I’ve seen people use it as a better “notepad.exe” for over a decade without even knowing about any of it’s advanced functions and they are still great coders and get the job done. I’ve also seen some people who usually come from vi(m), sublime, codeblocks, np++ or such, and in only a few days time they managed to re-invent the wheel, partially because they enjoyed Emacs’ customizability, partially because they didn’t know better. My number one reason to use Emacs is that it’s always been there and it always will be there. I can’t think of an editing problem that’s not already solved in Emacs or that can’t be solved in very few steps.

Leave a Reply

(required)

Staypressed theme by Themocracy