OCaml ‘completion’ within Emacs with YASnippet and tuareg-mode

Hi,
Some days ago, I found YASnippet.
I then started using it and extending it.

As I currently develop mainly with OCaml (with tuareg-mode on Emacs), and that TABbing is much better (for me) than tuareg’s shortcut keys, I’ve created some snippets for tuareg-mode.

You can find them here : http://mestan.fr/ocaml/emacs/yasnippet-tuareg-mode.tar.gz

To install my snippets, assuming yasnippet is already installed (otherwise, see YASnippet’s page), you have to :
- Uncompress the tarball, getting a “tuareg-mode” folder;
- Move it in /snippets/text-mode/ (mine’s path is : ~/.emacs.d/plugins/yasnippet/snippets/text-mode/tuareg-mode/)
- To get YASnippet work with tuareg-mode, you must add the following to your .emacs (or any init file loaded when emacs starts) BEFORE yasnippet’s loading code :

1
2
3
(add-hook 'tuareg-mode-hook
         '(lambda ()
            (yas/minor-mode-on)))

Then reload your .emacs or restart Emacs.

Now, when typing leti then TAB within tuareg-mode (e.g when editing a .ml file), it should expand to

1
let | in

where | is emacs’s cursor.
Note : typing TAB when cursor is between let and in moves the cursor at in’s right.

Here is the current snippet list I’ve defined :

  • begin … end => begin
  • module … = functor (…) -> struct … end => functor
  • if … then … else …=> if
  • let … in … => leti
  • match … with … -> … | … -> … => match
  • module … = struct … end => module
  • module type … = sig … end => sig
  • try … with => try

where the part on the left side of “=>” is obtained after having typed the part on the right side of “=>” then TAB.

Feel free to use and customize it, I’ll be glad to hear from you if you use it (by commenting this entry or at alp [AT] mestan [DOT] fr).


One Comment

  1. [...] Few days ago, I posted about completion for OCaml programers within Emacs, the best editor ever (Ok, ok, “one of the best”), using YASnippets and tuareg-mode. Remember, it was here. [...]

Leave a Comment