home · projects · papers · blog · gallery · contact
anil madhavapeddy // anil.recoil.org

Intellisense for OCaml with Vim and Merlin

03 October 2013   |   Anil Madhavapeddy   |   tags: ocaml, vim, and realworldocaml   |   all posts

I’m finalizing the installation instructions for Real World OCaml and finally got around to configuring the awesome Merlin editor tool. I pretty much never configure my editor, so here are my notes on getting Merlin working with Vim completely from scratch on MacOS X (I’m on 10.9DP7, but this should work with earlier versions of MacOS X too).

First, install some basic plugin tools by following the Synastic installation instructions.

Install vim-pathogen:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -so ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim

Install vim-sensible for useful defaults:

cd ~/.vim/bundle
git clone git://github.com/tpope/vim-sensible.git

Install Syntastic:

cd ~/.vim/bundle
git clone https://github.com/scrooloose/syntastic.git

Follow the Merlin/vim from scratch directions:

opam install merlin

Finally, add this this in your ~/.vimrc to turn everything on.

execute pathogen#infect()
let s:ocamlmerlin=substitute(system('opam config var share'),'\n$','','''') .  "/ocamlmerlin"
execute "set rtp+=".s:ocamlmerlin."/vim"
execute "set rtp+=".s:ocamlmerlin."/vimbufsync"

Trying it out

Read the excellent instructions at the Merlin for Vim from scratch wiki page. A few immediately useful things I did are below.

Displaying types: You can get the local type of something by typing in \t anywhere. This is the <LocalLeader> for anyone who has customized their setup (it is mapped to backslash by default).

Tab completing functions: You can tab-complete module signatures by pressing Ctrl-X-O (make sure you keep Ctrl pressed the whole time). This activates vim’s OmniComplete mode. To get this to work with your external projects, create a .merlin file. I use this for ocaml-dns for example.

PKG lwt cstruct
S lib
S lib_test
S lwt
B _build/lib
B _build/lib_test
B _build/lwt

Also, don’t forget to pass the -bin-annot function to the compiler to generate the typed-AST cmt files. You can add true: bin_annot to your _tags file if you’re using OCamlbuild (in OCaml 4.01.0 or higher, and it’ll be silently ignored on previous versions so you don’t need to worry about breaking older OCaml compilers).

 
blog comments powered by Disqus