There are several questions here on indenting code in visual studio code (How do you format code in Visual Studio Code (VSCode)?), and on indenting OCaml code (How to indent existing OCaml code), but none of the answers work for indenting OCaml code in visual studio code.
I have installed the OCaml Platform visual studio code extension, vscode-ocaml-format (following https://dev.realworldocaml.org/install.html), ocaml-lsp-server (following https://ocaml.org/learn/tutorials/up_and_running.html) using opam, updated and upgraded everything, used eval $(opam env)
, but this still does not work. I'm a bit at a loss...

- 185
- 11
-
1Have you also installed the `ocamlformat-rpc` package with `opam install ocamlformat-rpc`? And also, make sure that you have `.ocamlformat` in the top folder of your project. – ivg Jan 24 '22 at 16:51
1 Answers
It is very hard to answer such questions as we need more debugging input from your side. On the other hand, it is so hard to get it from vscode so it is better just to walk you through the whole process from the very beginning to see where things might go wrong. I would suggest you follow the process for ease of debugging. After you have everything working you can adapt it to your particular setup.
- Create a fresh new folder and put some OCaml file into it, let's name it
test.ml
and let's put into it the following code,
let test = [
"hello"
]
- Now create a fresh local opam switch, by issuing in the same folder as the
test.ml
file the following command, (note the dot at the end of the command, it is required, it will create a local switch for you)
opam switch create .
- Next install the required dependencies.
opam install ocamlformat ocamlformat-rpc ocaml-lsp-server
Create the
.ocamlformat
file in the same folder as thetest.ml
file. It will tell ocamlformat that you want to be ocamlformatted and you can use this file to setup your preferences.Make sure that you have installed ocamlplatform for vscode
Now we are ready for the test. Start vscode and open
test.ml
. It will ask you to select the sandbox. Select the sandbox that corresponds to the folder where you puttest.ml
(it should be marked aslocal
and have the same name as the folder name, and there will be the full path to it, so it will be easy for you to find it). The code syntax should be highlighted and there should be no error messages from vscode. Finally, hitCtrl-Shift-I
to re-indent your file, it should transform your code to,
let test = [ "hello" ]

- 34,431
- 2
- 35
- 63
-
Thank you, this works ! But will I have to do this for every program ? How can I do this so that it works globally, without using "switch" and having a .ocamlformat in every directory ? In vs code, I can now select six sandboxes (global, local, test, 4.12, 4.13.1, default). How can I clean this to have only a single one for everything ? – user0 Jan 24 '22 at 20:10
-
1of course, you don't need to create a fresh compiler installation (aka sandbox aka switch) for each program :) Pick the global switch that you prefer, e.g., 4.13.1 (delete all others with `opam switch delete
`, use `opam switch – ivg Jan 24 '22 at 20:25` to switch between the sandboxes), then make sure that you have the above three packages installed in the global switch and select that switch/sandbox in vscode. You still have to create `.ocamlformat` manually in the top folder of your project. -
1Thanks a lot for these explanations about switches/sandboxes. It was the .ocamlformat file thing that I was missing. Documentation is less easy to find on all these tooling details. Thanks again ! – user0 Jan 24 '22 at 20:57
-
1