Setting Up OCaml Indentation in Vim with ocp-indent
Vim’s default OCaml indentation is mediocre at best. The ocp-indent tool provides intelligent, syntax-aware indentation that respects OCaml conventions and integrates cleanly with Vim and Neovim.
Installing ocp-indent
Install via opam:
opam install ocp-indent
This installs the indentation engine and Vim plugin support. Verify the installation:
which ocp-indent
Configuring Vim
Add this to your ~/.vimrc or ~/.config/nvim/init.vim:
augroup ocaml_config
autocmd!
autocmd BufNewFile,BufRead *.ml,*.mli setfiletype ocaml
autocmd FileType ocaml call SetupOCaml()
augroup END
function! SetupOCaml()
setlocal textwidth=80
setlocal colorcolumn=80
setlocal shiftwidth=2
setlocal tabstop=2
setlocal softtabstop=2
setlocal expandtab
filetype indent on
filetype plugin indent on
" Load ocp-indent
let opamshare = system('opam var share | tr -d "\n"')
if isdirectory(opamshare)
let ocp_indent_path = opamshare . '/vim/syntax/ocp-indent.vim'
if filereadable(ocp_indent_path)
execute 'source ' . ocp_indent_path
endif
endif
endfunction
Key points:
- Sets filetype detection for
.mland.mlifiles - Uses 2-space indentation (OCaml standard)
- Expands tabs to spaces with
expandtab - Safely loads ocp-indent only if the file exists
- Uses
augroupto prevent duplicate autocommands on reload
For Neovim with plugin managers
If using lazy.nvim:
{
'ocaml/vim-ocaml',
ft = { 'ocaml' },
config = function()
vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2
vim.opt_local.expandtab = true
end,
}
Or with vim-plug:
Plug 'ocaml/vim-ocaml'
Using ocp-indent
Once configured, indentation works automatically:
| Action | Command |
|---|---|
| Current line | == in normal mode |
| Visual selection | Select + = |
| Entire file | gg=G |
| Paragraph | =ap |
| Automatic | Triggers when you press Enter or type structural keywords (if, let, match, etc.) |
You can also reindent the entire buffer with:
:0,$!ocp-indent
This pipes the buffer through ocp-indent directly, useful for reformatting imported code.
Configuring ocp-indent behavior
Create ~/.ocp/ocp-indent.conf to customize rules:
JaneStreet
indent_with_tabs = false
indent_comments = true
strict_comments = false
Available profiles:
normal— Standard OCaml indentationapprentice— Conservative indentationJaneStreet— Jane Street style guide (good default for most projects)
Troubleshooting
ocp-indent not loading:
Check that opam is initialized:
eval $(opam env)
Verify the opam share path exists:
opam var share
If empty, run:
opam init
Conflicting indentation settings:
Remove any duplicate indentation configs in your vimrc. Ensure filetype plugin indent on appears after loading ocp-indent. Check for conflicting plugins with:
:scriptnames
Indentation still looks wrong:
Verify ocp-indent is being sourced:
:echo exists('*ocp_indent#indent')
Should return 1. If 0, the plugin didn’t load. Check the file exists at:
cat $(opam var share)/vim/syntax/ocp-indent.vim
For Neovim specifically:
If using a plugin manager, ensure the vim-ocaml plugin loads before other OCaml-related plugins. Check LSP client settings aren’t overriding indentation behavior.
2026 Best Practices and Advanced Techniques
For Setting Up OCaml Indentation in Vim with ocp-indent, understanding both the fundamentals and modern practices ensures you can work efficiently and avoid common pitfalls. This guide extends the core article with practical advice for 2026 workflows.
Troubleshooting and Debugging
When issues arise, a systematic approach saves time. Start by checking logs for error messages or warnings. Test individual components in isolation before integrating them. Use verbose modes and debug flags to gather more information when standard output is not enough to diagnose the problem.
Performance Optimization
- Monitor system resources to identify bottlenecks
- Use caching strategies to reduce redundant computation
- Keep software updated for security patches and performance improvements
- Profile code before applying optimizations
- Use connection pooling and keep-alive for network operations
Security Considerations
Security should be built into workflows from the start. Use strong authentication methods, encrypt sensitive data in transit, and follow the principle of least privilege for access controls. Regular security audits and penetration testing help maintain system integrity.
Related Tools and Commands
These complementary tools expand your capabilities:
- Monitoring: top, htop, iotop, vmstat for system resources
- Networking: ping, traceroute, ss, tcpdump for connectivity
- Files: find, locate, fd for searching; rsync for syncing
- Logs: journalctl, dmesg, tail -f for real-time monitoring
- Testing: curl for HTTP requests, nc for ports, openssl for crypto
Integration with Modern Workflows
Consider automation and containerization for consistency across environments. Infrastructure as code tools enable reproducible deployments. CI/CD pipelines automate testing and deployment, reducing human error and speeding up delivery cycles.
Quick Reference
This extended guide covers the topic beyond the original article scope. For specialized needs, refer to official documentation or community resources. Practice in test environments before production deployment.

Update: newer version of `ocp-indent` changes the location of the ocp-indent.vim script. Updated the .vimrc config to reflect this change.
Hello, Could you explain the third part in more detail? Specifically:
(* I’m not clear on what the significance of this instruction is *)
“You can use == to format the code selected or the current line now.”
(* how do you “add” the script? *)
“Third, to make ocp-indent be automatically invoked by autoindent, add the ocp-indent-vim script. This ensures that ocp-indent will indent the current line after you input Enter, “if”, “else”, etc. You can use tools like pathogen to manage the installed plugins in Vim.”
Kindly,
John
> About “You can use == to format the code selected or the current line now.”
Pressing `=` twice in command mode will make Vim help us indent the code (if it was not indented correctly).
> About “add”ing the script:
Pathogen is a Vim plugin manager. Now with latest Vim, I suggest to use Vim’s built-in package management instead: `:help packages`.