Trouble setting up vim for use with OCaml 5.0.0

In recognition of OCaml 5.0.0 getting much-improved parallelism, I decided to give OCaml another try.

Going through Get Up and Running With OCaml · OCaml Tutorials I got:

$ opam user-setup install
below cmd output started 2022 Sat Dec 17 04:34:48 PM PST
Opam plugin "user-setup" is not installed. Install it on the current switch? [Y/n] y
The following actions will be performed:
  - install user-setup 0.7

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> retrieved user-setup.0.7  (https://opam.ocaml.org/cache)
-> installed user-setup.0.7
Done.

<><> user-setup.0.7 installed successfully ><><><><><><><><><><><><><><><><><><>
=> To setup or update your editors, run 'opam user-setup install'.

<><> Carrying on to "opam user-setup install" <><><><><><><><><><><><><><><><><>

[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
[WARNING] var was deprecated in version 2.1 of the opam CLI. Use opam var instead or set OPAMCLI environment variable to 2.0.
ocamltop > base > .ocamlinit: adding configuration
vim > .vimrc: already exists, not installing base template
vim > base > .vimrc: manual changes: leaving as is
vim > ocp-indent > .vimrc: adding configuration
above cmd output done    2022 Sat Dec 17 04:35:09 PM PST
dstromberg@tp-mini-c:~ x86_64-pc-linux-gnu 2670

$ vi ~/.vimrc
below cmd output started 2022 Sat Dec 17 04:36:58 PM PST
Error detected while processing /home/dstromberg/.vimrc:
line  231:
E121: Undefined variable: s:opam_available_tools
E116: Invalid arguments for function count(s:opam_available_tools,"ocp-indent") == 0
Press ENTER or type command to continue

I tried Googling for “Undefined variable: s:opam_available_tools”, but didn’t find anything relevant-looking.

What do I need to do to set up my .vimrc for use with OCaml?

Thanks!

1 Like

For now I personally only rely on syntax highlighting and autocomplete and am quite happy with the minimal neovim setup from https://www.rockyourcode.com/setup-ocaml-with-neovim

My setup finally is 2 config lines: https://code.mro.name/mro/dotfiles/src/master/vim/.config/nvim/init.vim

I guess your vim may need some plugin(s) to make sense of .vimrc line 231.

1 Like

The warnings you’re seeing come from this change to which a PR has been put up for user-setup but not merged yet.

Could you provide a snippet from your ~/.vimrc that includes how opam_available_tools is assigned a value?

PS. This might be overkill for what you are trying to achieve but a member of the community put together a good write-up on his vim setup here: NeoVim setup in Lua for OCaml

1 Like

I don’t think my ~/.vimrc has opam_available_tools set to anything. And IINM, that’s the problem, but I don’t know what should be setting it.

The added lines look like just:

" ## added by OPAM user-setup for vim / ocp-indent ## 30bd5f8afb57b9f71e92b9f9bb2671a8 ## you can edit, but keep this line           
if count(s:opam_available_tools,"ocp-indent") == 0                                                                                   
  source "/home/dstromberg/.opam/default/share/ocp-indent/vim/indent/ocaml.vim"                                                      
endif                                                                                                                                
" ## end of OPAM user-setup addition for vim / ocp-indent ## keep this line                                                          
1 Like

personally I would purge those 3 lines and occasionally run :!ocp-indent --inplace % manually from within vim.

I wound up uncommenting what I used last time I looked (very briefly) at OCaml:

" OCaml!                                                                                                                             
" This is from https://github.com/ocaml/merlin/wiki/vim-from-scratch :                                                               
let g:syntastic_ocaml_checkers = ['merlin']                                                                                          
" ## added by OPAM user-setup for vim / base ## 93ee63e278bdfc07d1139a748ed3fff2 ## you can edit, but keep this line                 
let s:opam_share_dir = system("opam config var share")                                                                               
let s:opam_share_dir = substitute(s:opam_share_dir, '[\r\n]*$', '', '')                                                              
                                                                                                                                     
let s:opam_configuration = {}                                                                                                        
                                                                                                                                     
function! OpamConfOcpIndent()                                                                                                        
  execute "set rtp^=" . s:opam_share_dir . "/ocp-indent/vim"                                                                         
endfunction                                                                                                                          
let s:opam_configuration['ocp-indent'] = function('OpamConfOcpIndent')                                                               
                                                                                                                                     
function! OpamConfOcpIndex()                                                                                                         
  execute "set rtp+=" . s:opam_share_dir . "/ocp-index/vim"                                                                          
endfunction                                                                                                                          
let s:opam_configuration['ocp-index'] = function('OpamConfOcpIndex')                                                                 
                                                                                                                                     
function! OpamConfMerlin()                                                                                                           
  let l:dir = s:opam_share_dir . "/merlin/vim"                                                                                       
  execute "set rtp+=" . l:dir                                                                                                        
endfunction                                                                                                                          
let s:opam_configuration['merlin'] = function('OpamConfMerlin')                                                                      
                                                                                                                                     
let s:opam_packages = ["ocp-indent", "ocp-index", "merlin"]                                                                          
let s:opam_check_cmdline = ["opam list --installed --short --safe --color=never"] + s:opam_packages                                  
let s:opam_available_tools = split(system(join(s:opam_check_cmdline)))                                                               
for tool in s:opam_packages                                                                                                          
  " Respect package order (merlin should be after ocp-index)                                                                         
  if count(s:opam_available_tools, tool) > 0                                                                                         
    call s:opam_configuration[tool]()                                                                                                
  endif                                                                                                                              
endfor                                                                                                                               
" ## end of OPAM user-setup addition for vim / base ## keep this line                                                                

It might be a good idea to make A First Hour with OCaml · OCaml Tutorials work smoothly though - I imagine with the release of OCaml 5.0.0, there will be some newcomers (like me).

For formatting, ocp-indent is… still working but (afaik) not maintained, or in a bug-fixes-only mode.

My recommendation is to remove ocp-indent, and use ocamlformat instead. It works “out of the box” and allows for some tweaks. Add a .ocamlformat file to your project root and insert

profile = default
version = 0.24.1

to the file. Then you can format your entire project easily. Dune also has ocamlformat support. Personally i have mapped localleader F to format the current buffer (i use neovim, and bind to vim.lsp.buf.format()).

Edit. You could have a look at LSP instead of binding to merlin. LSP has all kinds of goodies, and neovim has an builtin LSP client. You just need to install the lsp server for ocaml. If you are on vanilla vim CoC is a good alternative. But there are a few other client implementations too.

1 Like

this looks like a tooling rabbit hole to me. I focussed on the language and never used what you struggle with.

I’m sorry if that derails the discussion further, since it actually doesn’t fix the initial problem :slight_smile: But I can second the recommendation for using the OCaml LSP, especially with Neovim.

My current configuration has almost no OCaml specific settings anymore and mainly follows the init.lua of the kickstart config repo. It is a “ready-to-go” initial configuration with all the (LSP) bells and whistles.

All you would need to do, is adding ‘ocamllsp’ to line 340.

And don’t forget to install all the recommended OCaml development packages, like the lsp and ocamlformat. Get Up and Running With OCaml · OCaml Tutorials

  1. One of the Neovim core devs also created an accompanying video for the kickstart repo recently, which explains the reasoning behind some of the default settings:
    Effective Neovim: Instant IDE - YouTube
1 Like