When creating a new buffer via:

  1. C-x b
  2. NonExistentBufferName RET RET

I would like Emacs to ask me what major mode this buffer is going to use upon opening. Moreover, I want Emacs to infer the major mode in some cases based on the buffer's name:

  1. C-x b
  2. test.org(no such buffer so far) RET RET => org-mode is used without asking me anything.

Could anybody advice if I can use any package for this? Or is it achievable at all via pure elisp?

P.S. I'm using ido if it matters.

  • I've migrated this over to emacs.SE. Flag for merger with your other question. Please don't cross post the next time.
    – Bhargav Rao
    May 11, 2019 at 22:56

1 Answer 1


Add entries in the magic-mode-alist/auto-mode-alist (in your init file most likely) if you want emacs to automatically determine major mode from buffer/file names (C-h v auto-mode-alist for info).

To interactively set the major mode just use M-x <mode-name, eg. c-mode>.

If you actually want emacs to interactively prompt you for a major mode every time you open a buffer, that could be achieved via a small advice to function you use to visit files (the ido functions in your case).

  • auto-mode-alist seems to be working only with file buffers. Yeah, I'm looking for a way to have Emacs prompt me for a major mode. I tried to find the right function to hook it up May 11, 2019 at 19:48
  • @DmitrySenin yea that is for file buffers, perhaps magic-mode-alist would meet your needs. Otherwise, you could write a small function for completing read of available modes and advise the finding function
    – Rorschach
    May 11, 2019 at 19:55
  • @Rorschach I am trying to have emacs prompt me for one of two modes for a specific file extension, and magic-mode-alist seems to be what I am after. How would the assigned function have to be? Thanks in advance.
    – Ajned
    Apr 16, 2022 at 8:41

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.