Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the current setup to use OCaml in Vim? [closed]

Tags:

vim

ocaml

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.


One wants support from his editor for these reasons:

  1. better presentation via syntax highlighting/folding
  2. better automatic indentation
  3. spot errors earlier via background compilation, syntax highlighting (again) or by any other mean
  4. inline debugging
  5. introspection to list available names/methods, see an object type, and so on.

For 1 and 2, the default should be good enough, although there is this ocp-indent plugin that try to do a better job.

For 3 I resort on syntax highlighting, and do not know anything better for vim.

With regard to inline debugging, I never managed to make use of ocamldebug from the command line so never tried from the editor.

Regarding introspection, I can't recommend enough the annot program, that can be installed as a vim plugin as explained in the README.

Once installed you can get the type of anything under the cursor with only two keystrokes, which is very convenient for a language capable of inferring the most complex types.


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


There is a really nice guide at https://github.com/ocaml/merlin/wiki/vim-from-scratch

Since Stack Overflow does not appreciate link only answers, here are the commands, but really - visit that page for insights and context.

opam install merlin

Add this to .vimrc:

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