I added the following line to my init file to replace ruby-mode with enh-ruby-mode:

(setq auto-mode-alist
       (lambda (x)
         (if (eq (cdr x) 'ruby-mode)
             (cons (car x) 'enh-ruby-mode)
           x)) auto-mode-alist))

But it works only for the first file. The first ruby file I open is in enh-ruby-mode. But in the process the following command is executed:


(add-to-list 'auto-mode-alist
             (cons (purecopy (concat "\\(?:\\.\\(?:"

(The same line is in /usr/share/emacs/26.3/lisp/loaddefs.el.) Which means the second and the following files are opened in ruby-mode.

I guess to remedy this I've got to not change the line in auto-mode-alist, but add one. Anyways, how does this autoload thing work? When does loaddefs.el get executed? Is there a way to keep the line out of the file?

  • This looks like a land grab to me: it should be up to the users to set auto-mode-alist as they see fit. I would open a bug report. In the meantime, you can force the autoload with (require 'ruby-mode) in your init file and then hack auto-mode-alist to your heart's content.
    – NickD
    Commented Jul 23, 2020 at 2:38
  • @NickD Can you possibly explain how it works? Without requiring ruby-mode, how does emacs know that it has to update auto-mode-alist? Okay, probably from loaddefs.el. But is it a generated file? When does the generation happen? When is loaddefs executed? And why does requiring ruby-mode solve it?
    – x-yuri
    Commented Jul 23, 2020 at 9:55
  • My guess is that when emacs is built, ruby-mode's add-to-list call is copied to loaddefs.el. As such when emacs starts ruby-mode is set up to be used for ruby files. But it also provides for ruby-mode function to be autoloaded. Then when I change auto-mode-alist, something triggers the function, rube-mode.el gets loaded, and the line is back in auto-mode-alist.
    – x-yuri
    Commented Jul 24, 2020 at 8:05
  • @NickD You might want to check out my answer. Also, if you're going to file a bug report, please add a link here afterwards.
    – x-yuri
    Commented Jul 24, 2020 at 9:06
  • I don't use ruby or ruby-mode or enh-ruby-mode, so I'm not going to file a bug report.
    – NickD
    Commented Jul 24, 2020 at 16:49

1 Answer 1


The thing can be reproduced with expand-region package installed.

From what I can see, when enh-ruby-mode gets activated, expand-region loads enh-ruby-mode-expansions.el. But that in its turn loads ruby-mode-expansions.el, which requires ruby-mode.el.

As a result, when you open a ruby file, enh-ruby-mode gets activated, but still ruby-mode.el is also loaded.

So, if I change auto-mode-alist in my init file, then when I open a ruby file, ruby-mode.el gets executed, and adds the association with ruby-mode back.

To avoid that one has to require ruby-mode.el in advance (before opening ruby files).

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.