There are two options that can be set to run arbitrary programs to format text (code). When they aren't set, vim will work with lots of other (often complex, and often somewhat hacky) options to indent or format your text.
These options are
equalprg (trigged by
formatprg (triggered by
gq). Technically one is for indenting and the other for formatting, but since most formatting programs do both these days you may not care; pick the one you use the most.
It's not specified for
equalprg, but the program you use for either (take input on standard in, write formatted output to standard out).
So in the case of haskell-formatter, you would do
gq to fix your code. You may find that some programs don't handle single lines too well; in that case, using
gqap tends to work better.
gggqG take care of the whole file, but at that point it may be easier to run
You could technically drop these lines in an ftplugin file, too, but this seems a bit cleaner to me.
An alternative, if you need to always reindent/reformat the whole file:
nnoremap <buffer> = :execute '%!' &l:equalprg<CR>
nnoremap <buffer> == :execute '%!' &l:equalprg<CR>
nnoremap <buffer> = gg=G
" ... you get the idea
You could similarly remap
gqq) and use
Make sure to set
b:undo_ftplugin if you care about such things.
If you want to restore cursor position/etc., I would use