Nathan
Nathan

Reputation: 6225

How do I intelligently re-indent Clojure in Emacs?

I'm new to Clojure and new to Emacs.

Is there an Emacs short-cut to intelligently re-indent the whole file? if not, is there at least a way to indent selected regions left or right?

I feel like I'm back in the stone age repeatedly pressing the arrows

Upvotes: 26

Views: 8781

Answers (5)

Micah Elliott
Micah Elliott

Reputation: 10274

Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.

Upvotes: 0

Micah Elliott
Micah Elliott

Reputation: 10274

When you capture data from a sequence like C-u C-c C-e (cider-eval-last-sexp), the raw data output to your buffer can be unwieldy to inspect/work with. And the normal code-indenting commands (mentioned in answers here) don't handle it well.

For handling results from such evaluated expressions, try cider-format-edn-region.

As a concrete example, have you ever tried reformatting your ~/.lein/profiles.clj? This is pretty hard to do and keep consistent, until you discover cider-format-edn-region. Take caution that it will, however, remove any comments.

Upvotes: 3

fgui
fgui

Reputation: 1574

cider-format-buffer command (Since cider 0.9.0)

Upvotes: 8

user100464
user100464

Reputation: 18499

C-x h selects the entire buffer. C-M-\ reindents the selected region.

Upvotes: 43

joelittlejohn
joelittlejohn

Reputation: 11832

Ctrl-x, h (select all) followed by Tab (to indent)

Upvotes: 12

Related Questions