Skip to content

Instantly share code, notes, and snippets.

@dbuenzli
Last active August 11, 2022 00:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbuenzli/a797e398cb3f6503b6e0b5f34249648a to your computer and use it in GitHub Desktop.
Save dbuenzli/a797e398cb3f6503b6e0b5f34249648a to your computer and use it in GitHub Desktop.
Emacs setup for ocaml

Install the following opam packages:

opam install caml-mode merlin ocp-indent

Tweak your .emacs file with some or all of the following:

; shift tab to complete
(global-set-key (kbd "S-<tab>") 'company-complete)

; Shell, needed to get correct env
(setq shell-command-switch "-lc")
(setq explicit-bash-args '("--login" "-i"))

; locate opam's site-lisp
(setq opam-share
      (substring (shell-command-to-string
                  "opam config var share 2> /dev/null") 0 -1))
(add-to-list 'load-path (expand-file-name "emacs/site-lisp" opam-share))

; Compilation mode
(require 'compile)
(setq compilation-scroll-output 'first-error)
(setq compilation-always-kill t)
(setq next-error-highlight t)

; Company mode
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
(define-key company-active-map [tab] 'company-complete-selection)
(setq company-idle-delay nil)

; caml-mode
(add-to-list 'auto-mode-alist '("\\.ml[iylp]?$" . caml-mode))
(autoload 'caml-mode "caml" "Major mode for editing OCaml code." t)
(autoload 'run-caml "inf-caml" "Run an inferior OCaml process." t)
(autoload 'camldebug "camldebug" "Run ocamldebug on program." t)
(add-to-list 'interpreter-mode-alist '("ocamlrun" . caml-mode))
(add-to-list 'interpreter-mode-alist '("ocaml" . caml-mode))
(if window-system
    (progn
      (require 'caml-font)
      (set-face-foreground 'caml-font-doccomment-face "#cb4b16")))

; merlin
(require 'merlin)
(require 'caml-types)
(require 'merlin-company)
;(add-to-list 'company-backends 'merlin-company-backend)
(add-hook 'caml-mode-hook 'merlin-mode t)
(setq merlin-use-auto-complete-mode 'easy)
(setq merlin-command 'opam)
(setq merlin-error-on-single-line t)

; ocp-indent
(require 'ocp-indent)
(setq ocp-indent-path
     (concat
      (replace-regexp-in-string "\n$" ""
          (shell-command-to-string "opam config var bin")) "/ocp-indent"))
(setq ocp-indent-config "strict_with=always,match_clause=4,strict_else=never")
@syntakker
Copy link

Title should read "Emacs setup for ocaml " :-)

@dbuenzli
Copy link
Author

You are ritgh !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment