hscolour is a small Haskell script to colourise Haskell code. It currently has four output formats: ANSI terminal codes, HTML with <font> tags, HTML with CSS, and LaTeX.


Here's a little example of the HTML output:

    module Main (main) where
    import Prelude 
    -- The notorious nfib "benchmark", but using Doubles.
    main = print (nfib 30)
    nfib :: Double -> Double
    nfib n = if n <= 1
             then 1
             else nfib (n`subtract`1) + nfib (n`subtract`2) + 1



Just use one of the standard ways of building a Haskell program:

  • hmake HsColour
  • ghc --make HsColour
  • runhugs HsColour


  • HsColour [ -tty | -html | -css | -latex | -anchorHTML | -anchorCSS ] [file.hs]

You can colourise a Haskell source file for either ANSI terminal codes (option -tty), or HTML with font tags (option -html), or HTML output with CSS (option -css), or for LaTeX. The default is for terminal output.

If no file argument is given, it reads standard input. Output is always written to standard output.

HsColour can add named anchors (options -anchorHTML or -anchorCSS) to top-level definitions in the source file (functions, datatypes, classes). This enables you to make links to a specific location in the generated file with the standard file.html#anchor notation (e.g. from haddock-generated library documentation). See below for details.

Configuration of colours

If you use either -html, -tty, or -latex, you can configure the colours for different lexical entities by editing a configuration file called .hscolour in the current directory. (An example is included in the distribution.) For CSS output, it is sufficient to edit the hscolour.css file, also in the distribution.

The .hscolour file format is a simple Haskell value of type ColourPrefs, constructed using named fields, as follows:

  data ColourPrefs = ColourPrefs
    { keyword, keyglyph, layout, comment
    , conid, varid, conop, varop
    , string, char, number
    , selection, variantselection :: [Highlight]

  data Colour = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White

  data Highlight =
    | Bold
    | Dim
    | Underscore
    | Blink
    | ReverseVideo
    | Concealed
    | Foreground Colour
    | Background Colour

Use it as a library

If you want to incorporate hscolour-like functionality into your own Haskell program, it is now (from 1.4) also available as a library, thanks to Björn Bringert. The library is Cabal-ised, so just do the usual thing to install it as a package:
  • runhaskell Setup.hs configure
  • runhaskell Setup.hs build
  • runhaskell Setup.hs install
There is haddock documentation of the API.

Using HsColour with Haddock

Let's say you want to generate some pretty-coloured HTML versions of your source files, at the same time as you are generating library documentation using Haddock. Haddock (1.8 onwards, currently only available in darcs) has options to link the API docs to the source code itself. Here is a quick summary of the shell commands to use:
for file in $(SRCS)
do HsColour -anchorHTML $file >docs/`dirname $file'/`basename $file .hs`.html
haddock --html --title="My Library" --odir=docs   $(SRCS) \
    --source-module="src/%{MODULE/.//}.html" \

Copyright and licence

hscolour is © Malcolm Wallace 2003-2006. It is distributed under the Gnu GPL, which can be found in the file LICENCE-GPL.


HsColour is not yet able to add anchors to class methods, nor to foreign decls.


The Programatica project has a more sophisticated HTML syntax-highlighter. It hyperlinks every usage of an identifier to its definition, which is highly useful for browsing large amounts of code. However, it is a more heavyweight solution as well - requiring the entire front-end of a compiler not only to parse the Haskell code, but to chase all its module dependencies as well. As a consequence, you need source access to every definition used in your program, including the Prelude and all library packages...


added -latex output mode
move generated HTML anchors to before comments/typesigs
made available as a Cabal-ised library
added HTML and CSS anchors
added CSS output mode (from Neil Mitchell)
fixed compatibility with ghc-6.4
first release

This page last modified: 21st December 2006
Malcolm Wallace