Emacs Redux

Return to the Essence of Text Editing

Auto-indent Your Code With Aggressive-indent-mode

| Comments

One of the things I hate the most while programming, is having to manually adjust the indentation of some code, after I’ve moved or renamed something in it. While it’s pretty easy to do such re-indent operations using commands like crux-indent-defun or advices like crux-with-region-or-buffer (you remember, crux, right?), there’s an even more efficient way to tackle the issue at hand. Enter aggressive-indent-mode.

aggressive-indent-mode’s name is a bit of a misnomer - it should probably have been named auto-indent-mode, as this is what it does. When you edit your code it will adjust the indentation automatically. It’s easier to show this than to explain it.

Here’s one example showing agressive-indent-mode enabled in emacs-lisp-mode:

And another example using cc-mode:

Provided you’ve installed the mode, enabling it for particular major modes is a piece of cake:

1
2
3
(add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
(add-hook 'clojure-mode-hook #'aggressive-indent-mode)
(add-hook 'ruby-mode-hook #'aggressive-indent-mode)

If you want to enable it in all major modes you can do this as well:

1
(global-aggressive-indent-mode 1)

Note that this is not going to work well with modes like python-mode and haml-mode where the proper indentation can’t be reliably determined. When global-aggressive-indent-mode is enabled it will not affect major modes listed in aggressive-indent-excluded-modes.

For more info - head over to the project’s readme.

Comments