14

I'd like to load the yaml-mode for any files named *.yml or *.yaml.

  • 7
    Read the Elisp manual, node Auto Major Mode, and you will be able to answer your own question: auto-mode-alist. Oh, I see you did that, 10 seconds after you posted the question. Maybe not a good idea to pump Q & A from the manual this way. But I guess it is a question that will occur to people, and the answer will help them. Too bad they will learn it this way, instead of learning it from Emacs itself. Better to teach people how to find such answers, IMO. – Drew Oct 3 '14 at 3:35
  • 1
    Feel free to add this as an answer. I asked this question because it was one I solved in the past, and it was very similar to a proposed question in the area51 list. – b4hand Oct 3 '14 at 15:45
  • 1
    No problem. Sorry if I sounded negative. As I said, the question and the answer will help people. – Drew Oct 3 '14 at 15:50
  • For what it is worth, this is very similar to a question that was asked during the proposal phase on area51: area51.stackexchange.com/proposals/76571/emacs/76640#76640 – b4hand Oct 3 '14 at 22:59
  • There's nothing wrong with asking such a question. My comment was originally just the first sentence. IMO, it is good for people to get in the habit of asking Emacs itself first, and asking SE second. Emacs is a bit special in helping users this way - it is not your average SE topic. But users often need help learning how to ask Emacs. – Drew Oct 4 '14 at 6:16
13

Add these lines to your .emacs file:

(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-mode))

The auto-mode-alist is a variable which emacs consults whenever a new file is opened. You can add mappings between filename patterns and major-modes.

You can find out more about how Emacs determines which modes to load for a given buffer from the Emacs manual.

  • 4
    "\\.ya?ml\\'" is the preferred form, since it's really the end of the string, not a newline. It actually makes a difference for file that have a newline in their name... 'foo.yaml\n.c' should really be a C file, not a yaml file (yes, it's pretty unlikely this will ever happen, but still) – Sigma Oct 3 '14 at 1:55
  • 2
    It would be helpful if you also linked to the emacs manual where this information is given. – Trevoke Oct 6 '14 at 13:31
  • Also the (require 'yaml-mode) is unnecessary if the mode is autoloaded (which is almost always the case). The library will be automatically loaded when you open a file with the correct file extension. – shosti Oct 7 '14 at 0:49
  • Since yaml-mode was installed as a package, I definitely needed the (require 'yaml-mode) line; otherwise, I would see the following error: File mode specification error: (void-function yaml-mode). – b4hand Oct 16 '14 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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