OCaml+Vim+OS X

by Scott Owens

Modern language, ancient editors

There are two editors with good support for OCaml: Emacs and Vim. Some other editors have support for OCaml syntax highlighting and automatic indenting – of sometimes dubious accuracy – and you can get by with that. In fact, everyone used to have to get by with mediocre editor support, and it wasn't that terrible; its only in the past few years that Emacs and Vim have done better. In the future, other editors will probably have good support too, but not at the present.

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.

Getting set up

The general plan is to first install the OCaml compiler, OPAM package manager, and the Vim editor on your system, then to get ocp-indent, Merlin, and other useful OCaml libraries installed, and lastly to install Syntastic for Vim and build a good Vim configuration.

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.

  1. Use your system's package manager to install OCaml, OPAM, and Vim if they aren't already there. This page details the various ways to install OCaml and OPAM on various systems. On OS X you should use the Homebrew package manager, which can be installed by running the following in a terminal.

    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.

  2. If you just installed OPAM, initialise it:

    opam init

    OPAM puts everything in the .opam subdirectory of your home directory.

    If you already have OPAM, you can update it with opam update and upgrade all of the packages you've already installed with opam upgrade. In either case, you might want to do this in the future.

  3. Use OPAM to install Merlin and ocp-indent (or otherwise follow the installation directions on their web pages).

    opam install merlin
    opam install ocp-indent

  4. Install useful OCaml packages ppx_deriving and extlib. ppx_deriving supplies automatic generation of boilerplate functions for printing and comparing values of user-defined types. extlib is a more complete standard library than the rather minimal one that comes with the OCaml compiler.

    opam install ppx_deriving
    opam install extlib

  5. Setup Pathogen. Pathogen makes it easy to add extension to Vim, you just put the extension into ~/.vim/bundles.

    mkdir -p ~/.vim/autoload ~/.vim/bundle

    Put this file into the ~/.vim/autoload directory.

  6. Install useful Vim utilities into the ~/.vim/bundle directory that we just set up. All of these are hosted on Github.

    cd ~/.vim/bundle
    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.

  7. Setup a good ~/.vimrc file. This is mine. At a minimum it needs the following four lines:

    execute pathogen#infect()
    let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
    execute "set rtp+=" . g:opamshare . "/merlin/vim"
    let g:syntastic_ocaml_checkers = ['merlin']

  8. Check that everything worked. Make a new file test.ml and edit it with Vim.

    touch test.ml
    mvim test.ml

    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.

    :set indentexpr

    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 the :scriptnames command in Vim to check which scripts are running. Look for the ones named SOME_PATH/indent/ocaml.vim. 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 /usr/local/Cellar/macvim/7.4-76/MacVim.app/Contents/Resources/vim/runtime/indent/ocaml.vim). Simply move the bad one out of the way.


Validate HTML