Some Handy Electric Modes

Bozhidar Batsov is blogging up a storm on his new Emacs Redux blog. The other day, he mentioned a couple of useful modes that, since Emacs 24.1, are built in. The first is electric-pair-mode. It provides the automatic pairing of special delimiters such as ", (, [, and {. Much like paredit, when you type the left character of the pair, the right character is also inserted and the point is placed between them. Typing the right character merely skips over the already inserted right character rather than inserting a new one. You can insert just a single character by preceding it with the quote sequence 【Ctrl+q】.

I didn't realize this was a built-in and have been meaning to install the package. It was a nice surprise to learn that all I needed to do was enable it.

The second electric mode is electric-indent-mode. It's purpose is to automatically indent the next line after you type certain characters such as a newline. I already have 【Return】 mapped to newline-and-indnet so this is less useful to me and I haven't enabled it. You may find it useful, though. Be aware that it reportedly causes problems with Python mode so be alert.

This entry was posted in General and tagged . Bookmark the permalink.
  • David Bjergaard

    I would like to point out that electric-ident-mode scratches a major itch I've had regarding open-line: namely that open-line's default behavior is to simply insert a newline without re-indenting the line below.
    Example (| is the cursor):

    |int NewVar=0;

    Pressing M-o will result in:

    int NewVar=0;

    With electric-indent-mode enabled the result is:

    int NewVar=0;

  • Comparing electric parens (or autopair) to paredit is very unfair. Paredit's scope is so far beyond simple pair insertion that it's not even "apples and orranges", it's "microbes and super-inteligent aliens". Though I'm not a huge fan of paredit's philosophy, it's a big inspiration for my SP package, and so I feel obliged to point this out.

    I urge everyone to give it a look, try to use it for 3 days and then switch to smartparens ;D (or not, whatever you like is fine).

    • jcs

      I wasn't so much comparing electric-pair-mode to paredit as saying that the function electric-pair-mode provides (roughly, inserting special symbols in pairs) is much like the analogous function that paredit provides (along with many others, of course).

      For what it's worth, I have a post on smartparens coming up.

  • I had a quick look at the built-in electric-pair-mode when I saw that post too, but I ended up abandoning it. I've been using (except where paredit is used), mainly because of one additional feature: if you have the following situation, "(|)", and you backspace, autopair deletes the closing paren as well while electric-pair leaves you with "|)".