Emacs: How to Associate a File with a Major Mode

Perm url with updates: http://xahlee.org/emacs/emacs_auto-activate_a_major-mode.html

Emacs: How to Associate a File with a Major Mode?

Xah Lee, 2008-07, 2011-07-29

This page tells you how emacs determine what major mode to load when you open a file, and how you can change the setup.

Note that when you install a new package, some has the file association setting code within the package, while others ask you to put a few lines in your emacs init file instead.

How Emacs Determines Which Major Mode to Load

Emacs determines what mode to activate by the following mechanisms, in order. If a match is found, the process stops.

  1. Look for a special emacs-specific syntax in the file. For example: if first line in the file contains -*- mode: xyz-*-, emacs will load “xyz-mode”. This is from a general mechanism for emacs to load elisp variables. (See: (info "(emacs) File Variables").) This has the top priority, but this mechanism is not the usual way for programing language files to associate with a major mode.
  2. Check the first line in the file for unix “shebang” syntax (e.g. #!/usr/bin/perl) and match it with interpreter-mode-alist.
  3. Trys to match first line text with magic-mode-alist. (As of emacs 23.2.1, by default this list is empty.)
  4. Match the file name with auto-mode-alist.

(info "(emacs) Choosing Modes")

If you are installing a new package, or want to modify loading one of your special files, the most practical way is to add values to “magic-mode-alist” or “auto-mode-alist”.

magic-mode-alist for First Line

The “magic-mode-alist” is for associating first line of a file with a mode. (when the line otherwise isn't the unix shebang #!… or embedded elisp variable.) Use it like this:

;; if first line of file matches, activate nxml-mode
(add-to-list 'magic-mode-alist '("<!DOCTYPE html .+DTD XHTML .+>" . nxml-mode) )

The “magic-mode-alist” is variable. Its value is a list of pairs. ((info "(elisp) Association Lists")) The first element is a regex string, the second is a mode name (of type symbol). Emacs tries to match the first line of a file to values in “magic-mode-alist”. If there's a match, it sets the buffer to that mode.

auto-mode-alist for File Name

“auto-mode-alist” is for matching file name. Use it like this:

;; setup files ending in “.js” to open in js2-mode
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))

“auto-mode-alist” is a variable. Its value is a list of pairs. First element is a regex string. The second element is a mode name.

Note: in the elisp code above, the double backslash in the string \\.js\\' is used to escape the backslash. So, the regex engine just got \.js\'. The \. is to match a period. The \' is one of emacs special regex syntax, to match end of a string.

See also: emacs regex tutorial.

(info "(elisp) Regexp Backslash")

You can see what are the values of “magic-mode-alist” or “auto-mode-alist” by calling “describe-variable”.

Back to How to Install Emacs Packages.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs