Return to the Essence of Text Editing

A Peek at Emacs 24.4: Auto-indentation by Default

I’ve written in the past about
electric-indent-mode,
which was added in Emacs 24.1. In Emacs 24.4 one of the most prominent
user visible changes is that it’s enabled out-of-the box. That’s a
huge step towards the “modernization” of Emacs and one of the bigger
changes to the defaults in recent times. Let’s review briefly how the
mode works with a couple of Ruby examples (| signifies the cursor
position). Without electric-indent-mode:

1

defsomething|

After you press Return you’ll get:

12

defsomething|

With it:

1

defsomething|

After you press Return you’ll get:

12

defsomething|

Nice, ah?

One problem with electric-indent-mode is that it doesn’t play nice
with some (mostly third-party) modes (yaml-mode, slim-mode,
etc). I guess the situation will improve over time, but for now you
can simply disable the mode in such problematic cases:

1

(add-hook'yaml-mode-hook(lambda()(electric-indent-local-mode-1)))

Note that electric-indent-local-mode was introduced in Emacs 24.4.

If you want to make a major mode electric-indent aware, have a look at
the documentation of electric-indent-functions and
electric-indent-chars.

P.S.

Dmitry Gutov recently wrote
more on the topic
in the context of ruby-mode in Emacs 24.4.