15

How to disable auto indent in Emacs globally or only for some modes?

I have a number of packages installed for RubyOnRails (ruby, html, js, css).

Let's say I want to disable autoindent for css-mode.

msorc
  • 907
  • 1
  • 7
  • 20

3 Answers3

8

For me, on emacs 24.x, M-xelectric-indent-mode toggled the behavior that I wanted to disable.

FWIW, the behavior was that RET was bound to the command newline which is defined in simple.el... Among other things, the behavior of that command is altered by electric-indent-mode.

daveloyall
  • 2,140
  • 21
  • 23
  • 2
    Next time my enter key doesn't do what I want, I'll remember to just `C-h k RET` to see what function/command it is bound to. – daveloyall Mar 01 '16 at 23:56
4

You may want to look for variable names containing the word electric. (This is the common Emacs parlance for actions which occur automatically when particular visible characters are typed.)

In this instance, M-x apropos-variable RET electric RET shows me that there is a css-electric-keys variable containing a list of "Self inserting keys which should trigger re-indentation."

You could use M-x customize-variable RET css-electric-keys RET to set this list to nil, or add (setq css-electric-keys nil) to your init file.

Sometimes a minor mode is used to implement electric behaviours, so that you can switch them on and off more easily. Those would likely be found via M-x apropos-command RET electric RET, and you would probably use a major mode hook to ensure that the electric minor mode was disabled, in a similar fashion to this:

(add-hook 'MAJORMODE-mode-hook 'my-MAJORMODE-mode-hook)
(defun my-MAJORMODE-mode-hook ()
  (ELECTRICMODE-mode 0))
phils
  • 71,335
  • 11
  • 153
  • 198
  • How do you find out what terms to use in your last ELECTRICMODE-mode example? I'm having trouble turning off autoindenting in JavaScript files and I haven't had any luck with your other suggestions for JS, unfortunately. – Malvineous Sep 23 '11 at 06:17
  • 4
    My best suggestion is to get as familiar as you can with the self-documenting nature of Emacs, and commands which revolve around this facility. For javascript mode I used `M-x find-function RET javascript-mode RET` to open the source file which defines it, and then simply searched for "auto" and quickly found an appropriate hit in this variable: `C-h v js-auto-indent-flag RET` (which happens to be customizable, so you could also have found it by using `M-x customize-group RET js RET` ("javascript" would have been nicer, but "js" was a fairly obvious second-guess). – phils Sep 23 '11 at 11:14
  • Similarly, `M-x apropos-variable RET indent RET` would (after the mode was loaded) discover the `js-auto-indent-flag` variable. As a general recommendation, you may find it useful to read through the manual's section on Help: `M-: (info "(emacs) Help") RET`. Once you have a decent handle on how to find things like this, customisation becomes a much easier process. – phils Sep 23 '11 at 11:18
0

In my case (TypeScript + React through web-mode) the variable causing indent on RET was web-mode-enable-auto-indentation - had to toggle it from t to nil.

I guess, in general you may probably want to check all variables with indent in their names that are t. Or rather start looking from RET binding as others suggest.