How to turn off electric-indent-mode for specific Major mode?

electric-indent-mode will be enabled by default in 24.4. To turn it off locally, you will be able to use electric-indent-local-mode as mentioned by lunaryorn. But to turn it off locally in 24.3, you can do:

(add-hook 'foo-mode-hook
          (lambda () (set (make-local-variable 'electric-indent-mode) nil)))

You mentioned that the first form didn't work for you, but it should (i.e. if it doesn't, it's because of the some other problem).


At least on emacs 24.3 you cannot disable electric indent mode locally, since it is a global-mode. Anyways the issue with yaml-mode is that the electric-indent functionality is built into it i.e. it will be enabled even without electric-indent-mode. The package does not provide a way to turn this behaviour off, maybe you should file an issue on its github repo.

Try this to disable the electric-indent functionality in yaml-mode

(define-key yaml-mode-map "|" nil)
(define-key yaml-mode-map ">" nil)
(define-key yaml-mode-map "-" nil)
(define-key yaml-mode-map "." nil)
(define-key yaml-mode-map [backspace] nil)

To restore the electric-indent behaviour afterwards, you can do

(define-key yaml-mode-map "|" 'yaml-electric-bar-and-angle)
(define-key yaml-mode-map ">" 'yaml-electric-bar-and-angle)
(define-key yaml-mode-map "-" 'yaml-electric-dash-and-dot)
(define-key yaml-mode-map "." 'yaml-electric-dash-and-dot)
(define-key yaml-mode-map [backspace] 'yaml-electric-backspace)

With a recent Emacs (probably Emacs snapshot only) you can use electric-indent-local-mode, e.g.:

(add-hook 'yaml-mode-hook (lambda () (electric-indent-local-mode -1)))

If your Emacs lacks this function, you can still sort of disable the mode via electric-indent-functions, e.g.

(add-hook 'yaml-mode-hook
          (lambda ()
             (add-hook 'electric-indent-functions
                            (lambda () 'no-indent) nil 'local)))

And in either case, you may probably want to restore C-j, via

(add-hook 'yaml-mode-hook 
          (lambda () (local-set-key (kbd "C-j") #'newline-and-indent)))