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.


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).

  • You don’t need to define an extra function: (add-hook 'LaTeX-mode-hook (lambda () (electric-indent-local-mode -1))) suffices. Mar 5 '20 at 15:36
  • 1
    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
    Mar 6 '20 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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