Good editor here support comes from two tools, ocp-indent and Merlin. Ocp-indent is a utility for indenting OCaml files (the ocp is from OCaml Pro, the company that made it), and Merlin supplies modern IDE features (auto-completion and the like) for OCaml. For more on using Merlin with Vim also see this page. Because most OCaml programmers use either Emacs or Vim, the OCaml community has naturally added ocp-indent and Merlin support to them first.
This page is about getting Vim set up on a Unix-style system (especially OS X), and some rudimentary use.
The following directions are not the only way to do this, the linked web pages for the various tools explain in detail the various options for installation. Nor are they guaranteed to work: depending on the state of your computer – which packages, and package managers it has installed, etc. – they might fail. So regard them as a guide to one easy-ish way to get everything up and running, but not as a foolproof recipe to unthinkingly follow.
I assume that you know how to use the command line and Vi or Vim at a basic level. The WWW is full of Vim tutorials, including an online interactive tutorial.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Use Homebrew to install the software.
brew install ocaml
brew install opam
brew install macvim
OS X comes with a terminal-only Vim, which is fine, but the MacVim version is better: it acts like a Mac application with mouse support, cut and paste, etc.
Make sure that you have a recent OCaml, 4.02 or higher. 4.02.3 is current as of the writing of this page.
OPAM puts everything in the
.opam subdirectory of your
If you already have OPAM, you can update it with
update and upgrade all of the packages you've already installed
opam upgrade. In either case, you might want to do this
in the future.
opam install merlin
opam install ocp-indent
opam install ppx_deriving
opam install extlib
mkdir -p ~/.vim/autoload ~/.vim/bundle
Put this file into the ~/.vim/autoload directory.
git clone https://github.com/def-lkb/ocp-indent-vim.git
git clone https://github.com/scrooloose/syntastic.git
git clone https://github.com/tpope/vim-sensible.git
ocp-indent-vim specialises ocp-indent to work with Vim. It is recommended over the default setup from OPAM in this generally useful blog post. Syntastic checks for syntax errors whenever you save your file. Vim-sensible gives a modern, basic configuration for Vim; it gets Vim into a state where the other extensions will work.
let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
execute "set rtp+=" . g:opamshare . "/merlin/vim"
let g:syntastic_ocaml_checkers = ['merlin']
Type a simple OCaml program into it.
let x = 1
Move the cursor over x and type
\t. At the bottom of the
screen the type
int of x should be displayed.
In a program with a type error (
let x = 1 1), a red arrow
should appear in the left column when the file is saved.
Lastly, check which indenter is running.
The result should be
indentexpr=ocpindent#OcpIndentLine(). Any answer not
referring to OcpIndent indicates that the (poor) Vim default OCaml
indenter is running. This shouldn't happen, but it did when I was setting
everything up for myself. Although I don't know how to fix it properly,
you can hack around it be removing the default indenter from Vim. Use
:scriptnames command in Vim to check which scripts are
running. Look for the ones named
There should be the good one in
~/.vim/bundle/ocp-indent-vim/indent/ocaml.vim, and a bad one
somewhere else inside of your Vim installation (in my case in
Simply move the bad one out of the way.