OCaml Local Development Environment Setup
Once you have completed the OCaml Setup, you should be ready to configure your development environment. A good development environment is important to efficiency: it will take longer to do the same amount of work if you don’t have good tools to help you. You are free to use whichever software you like, but the instructions in this guide will likely lead to a better OCaml development experience.
Your setup process will depend upon what kind of editor you are using. Your instructor uses and recommends Visual Studio Code, which is already installed on the department machines, but you are free to use other editors if you like. The instructions below largely concern configuring your editors to use some tools we installed during the OCaml setup, most notably a code analysis tool called Merlin.
(Note for Windows users: some of these instructions may not work for you depending upon how OCaml is installed on your system. Please see the instructor if you need any assistance.)
Visual Studio Code
If you are using the department machines, Visual Studio Code is already installed. Otherwise, please make sure that Visual Studio Code is installed before continuing.
Visual Studio Code already has a number of extensions available which make OCaml development relatively easy. You can install these extensions by using the extension manager in VSCode or by running the following command:
code --install-extension ocamllabs.ocaml-platform --install-extension zepalmer.ocaml-indentation
By default, autocompletion is bound to Ctrl+Space
.
Visual Studio Code uses the notion of a “command palette”: nearly anything you can do in the editor can be accessed by typing Ctrl+Shift+P
and then typing the name of the command into the fuzzy search bar. For instance, with an OCaml .ml
file open, we can type the word “format” to see commands such as “Format Document” and “Format Document With…”. Hovering over an option reveals a gear icon to the right of the menu; this icon allows you to set a keystroke to activate the command. For instance, your instructor has set “Format Document” to the keystroke Ctrl+Shift+I
to make it easy to automatically indent OCaml code.
Atom
If you are using the department machines, Atom is already installed. Otherwise, please make sure that Atom is installed on your computer before continuing.
Fortunately, existing packages make it fairly easy to use OCaml with Atom; we just need to install them. This can be accomplished using Atom’s own package manager from the command line:
apm install language-ocaml linter ocaml-indent ocaml-merlin
Once you have installed these packages, you can verify they are installed by restarting Atom, pressing Ctrl+,
(control comma), and clicking “Packages” on the left. You should see all of the above packages in the “Community Packages” section.
By default, autocompletion is bound to Ctrl+Space
.
The above packages do not configure every keystroke we want. We can configure them by editing ~/.atom/keymap.cson
file and adding the following text. (You will probably have to create this file from scratch.) Be sure to keep the indentation exactly as it appears here!
'atom-text-editor[data-grammar="source ocaml"]':
'ctrl-shift-t': 'ocaml-merlin:show-type'
'alt-shift-r': 'ocaml-merlin:rename-variable'
'ctrl-alt-f': 'ocaml-indent:file'
You can adjust the keystrokes if you like; these keystrokes are used by your instructor. They have the following effect in OCaml source files:
Ctrl+Shift+T
will show the type of the expression under the cursor (as long as your code is well-formed)Alt+Shift+R
will rename the variable under the cursor (replacing this occurrence as well as all other occurrences that bind to the same variable; this is not just a search and replace)Ctrl+Alt+F
will automatically indent your source code
Vim
Configuration for Vim is relatively easy: it should be enough to run the following command:
opam-user-setup install
Once this is finished, you should be able to test your configuration by:
- Running
vim test.ml
- Typing “
List.m
” - Pressing
Ctrl+X
followed byCtrl+O
to activate autocompletion
If Merlin setup was successful, you should see a list of the names and types of all of the functions in the List
module that begin with the letter “m”.
Documentation on the Vim commands and variables provided by this configuration can be found here.
Emacs
Configuration for Emacs is relatively easy; in fact, Emacs is a very popular choice among OCaml developers. It should be enough to run the following command:
opam-user-setup install
Once this is finished, you should be able to test your configuration by:
- Running
emacs test.ml
- Typing “
List.m
” followed by the tab key
If Merlin setup was successful, you should see a list of the names and types of all of the functions in the List
module that begin with the letter “m”.
Documentation on the Emacs commands and variables provided by this configuration can be found here.
GEdit and Sublime 3
The command
opam-user-setup install
indicates that it has some amount of support for GEdit and Sublime 3. You can try running that command to see what happens. Your instructor has no experience with these environments but will try to assist if you run into trouble.