18

I don't like the way electric indent mode works in latex-mode. Everywhere else (at least everywhere I use), it's great.

How can I permanently disable electric indent mode for latex mode only, but keep it everywhere else?

I'm guessing it's a one-line lisp hack in my config file, or something like that, but I'm horrible at lisp, so I can't figure it out.

1
  • 1
    If you want to disable electric indent in a single latex file, then add % -*- electric-indent-inhibit: t -*- at the top and run M-x normal-mode.
    – ntc2
    Commented Sep 14, 2022 at 6:31

1 Answer 1

16

The command you need is electric-indent-local-mode. You can use this to turn off electric-indent-mode in any buffer by calling it manually: M-x electric-indent-local-mode. This is a toggle, so calling it again in the same buffer turns it back on again.

To do this automatically from your init file, you need to set up a hook. First, define the hook:

(defun remove-electric-indent-mode ()
  (electric-indent-local-mode -1))

Then add it to the appropriate mode hooks. To turn off electric-indent-mode for the LaTeX mode provided by AUCTex, use this:

(add-hook 'LaTeX-mode-hook 'remove-electric-indent-mode)

For the default texmode, use:

(add-hook 'tex-mode-hook 'remove-electric-indent-mode)

The same pattern holds for any other mode you want to turn off electric indentation.

For more details on mode hooks, see the built-in Emacs manual node [(emacs) Hooks][1]. You can get there from Emacs via C-h i r m Hooks <enter>: C-h for help, i for info, r for read the manual, m for menu item, and Hooks to pick the menu item. (or you can follow the link above to see the html version).

2
  • 1
    You don’t need to define an extra function: (add-hook 'LaTeX-mode-hook (lambda () (electric-indent-local-mode -1))) suffices. Commented Mar 5, 2020 at 15:36
  • 2
    True, but if you define a function, you can reuse it in multiple different hooks without duplicating code. It's also easier to remove a defined function than an anonymous one. That can be useful when debugging
    – Tyler
    Commented Mar 6, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.