Configuring Your Editor
While the toplevel is great for interactively trying out the language, we will shortly need to write OCaml files in an editor. We already installed the tools required to enhance Merlin, our editor of choice with OCaml support. Merlin provides all features such as "jump to definition," "show type," and ocaml-lsp-server
, a server that delivers those features to the editor through the LSP server.
OCaml has plugins for many editors, but the most actively maintained are for Visual Studio Code, Emacs, and Vim.
Visual Studio Code
TL;DR Install the packages
ocaml-lsp-server
andocamlformat
in your opam switch.
For VSCode, install the OCaml Platform Visual Studio Code extension from the Visual Studio Marketplace. The extension depends on OCaml LSP and OCamlFormat. To install them in your switch, you can run:
opam install ocaml-lsp-server ocamlformat
Upon first loading an OCaml source file, you may be prompted to select the toolchain in use. Pick the version of OCaml you are using, e.g., 5.1.0
from the list.
Editor Features at Your Disposal
If your editor is setup correctly, here are some important features you can begin using to your advantage:
1) Hovering for Type Information
This is a great feature that let's you see type information of any OCaml variable or function. All you have to do is place your cursor over the code and it will be displayed in the tooltip.
Ctrl + Click
2) Jump to Definitions With If you hold down the Ctrl key while hovering, the code appears as a clickable link which if clicked takes you to the file where the implementation is. This can be great if you want to understand how a piece of code works under the hood. In this example, hovering and Ctrl + Clicking
over the peek
method of the Queue
module will take you to the definiton of the peek
method itself and how it is implemented.
Ctrl + Shift + P
3) OCaml Commands With Pressing the key combination Ctrl + Shift + P opens a modal dialog at the top. If you type the word ocaml
, you will be presented with a list of various OCaml commands at your disposal which can be used for different purposes.
Windows Users
If you used the DkML distribution, you will need to:
1. Go to File
> Preferences
> Settings
view (or press Ctrl ,
)
2. Select User
> Extensions
> OCaml Platform
3. Uncheck OCaml: Use OCaml Env
. That's it!
Emacs
Using Emacs to work with OCaml requires at least two modes:
- A major mode, which, among other things, supports syntax highlighting and the structuring of indentation levels
- A minor mode, which will interact with a language server (such as
ocaml-lsp-server
ormerlin
). In this tutorial, we will focus on using the newocaml-eglot
mode andocaml-lsp-server
as a server.
Choosing a major mode
There are several major modes dedicated to OCaml, of which the 3 main ones are:
- Tuareg: an old-fashioned (but still updated), very complete mode, usually the recommended one
- Caml: a mode even older than
tuareg
(but still updated), lighter thantuareg
- Neocaml: a brand new mode, based on modern tools (like tree-sitter). Still experimental at the time of writing.
For the purposes of this tutorial, we are going to focus on the use of tuareg
as the major mode, but you should feel free to experiment and choose your favourite one! To use tuareg
, you can add these lines to your Emacs configuration:
(use-package tuareg
:ensure t
:mode (("\\.ocamlinit\\'" . tuareg-mode)))
use-package
Melpa and If your version of Emacs does not support the use-package
macro (or is not set up to take MELPA packages into account), please update it and follow these instructions to install use-package
and MELPA.
LSP setup for OCaml
Since version 29.1
, Emacs has had a built-in mode for interacting with LSP servers, Eglot. If you are using an earlier version of Emacs, you will need to install it this way:
(use-package eglot
:ensure t)
Next, we need to bridge the gap between our major mode (in this case, tuareg
) and eglot
. This is done using the ocaml-eglot
package:
(use-package ocaml-eglot
:ensure t
:after tuareg
:hook
(tuareg-mode . ocaml-eglot)
(ocaml-eglot . eglot-ensure))
And that's all there is to it! Now all you need to do is install ocaml-lsp-server
and ocamlformat
in our switch:
opam install ocaml-lsp-server ocamlformat
You are now ready to edit OCaml code productively with Emacs!
Finer configuration
OCaml-eglot can be finely configured, the project README gives several configuration paths to adapt perfectly to your workflow. You will also find there an exhaustive presentation of the different functions offered by the mode.
Getting Type Information
Opening an OCaml file should launch an ocaml-lsp
server, and you can convince yourself that it's working by using, for example, the ocaml-eglot-type-enclosing
command (or using the C-c C-t
binding) on an expression of your choice:
OCaml-eglot README provides a comprehensive overview of all the functions available in this mode!
Vim
For Vim, we won't use the LSP server but rather directly talk to Merlin.
opam install merlin
After installing Merlin above, instructions will be printed on how to link Merlin with your editor. If you do not have them visible, just run this command:
opam user-setup install
Talking to Merlin
Getting Type Information
- In the Vim editor, press the Esc to enter command mode.
- Place the cursor over the variable.
- Type
:MerlinTypeOf
and press Enter. - The type information will be displayed in the command bar. Other Merlin commands for Vim are available and you can checkout their usage on the Merlin official documentation for Vim.
Help Improve Our Documentation
All OCaml docs are open source. See something that's wrong or unclear? Submit a pull request.