You can select a major mode explicitly for the current buffer, but most of the time Emacs determines which mode to use based on the file name or on special text in the file.
To explicitly select a new major, you use an M-x command.
Take the name of a major mode and add -mode to get the name of
the command to select that mode. Thus, you can enter Lisp mode by
executing M-x lisp-mode.
When you visit a file, Emacs usually chooses the right major mode automatically. Normally, it makes the choice based on the file name—for example, files whose names end in ‘.c’ are normally edited in C mode—but sometimes it chooses the major mode based on the contents of the file. Here is the exact procedure:
First, Emacs checks whether the file contains a file-local variable that specifies the major mode. If so, it uses that major mode, ignoring all other criteria. See File Variables. There are several methods to specify a major mode using a file-local variable; the simplest is to put the mode name in the first nonblank line, preceded and followed by ‘-*-’. Other text may appear on the line as well. For example,
; -*-Lisp-*-
tells Emacs to use Lisp mode. Note how the semicolon is used to make Lisp treat this line as a comment. Alternatively, you could write
; -*- mode: Lisp;-*-
The latter format allows you to specify local variables as well, like this:
; -*- mode: Lisp; tab-width: 4; -*-
Second, Emacs checks whether the file's contents begin with
‘#!’. If so, that indicates that the file can serve as an
executable shell command, which works by running an interpreter named
on the file's first line (the rest of the file is used as input to the
interpreter). Therefore, Emacs tries to use the interpreter name to
choose a mode. For instance, a file that begins with
‘#!/usr/bin/perl’ is opened in Perl mode. The variable
interpreter-mode-alist specifies the correspondence between
interpreter program names and major modes.
When the first line starts with ‘#!’, you usually cannot use the ‘-*-’ feature on the first line, because the system would get confused when running the interpreter. So Emacs looks for ‘-*-’ on the second line in such files as well as on the first line. The same is true for man pages which start with the magic string ‘'\"’ to specify a list of troff preprocessors.
Third, Emacs tries to determine the major mode by looking at the
text at the start of the buffer, based on the variable
magic-mode-alist. By default, this variable is nil (an
empty list), so Emacs skips this step; however, you can customize it
in your init file (see Init File). The value should be a list of
elements of the form
(regexp . mode-function)
where regexp is a regular expression (see Regexps), and mode-function is a Lisp function that toggles a major mode. If the text at the beginning of the file matches regexp, Emacs chooses the major mode specified by mode-function.
Alternatively, an element of magic-mode-alist may have the form
(match-function . mode-function)
where match-function is a Lisp function that is called at the
beginning of the buffer; if the function returns non-nil, Emacs
set the major mode wit mode-function.
Fourth—if Emacs still hasn't found a suitable major mode—it
looks at the file's name. The correspondence between file names and
major modes is controlled by the variable auto-mode-alist. Its
value is a list in which each element has this form,
(regexp . mode-function)
or this form,
(regexp mode-function flag)
For example, one element normally found in the list has the form
("\\.c\\'" . c-mode), and it is responsible for selecting C
mode for files whose names end in .c. (Note that ‘\\’ is
needed in Lisp syntax to include a ‘\’ in the string, which must
be used to suppress the special meaning of ‘.’ in regexps.) If
the element has the form (regexp mode-function
flag) and flag is non-nil, then after calling
mode-function, Emacs discards the suffix that matched
regexp and searches the list again for another match.
On systems with case-insensitive file names, such as Microsoft
Windows, Emacs performs a single case-insensitive search through
auto-mode-alist. On other systems, Emacs normally performs a
single case-sensitive search through the alist. However, if you
change the variable auto-mode-case-fold to t, Emacs
performs a second case-insensitive search if the first search fails.
Finally, if Emacs still hasn't found a major mode to use, it
compares the text at the start of the buffer to the variable
magic-fallback-mode-alist. This variable works like
magic-mode-alist, described above, except that is consulted
only after auto-mode-alist. By default,
magic-fallback-mode-alist contains forms that check for image
files, HTML/XML/SGML files, and Postscript files.
Once a major mode is chosen, Emacs sets the value of the variable
major-mode to the symbol for that major mode (⁖,
text-mode for Text mode). This is a per-buffer variable
(see Locals); its buffer-local value is set automatically, and you
should not change it yourself.
The default value of major-mode determines the major mode to
use for files that do not specify a major mode, and for new buffers
created with C-x b. Normally, this default value is the symbol
fundamental-mode, which specifies Fundamental mode. You can
change it via the Customization interface (see Easy Customization), or by adding a line like this to your init file
(see Init File):
(setq-default major-mode 'text-mode)
If the default value of major-mode is nil, the major
mode is taken from the previously current buffer.
If you have changed the major mode of a buffer, you can return to
the major mode Emacs would have chosen automatically, by typing
M-x normal-mode. This is the same function that
find-file calls to choose the major mode. It also processes
the file's ‘-*-’ line or local variables list (if any).
See File Variables.
The commands C-x C-w and set-visited-file-name change to
a new major mode if the new file name implies a mode (see Saving).
(C-x C-s does this too, if the buffer wasn't visiting a file.)
However, this does not happen if the buffer contents specify a major
mode, and certain “special” major modes do not allow the mode to
change. You can turn off this mode-changing feature by setting
change-major-mode-with-file-name to nil.