17

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.

  • 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 Sep 14 '22 at 06:31

1 Answers1

15

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

Tyler
  • 21,719
  • 1
  • 52
  • 92
  • 1
    You don’t need to define an extra function: `(add-hook 'LaTeX-mode-hook (lambda () (electric-indent-local-mode -1)))` suffices. – Emil Jeřábek Mar 05 '20 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 Mar 06 '20 at 16:51