1

Currently, in my .emacs.d/init.el, I have:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

(push (cons "\\*shell\\*" display-buffer--same-window-action) display-buffer-alist)

(when (fboundp 'electric-indent-mode)
  (electric-indent-mode -1))

The last line does what I like, but it also turns it off in C mode, where I prefer it to be on. How can I make it so it's only off in fundamental mode, when I'm editing .txt or .md files or the like?

1 Answer 1

1

The global electric-indent-mode does not provide an exclusion list where you could add fundamental-mode and txt-mode.

Use electric-indent-local-mode in major mode hooks instead of the global electric-indent-mode.


The first "normal" approach is to switch the minor-mode actively on in all major-modes you want it to have: Call electric-indent-local-mode in the hooks of the major modes where you want to switch it on:

(with-eval-after-load 'electric
  (electric-indent-mode -1))

(add-hook 'c-mode-hook #'electric-indent-local-mode)

Alternative to c-mode-hook you can also use prog-mode-hook to switch electric-indent-mode on in almost all modes for programming languages.


The second version is to call electric-indent-local-mode in after-change-major-mode-hook if the major-mode is not in some exclusion list.

(with-eval-after-load 'electric
  (electric-indent-mode -1))

(defcustom my-electric-indent-exclusion-list '(txt-mode fundamental-mode)
  "Major modes where `electric-indent-local-mode' is not switched on."
  :group 'my
  :type '(repeat symbol))

(defun my-switch-on-electric-indent ()
  "Maybe switch on `electric-indent-local-mode'.
Do not activate electric indent if the major mode is
in `my-electric-indent-exclusion-list'."
  (unless (apply #'derived-mode-p my-electric-indent-exclusion-list)
    (electric-indent-local-mode)))

(add-hook 'after-change-major-mode-hook #'my-switch-on-electric-indent)

As Drew indicated in his comment, if it was not for fundamental mode, there would be another simple method to disable electric-indent-mode for specific major modes.

For most minor modes that have a global and a local version and where there is not an explicite exclusion list, it is possible to switch on the global version and switch it off with the local minor mode version in the major-modes hook.

This cannot be used for fundamental-mode since there is no fundamental-mode-hook variable.

I demonstrate it here for text-mode:

(electric-indent-mode)

(add-hook 'text-mode-hook 
      (lambda ()
        (electric-indent-local-mode -1)))
5
  • Good answer. Maybe worth pointing out that there's no mode hook for fundamental-mode. (If there were, then there would be a simpler answer.)
    – Drew
    Commented Apr 22, 2023 at 13:57
  • I realized that the third line in my init.el was what was causing the behavior I did not like. Regardless, you put a lot of thought and effort, and your answer worked with that line included. I'd like to note that prog-mode instead of c-mode-hook did not. I will accept your answer. Commented Apr 22, 2023 at 20:34
  • @user129393192 Sorry, the statement about prog-mode was not very clear. I meant that you need to replace c-mode-hook with the hook for prog-mode which is prog-mode-hook. I just tried and electric-indent-local-mode in prog-mode-hook really turns on electric-indent-mode in c-mode. I modified the relevant sentence. Now it should be crystal clear.
    – Tobias
    Commented Apr 22, 2023 at 21:00
  • @Drew You wrote: "Maybe worth pointing out that there's no mode hook for fundamental-mode. (If there were, then there would be a simpler answer.)". Do you mean the method that I added now to the answer? Thanks for the comment. I think that last bit completes the answer. BTW, IMHO global minor modes should provide exclusion lists for their users. Something that is easy to customize.
    – Tobias
    Commented Apr 22, 2023 at 21:18
  • @Tobias: Yes, that's what I meant. And yes, some way of specifying excluded modes would be good. Maybe submit an enhancement request: M-x report-emacs-bug.
    – Drew
    Commented Apr 23, 2023 at 3:10

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.