Using Emacs with Clojure


The Emacs editor can be integrated with Clojure to make Clojure more convenient and interactive. Make a directory/folder cs378 for your CS 378 files.

Emacs Command Summary

  1. Copy the file z.emacsclj from the class directory to your cs378 directory; then do mv z.emacsclj .emacs . (If you already have a .emacs, you can name the file something else and load it manually as described below.)

  2. Start Emacs. On the Linux workstations, it is under Applications - Accessories - Gnu Emacs (GUI).

  3. If you renamed your file to be .emacs, it will load automatically. Otherwise, within Emacs, enter M-x load file followed by Return, then complete the file path with cs378/z.emacsclj and Return. M-x or Meta-x is Alt while typing x or Esc followed by x .

  4. Enter M-x lisp mode followed by Return.

  5. Enter M-x run lisp followed by Return. Now you are running Clojure as a sub-process of Emacs.

  6. You can use Emacs commands to help edit your input to Clojure:

    1. Emacs will help indent using Tab at the start of a line, and it will show where parentheses balance.

    2. You can copy-paste a function definition and edit it before entering Return with the pointer at the end of the function definition to send the definition to Clojure.

    3. You can re-execute previous lines of input to Clojure by putting the pointer at the end of a line and entering Return.

  7. A good way to work is to have two emacs windows, one for source code and one for execution. You can use
    (load-file "myfile.clj") in the execution window to reload your code as you modify it; you can simply put the cursor and the end of the (load-file ...) and hit Return to reload.