osa1 github gitlab twitter cv rss

Quick Vim Tip for OCaml programmers: Fix OCamlYacc comment syntax

February 14, 2013 - Tagged as: vim, ocaml, en.

I’ve been editing OCaml with Vim for several months now, one problem that annoys me with Vim’s built-in OCaml syntax highlighter is it behaves OCamlYacc files like it’s a normal OCaml file.

It generally works fine but OCamlYacc’s comment syntax is different from OCaml’s. When working on big OCamlYacc files this quickly becomes annoying.

I’m not proficient in VimL but I could manage to write a simple solution. I replaced this line in Vim’s built-int OCaml syntax highlighter:

" Comments 
syn region   ocamlComment start="(\*" end="\*)" contains=ocamlComment,ocamlTodo

with:

" Comments
if expand("%:e") == "mly"
  syn region   ocamlComment start="/\*" end="\*/" contains=ocamlComment,ocamlTodo
else
  syn region   ocamlComment start="(\*" end="\*)" contains=ocamlComment,ocamlTodo
end

(You can find you Vim’s built-in files’ folders by runing :echo $VIMRUNTIME inside Vim. On my machine, ocaml.vims path is /usr/share/vim/vim73/syntax/ocaml.vim)

Works great. It can be further simplified but requires a variable declaration and I don’t want to mess with Vim variables without understanding the consequences(scope rules etc.)