Configuring vim For Good Haskell Style


So, you have decided to use vim to write some Haskell? Congratulations on a fine choice! Here are a few tips to help you stick to good Haskell style.

Eliminating tabs

In order to have your tab key generate the appropriate the appropriate number of spaces rather than a tab character, add this to .vimrc:

set expandtab

If you have a file that already contains tabs then you can convert them to spaces by typing :retab. You may need to set your tabstops correctly first, with e.g. :set ts=8.

Eliminating trailing whitespace

If you have a file that contains trailing whitespace then you can remove it all by typing :%s/ \+$//.

Highlighting bad whitespace

If you create .vim/after/syntax/haskell.vim with the following contents, then any tab characters and trailing space will be highlighted for you:

syn cluster hsRegions add=hsImport,hsLineComment,hsBlockComment,hsPragma
syn cluster hsRegions add=cPreCondit,cCppOut,cCppOut2,cCppSkip
syn cluster hsRegions add=cIncluded,cDefine,cPreProc,cComment,cCppString

syn match tab display "\t" containedin=@hsRegions
hi link tab Error
syn match trailingWhite display "[[:space:]]\+$" containedin=@hsRegions
hi link trailingWhite Error