Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up vim's identation for literate Haskell programming?

When I turn on autoindent for a regular *.hs file, the after pressing Enter the new line is indented as expected. However, this doesn't work with literate Haskell *.lhs files whose code lines start with > (AKA "bird-tracks"). The cursor is always positioned at the first column.

How can I set up vim so that when I'm inside a piece of code in a *.lhs file (and let\s say have autoindent on), pressing Enter creates a bird track at the new line and indents appropriately?

Update: To give an example, let's say have

> myfn x | x <= 0     = 0
>        | x == 1     = 1▌

where represents the position of the cursor (I hope you have no problem seeing the unicode character.) If I press Enter, I end up with

> myfn x | x <= 0     = 0
>        | x == 1     = 1
▌

whereas I want

> myfn x | x <= 0     = 0
>        | x == 1     = 1
>        ▌
like image 324
Petr Avatar asked Sep 02 '13 08:09

Petr


1 Answers

This should be easy to achieve with

:set formatoptions+=ro

or :se fo+=ro for short.

With r and o in 'formatoptions', Vim attempts to insert the comment "leader" including indentation on new lines inside a comment (that is, a non-comment in literate Haskell).

To make this setting automatically, set up an autocommand in your vimrc.

autocmd FileType lhaskell setlocal formatoptions+=ro

Tip: Use CTRL-U to remove the auto-inserted leader when you don't need it.

like image 162
glts Avatar answered Sep 29 '22 07:09

glts