I'd like to load the yaml-mode
for any files named *.yml
or *.yaml
.
1 Answer
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.
-
5"\\.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)– SigmaOct 3, 2014 at 1:55
-
2It would be helpful if you also linked to the emacs manual where this information is given.– Trevoke ♦Oct 6, 2014 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.– shostiOct 7, 2014 at 0:49 -
1Since
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)
.– b4handOct 16, 2014 at 21:52
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.