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

7

28

I never used Vi or Vim, but it seems that it is the best option to edit OCaml files. Unfortunately I am lost with so many things to care about: ocaml-vi-addon, vi-scripts, otags etc.

I broke my initial fear of Vim using cream, but I am in doubt if I need any package* other than vim-scripts.

  • -> I use Debian, maybe this info is useless
Bricklaying answered 20/3, 2013 at 3:46 Comment(2)
Emacs is another best option to edit OCaml files, unless you also have fear of Emacs.Breakdown
Yes, I have; and it is bigger than vim's. I swear I tried, but emacs definitely isn't for me.Bricklaying
M
29

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

Macruran answered 20/3, 2013 at 4:59 Comment(7)
You could also add ocp-indent to your answer.Khelat
Good suggestion, I actually do use ocp-indent.Macruran
Are they compatible with each other? Don't are they included in the package vim-scripts? Will vim-scripts be needed at all?Bricklaying
All of the the above are completely compatible. The easiest way to get going quickly with these is using Vundle.Macruran
@Macruran How do you switch between .mli and .ml files? I look for a.vim analog for OCaml.Psychophysiology
@Psychophysiology there's actually support for that in the default vim plugin for OCaml. I can't tell you the original key since I've rebound it but the function is OCaml_switchMacruran
@Psychophysiology merlin provides :ML Module_name and :MLI Module_name for navigation within a project as well.Nonplus
I
16

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.

Indoiranian answered 21/6, 2013 at 11:28 Comment(1)
To check if your version of vim supports the ocaml compiler type :compiler in your vim console. You will get a list of available compilers.Peaked
W
7

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

Whyte answered 17/10, 2020 at 13:3 Comment(0)
O
3

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.

Overbite answered 20/3, 2013 at 8:17 Comment(3)
annot functionality is actually built in to newer versions of vim's ocaml mode. It's bound to <leader>tMacruran
@Macruran That is what I am talking about when I say "I am lost" :)Bricklaying
How about 5? I was wondering if there is some plugin for vim to list the modules/names in the current file.Clamorous
B
1

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"
Brain answered 31/1, 2019 at 21:16 Comment(0)
C
1

Another alternative worth mentioning is Onivim a Vim-based editor which comes with out-of-the-box support for OCaml and ReasonML via ocaml-lsp.

Its front-end is actually written with OCaml (well, ReasonML) and it uses a fork of Vim (libvim) on the back-end. It's still in alpha and doesn't offer the full Vim experience yet, however, but I've already used it full-time for about a year myself.

It's also a commercial product, but free for non-commercial use and time-delayed open source.

(Full disclaimer: I used to work on Onivim, but at time of writing I do not)

Communal answered 17/10, 2020 at 13:43 Comment(0)
P
0

Omlet used to be a good mode for Vim: Author's page, vim.org. Event if there has been no new version since 2005, it might suit your needs. If you try it, tell us what you think!

Phoenicia answered 20/3, 2013 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.