Which is the current setup to use OCaml in Vim?

The answers are quite old, I figured this thread requires an update as the introduction of LSP as a de-facto standard have recently revolutionized the old text editors (vim, emacs...etc).

LSP (IDE-like features like autocompletions, refactoring etc)

The current state-of-the-art is to use the official OCaml Language Server: https://github.com/ocaml/ocaml-lsp

You can use coc.nvim for Vim8+ and Neovim and configure it to use ocaml-lsp: https://github.com/neoclide/coc.nvim/wiki/Language-servers#ocaml-and-reasonml

Or, instead of coc.nvim, you can use the latest Neovim nightly (0.5+) as a client for the OCaml Language Server: https://github.com/neovim/nvim-lspconfig#ocamllsp

Improved syntax highlighting

Use vim-ocaml to improve syntax highlighting: https://github.com/ocaml/vim-ocaml

Alternatively vim-ocaml is embedded in vim-polyglot, so you don't need to install vim-ocaml separately: https://github.com/sheerun/vim-polyglot

Bonus

This is a nice video explaining where the community is heading for the following years regarding tooling and the OCaml platform in general: https://www.youtube.com/watch?v=E8T_4zqWmq8&t

Bonus 2: vimrc and init.vim

My init.vim, compatible as a .vimrc too: https://github.com/nicobao/setup/blob/master/vim/init.vim

Great vimmer's .vimrc/init.vim: https://github.com/awesome-streamers/awesome-streamerrc


The default mode for OCaml is all there is to it really. You could consider using the following plugins:

https://github.com/scrooloose/syntastic - syntax checking

https://github.com/def-lkb/merlin - auto completion

https://github.com/jpalardy/vim-slime - repl integration

https://github.com/OCamlPro/ocp-indent - code formatting


Put these lines in your ~/.vimrc file:

filetype indent on
filetype plugin on
au BufRead,BufNewFile *.ml,*.mli compiler ocaml
syntax on

Then you get some nice shortcuts:

  • \s switches between the .ml and .mli file
  • \c comments the current line / selection (\C to uncomment)
  • % jumps to matching let/in, if/then, etc (see :h matchit-install)
  • \t tells you the type of the thing under the cursor (if you compiled with -annot)

Also, Vim can then parse the output of the compiler and jump to the correct location.

Tags:

Vim

Ocaml