I have ~30.000 lines of badly indented OCaml code (including mly and mll files) and would like to indent them. I tried googling for variations of 'ocaml indent' the closest I can get is to use Omlet.vim and indent the code one line at a time (via C-f in insert mode). Is there a way to do the indentation for all the 30000 lines?
4 Answers
I use Emacs as my editor with this package installed:
http://caml.inria.fr/pub/docs/u3-ocaml/emacs/index.html
To indent existing code you need to highlight it and then use the key combination C-M-\
You can script this per file pretty easily and I find the indentation to be pretty good.

- 7,834
- 11
- 55
- 85
-
1You can do the same thing in tuareg-mode with `M-q`. Don't know how it scales to thousands of lines though. – Chris Conway Dec 18 '09 at 04:36
-
@Chris, you're correct. I did use tuareg when I started learning OCaml and I do think it's more popular than the package hosted on the inria site. I just happen to prefer the package I posted. But take that with a grain of salt as I'm not the most polished OCaml programmer around;) – chollida Dec 18 '09 at 15:52
When I want to re-indent a whole file in vim, I use the following key sequence:
g g V G =
Breaking this down for you, g g moves the cursor to the beginning of the file. V enters visual mode. G selects to the end of the file. = indents the selected lines.
This should be much faster than your method of applying indentation line by line, but yet will use the same rules (warning: using Omlet, indenting a whole file may take a while).

- 12,034
- 15
- 57
- 79
Now, one can use ocp-indent (opam install ocp-indent) and invoke it like this :
ocp-indent bad-indented-ocaml.ml > well-indented-file.ml

- 4,346
- 1
- 12
- 25
Use ocp-indent
tool for this. Here is an example Makefile
rule, that will automatically indent you ml
files.
.PHONY: auto-ocp-indent
auto-ocp-indent: *.ml
for mlfile in $^; do ocp-indent -i $$mlfile; done

- 34,431
- 2
- 35
- 63