clj-refactor — Unleash your Clojure wizard.02 Nov 2015, by Artur Malabarba.
When I first started learning Clojure, I was charmed by how well integrated CIDER was with Emacs. In many ways, it felt just like writing Emacs-lisp. Nowadays, that feeling has gone slightly past the goal mark, and there are actually features I miss when I’m writing elisp. Clj-refactor is one of them.
This package is a Swiss knife of refactoring utilities for Clojure. From simple
operations like turning a form into a
-> thread, to the more complex
situations like renaming entire namespaces, it’s hard to find something
clj-refactor can’t do.
I won’t list all of them here, they already have a nice wiki page for that. The point here is to expose a few features (the ones I find most useful) and hopefully that’ll get you started on the road to enlightenment. To see some of the features in play (at a pretty quick pace), you can check out Magnar’s Parens of the dead videos. I also plan on doing a proper and thorough video review of this package in the future.
First, and most importantly, are the threading commands. These allow you to
instantly turn something like
(filter odd? (map inc (range n))) into
or back again. Clj-refactor default keybinds are pretty well thought out, but I use these commands so frequently that I felt the need to shorten them to just 2 or 3 keys.
Next is another feature that comes up a lot, but this one doesn’t even take up a
keybind. When you type
/, clj-refactor will look at the alias you’ve just
written, and try to add a require for it in the
ns form. The current snapshot
release is very smart about it, and actually learns what alias you use for each
namespace by looking at other files in the same project. You can also define
your preferences with the
The nice thing about the default keys is that they’re mnemonic, and you might
remember I have an entire series on this topic. The point being: it’s very easy
to remember that
cljr-create-fn-from-example is bound to C-c r f e when you
can actually sing “clojure refactor from example” as you’re typing.
A few others I use a lot (and the keys they’re bound to) are
move-to-let (m l), and
promote-function (p f).
Lastly, some miscellaneous configurations that simply reflect my personal preferences, but which you should be aware of nonetheless.