If you have a GPG-encrypted file, say notes.org.gpg, emacs figures out that the major mode has to be set to org-mode. This applies in general to any major mode out there; you do not need to modify the auto-mode-alist for each major mode to support that .gpg extension.

I would like to have my own extra extension, let's say .~bkp~ and have emacs ignore that when determining the major mode. Currently, if I have a file named notes.org.~bkp~, the org-mode is not set automatically.

I scoured through files.el, epa*.el, googled but couldn't figure out how emacs is ignoring the .gpg in the file names.

What function can I probably advice or a variable can I tweak to support my custom appended extensions?

1 Answer 1


The relevant entry in auto-mode-alist is this:

 ([...] ("\\.gpg\\(~\\|\\.~[0-9]+~\\)?\\'" nil epa-file) [...])

And here is the documentation that describes what is going on:

auto-mode-alist is a variable defined in files.el.

This variable may be risky if used as a file-local variable.

Documentation: Alist of filename patterns vs corresponding major mode functions. Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). (NON-NIL stands for anything that is not nil; the value does not matter.) Visiting a file whose name matches REGEXP specifies FUNCTION as the mode function to use. FUNCTION will be called, unless it is nil.

If the element has the form (REGEXP FUNCTION NON-NIL), then after calling FUNCTION (if it's not nil), we delete the suffix that matched REGEXP and search the list again for another match.

The extensions whose FUNCTION is archive-mode should also appear in auto-coding-alist with no-conversion coding system.

See also interpreter-mode-alist, which detects executable script modes based on the interpreters they specify to run, and magic-mode-alist, which determines modes based on file contents.


So there you go!

(add-to-list 'auto-mode-alist '("\\.bkp\\(~[0-9_]+~\\)\\'" nil backup-file))
  • Thanks. I might be missing something here. Your explanation makes sense and I tried to implement it but it does not work: (add-to-list 'auto-mode-alist '("\\.bkp~[0-9_]+~\\'" nil backup-file)). It does not set the mode for file.sv.bkp~20150619_1427~ correctly. May be a second set of eyes of the above regexp can see the problem? Jun 19, 2015 at 18:42
  • Even M-: (string-match "\\.bkp~[0-9_]+~\\'" (buffer-file-name)) (when I am in that file buffer) returns a non-nil value; but the major mode is still set to Fundamental mode. I also tried the same with a dummy notes.org.bkp~123~ file; same thing. Jun 19, 2015 at 19:21
  • Interesting.. It works with other extensions (i have a few in my auto-mode-alist) but I just tried your specific example and get the same behavior. The file also gets opened read-only, which seems odd. Must be some other handling of that extension somewhere?
    – glucas
    Jun 19, 2015 at 19:31
  • @glucas I searched for bkp in the C-h v buffer for auto-mode-alist and the list I added is the only one in that alist. Jun 19, 2015 at 19:41
  • 1
    Actually - what you are using is very close to what Emacs is already stripping off as version info. If I use a file name like foo.org.~bkp~1~ then Emacs visits that in org-mode without any additional rules in the auto-mode-alist. (Take a look at file-name-version-regexp.)
    – glucas
    Jun 19, 2015 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.