I'd like to load the yaml-mode
for any files named *.yml
or *.yaml
.
Asked
Active
Viewed 4,274 times
30
-
7Read the [Elisp manual, node `Auto Major Mode`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Auto-Major-Mode.html#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 03 '14 at 03:35
-
1Feel 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 03 '14 at 15:45
-
1No problem. Sorry if I sounded negative. As I said, the question and the answer *will* help people. – Drew Oct 03 '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: http://area51.stackexchange.com/proposals/76571/emacs/76640#76640 – b4hand Oct 03 '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 04 '14 at 06:16
1 Answers
28
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.

b4hand
- 1,995
- 1
- 19
- 31
-
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) – Sigma Oct 03 '14 at 01:55
-
2It would be helpful if you also linked to the emacs manual where this information is given. – Trevoke Oct 06 '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 07 '14 at 00: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)`. – b4hand Oct 16 '14 at 21:52