# 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';
ipp=sublime.installed_packages_path();
os.makedirs(ipp) if not os.path.exists(ipp) else None;
urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));
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
Author:              Carsten König
Maintainer:          Carsten@gettingsharper.de
Build-Type:          Simple
Cabal-Version:       >=1.2

Executable greetings
Main-is:           greetings.hs
Build-Depends:     base >= 3 && < 5



Important are the name (which names your package), Executable <name> (the –o(utput) name) and Main-is entry.

Finally you will need a (very basic) Setup.hs script for cabal:

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:

    {
"keys": ["ctrl+k", "ctrl+h", "ctrl+t"]
},
{
"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 (aside from not noticing String = [Char])

• http://gettingsharper.de/ Carsten König

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?

• http://gettingsharper.de/ Carsten König

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.

• http://gettingsharper.de/ Carsten König

I happened to forget to mention the preferences – you can find those and the way how to install them here: https://github.com/SublimeHaskell/SublimeHaskell