Emacs for Clojure - Part 2

This is the second in a two part post about a Clojure programmer workflow entirely within Emacs.

Editing Clojure

Some useful navigation key bindings in Clojure-mode, actually any Lisp code editing mode in Emacs, are as follows:

Keybinding Command
C-M-f forward-sexp
C-M-b backward-sexp
C-M-a beginning-of-defun
C-M-e end-of-defun
C-M-x slime-compile-defun
C-x C-e slime-eval-last-expression

Some of these key bindings get redefined when a buffer is in slime-mode to SLIME enhanced equivalents, but mostly they behave the same.

And, don't forget the exponential effect of the C-u prefix key.

Some other key bindings that are also useful are:

Keybinding Command Doc
C-M-q indent-sexp A lot of times when copying and pasting or otherwise modifying large blocks of s-expressions, the indentation of the code can get out of whack. indent-sexp can help restore the balance.
C-M-h mark-defun  
C-M-k kill-sexp  

Also worth knowing, the magic of dynamic abbrevs bound to the M-/ key binding. Dynamic abbrevs are a quick way to complete a long function or var name from a minimal prefix. It's very brute force (i.e, just searches for a match in all the open buffers), but since it's very fast, it comes in handy when you're working with partially evaluated Clojure code.

Clojure REPL

Everything begins with a Clojure instance which has SWANK loaded. Again, there are lots of ways of starting one of these, and the most common use case is with a Leiningen project setup. Setting up Leiningen is beyond the scope of this post, but the docs on Leiningen's github page are quite helpful in getting you started.

Once Leiningen is setup and you have a project.clj file for your project, you can invoke clojure-jack-in.

Once SLIME is connected, it's helpful to know the following commands:

Keybinding Command Doc
  slime-repl This is a quick way to jump to the *slime-repl clojure* buffer.
  slime-reset When your SLIME connection goes out of whack.
C-M-i slime-complete-symbol  
C-x e slime-eval-last-expression Makes every Clojure buffer into a REPL. Plus, it is very handy when iterating on tests.
C-c C-c, C-M-x slime-compile-defun This is convenient for compiling a defn or other top-level form, without having to put the cursor at the end of the expression.
  slime-list-connections If you find yourself having to connect to multiple SWANK servers this command is helpful in switching between them.
  slime-list-threads Show the list of scheduled JVM threads, and can provides an interactive way to kill running threads. Use with caution.

Emacs for Clojure - Part 1

This is a brief overview of the way I work in Clojure using Emacs - almost exclusively. Obviously, Emacs is not the only way to interact with Clojure, but it can be a pretty seamless and efficient environment for Clojure, once you become familiar with the powerful extensibility and "mouse-free" efficiency of Emacs.

Also, since Emacs uses Lisp as its extension language, there's a lot of value for a Clojure/Lisp developer in learning and mastering the Emacs' ecosystem. I hope this series of posts will be helpful in that endeavour.

There are lots of "getting started with Emacs" docs available online and on the web. The easiest one is the built-in Tutorial, which you can also bring up within Emacs with C-h t or M-x help-with-tutorial. Other online references:

  1. A Guided Tour of Emacs
  2. A Tutorial Introduction to GNU Emacs

For the most part, I'm going to assume you're using Emacs default key bindings, but the following customizations don't really depend on that.

Process

My typical work-flow involves the following steps (but not necessarily in the same order always):

(loop []
  ;; Edit code/tests
  ;; Interactive development with REPL and tests
  ;; Look up documentation
  ;; Search and Navigate a code tree
  ;; Run batch tests
  ;; Merge commits and resolve conflicts in Git
  (recur))

I'll try to cover each of these steps in detail over the course of the next few posts.

Clojure Mode Setup

Like snowflakes and Sting albums, there are myriad ways of setting up the Clojure environment in Emacs, but essentially all of them should be doing something similar to the following:

(defun clojure-mode-hook ()
  (setq indent-tabs-mode nil
        clojure-mode-use-backtracking-indent t)
  (setup-clojure-indentation-rules))
(require 'clojure-mode)
(require 'clojure-test-mode)
(require 'swank-clojure)
(add-hook 'clojure-mode-hook 'clojure-mode-hook)
(add-hook 'clojure-mode-hook 'clojure-test-mode)

If you are using Emacs 24 and have never setup Clojure-mode or SLIME before, check out the emacs-starter-kit.

Basic Editing

It's good to get familiar with the basic cursor movement keys such as:

Keybinding Command
C-n next-line
C-p previous-line
C-f forward-char
C-b backward-char
C-a move-beginning-of-line
C-e move-end-of-line

There's been a lot of cursing and teeth gnashing about Emacs' default key bindings, but here are some of the benefits that I've come to appreciate over time:

  1. Clustering – you can insert and move around with minimal physical movement from the home row of the keyboard.
  2. Easy to remember
  3. Work in bash and even on tty devices (never say never) - this is particularly useful if you're planning to do some remote pairing over tmux or screen

And, there is always C-h b describe-bindings which will list the key bindings in any given buffer.

Other useful basic key bindings:

Keybinding Command Doc
C-l recenter redraw buffer with the current line at the vertical center of the window; one useful variation is to C-u 0 C-l which will redraw the buffer with the current line at the top of the window
C-SPC set-mark-command set the beginning of a copy/cut region, and move the cursor to the end of the region
C-w kill-region cut the contents of the selected region
M-w kill-ring-save copy (not cut) the contents of the selected region
C-y yank paste the most recently copied/cut region at cursor point
M-y yank-pop when used after a yank, Emacs will replace the pasted region with older copied text cycling through the cut/copy history
C-x r k kill-rectangle mark a region as you would normally, but cut a rectangular sub-region. Useful when working with columns of data
C-x r y yank-rectangle paste the most recently killed rectangular region

Keyboard Macros

We've all been in situations where we've had to fix up some literal data structure or otherwise munge code by repeating a set of editing keystrokes, either deleting, inserting or otherwise splicing text. Keyboard macros can quickly help automate some of this work.

The basic principle is that you begin recording a macro C-x ( kmacro-start-macro, perform the keyboard actions, and then end recording with C-x ) kmacro-end-macro. You can then replay the macro using C-x e kmacro-end-and-call-macro. Also, check out C-x C-k n kmacro-name-last-macro for oft repeated macros.

Next post: REPL, SLIME and interactive programming.