Getting started with using SublimeHaskell and Cabal

I recently started using Sublime Text 2 as my main editor for non .net related languages – and I really enjoy it (I think I will pay Mr. Skinner soon – well deserved!).

If you are working with Haskell, then there is a rather good plugin named SublimeHaskell you can use to get automatic compile-error reporting (on save) and an integrated type query and type insert mechanism I quite like.

But in order to get this you have to set up your Haskell project with cabal and install the plugin first.

Install the SublimeHaskell-plugin

The installation itself is very simple once you installed the “Package Control” for Sublime Text 2 (see the link – it’s basically nothing more than running the following snippet inside the Sublime Text 2 console).

import urllib2,os;
pf='Package Control.sublime-package'; 
os.makedirs(ipp) if not os.path.exists(ipp) else None; 
open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen(''+pf.replace(' ','%20')).read()); 
print 'Please restart Sublime Text to finish installation'

With the Package Control ready you just have to use the package-install, search for SublimeHaskell and hit return – done.

Setup a basic Haskell Cabal project

Create a folder for your project and create your Haskell source file(s) – here I’m using a very basic one:

-- Greetings.hs
-- Copyright (c) 2012 Carsten König 

module Greetings where 
import System.Environment

main :: IO ()
main = getArgs >>= print . greetings . head   

greetings s = "Hello! " ++ s

Next create a .cabal file for your project – this is where you tell Cabal how to build/install your files and what dependencies it has, etc.(see the Cabal user guide for additional information):

Name:                greetings 
Version:             0.0 
Description:         say Hi 
License:             GPL 
Author:              Carsten König 
Build-Type:          Simple 
Cabal-Version:       >=1.2  

Executable greetings
   Main-is:           greetings.hs
   Build-Depends:     base >= 3 && < 5 
<p>Important are the name (which names your package), Executable &lt;name&gt; (the –o(utput) name) and Main-is entry.</p>
<p>Finally you will need a (very basic) Setup.hs script for cabal:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:d554d603-bad7-4199-8f61-e66344473225" class="wlWriterEditableSmartContent"><pre style=white-space:normal>

import Distribution.Simple 
main = defaultMain

That’s it – now you can install/build your project with Cabal like this:

cabal install --prefix=$HOME--user

Please note: $HOME is where cabal should write your binary output to – it will create/use a “bin”-folder right below $HOME.

That’s it – now whenever you save your file in SublimeText it ghc will run it and possible errors will show like this:


And even better: if you mapped your keybindings in the “Preferences/Key Bindings – User” settings of Sublime Text like this:

        "command": "haskell_show_type",
        "context": "source.haskell",
        "keys": ["ctrl+k", "ctrl+h", "ctrl+t"]
        "command": "haskell_insert_type",
        "context": "source.haskell",
        "keys": ["ctrl+k", "ctrl+h", "ctrl+i"]

You can put the cursor on a method, use the sequence <Ctrl>-<K>, <Ctrl>-<H>, <Ctrl>-<T> to see the type of the expression:


Or hit <Ctrl>-<K>, <Ctrl>-<H>, <Ctrl>-<I> to add the type above your method:


Neat isn’t it Smiley mit geöffnetem Mund (aside from not noticing String = [Char])

  • short remark: a simple “cabal configure” is enough to get SublimeHaskall working

  • thomas

    cool, thanks! learning haskell now and this helped me getting started.

  • D

    SublimeHaskell package installed, but I get no automatic build and error display upon save. How do I activate this feature?

    • If you installed every dependencie correctly this is controlled in the SublimeHaskell.sublime-settings file (Preferences->Package Settings->SublimeHaskell->Settings Default or better Settings User) – You should copy the Default files content into the User file and remove anything you did not change.

      There you will find a settings (somewhere at the top of the file) named “enable_auto_build” that enables this feature.

      But IMHO this is enabled by default so chances are high that your forget some dependencies or that sublimehaskell cannot find some of your tools (for example cabal) – make sure to include those into your systems path or use the “add_to_PATH” setting in the sublime-settings file.

    • I happened to forget to mention the preferences – you can find those and the way how to install them here: