GNU Emacs
Emacs
Dashboard

23.1 Major Modes

Every buffer possesses a major mode, which determines the editing behavior of Emacs while that buffer is current. The mode line normally shows the name of the current major mode, in parentheses (see The Mode Line).

The least specialized major mode is called Fundamental mode. This mode has no mode-specific redefinitions or variable settings, so that each Emacs command behaves in its most general manner, and each user option variable is in its default state.

For editing text of a specific type that Emacs knows about, such as Lisp code or English text, you typically use a more specialized major mode, such as Lisp mode or Text mode. Most major modes fall into three major groups. The first group contains modes for normal text, either plain or with mark-up. It includes Text mode, HTML mode, SGML mode, TeX mode and Outline mode. The second group contains modes for specific programming languages. These include Lisp mode (which has several variants), C mode, Fortran mode, and others. The third group consists of major modes that are not associated directly with files; they are used in buffers created for specific purposes by Emacs. Examples include Dired mode for buffers made by Dired (see Dired, the Directory Editor), Message mode for buffers made by C-x m (see Sending Mail), and Shell mode for buffers used to communicate with an inferior shell process (see Interactive Subshell).

Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer (see Choosing File Modes). You can explicitly select a new major mode by using an M-x command. Take the name of the mode and add -mode to get the name of the command to select that mode (e.g., M-x lisp-mode enters Lisp mode). Since every buffer has exactly one major mode, there is no way to “turn off” a major mode; instead you must switch to a different one.

The value of the buffer-local variable major-mode is a symbol with the same name as the major mode command (e.g., lisp-mode). This variable is set automatically; 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 this default value via the Customization interface (see Easy Customization Interface), or by adding a line like this to your init file (see The Emacs Initialization 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.

Specialized major modes often change the meanings of certain keys to do something more suitable for the mode. For instance, programming language modes bind TAB to indent the current line according to the rules of the language (see Indentation). The keys that are commonly changed are TAB, DEL, and C-j. Many modes also define special commands of their own, usually bound to key sequences whose prefix key is C-c (see Keys). Major modes can also alter user options and variables; for instance, programming language modes typically set a buffer-local value for the variable comment-start, which determines how source code comments are delimited (see Manipulating Comments).

To view the documentation for the current major mode, including a list of its key bindings, type C-h m ( describe-mode). See Other Help Commands.

Every major mode, apart from Fundamental mode, defines a mode hook, a customizable list of Lisp functions to run each time the mode is enabled in a buffer. See Hooks, for more information about hooks. Each mode hook is named after its major mode, e.g., Fortran mode has fortran-mode-hook. Furthermore, all text-based major modes run text-mode-hook, and many programming language modes 10 (including all those distributed with Emacs) run prog-mode-hook, prior to running their own mode hooks. Hook functions can look at the value of the variable major-mode to see which mode is actually being entered.

Mode hooks are commonly used to enable minor modes (see Minor Modes). For example, you can put the following lines in your init file to enable Flyspell minor mode in all text-based major modes (see Checking and Correcting Spelling), and ElDoc minor mode in Emacs Lisp mode (see Emacs Lisp Documentation Lookup):

(add-hook 'text-mode-hook 'flyspell-mode)
(add-hook 'emacs-lisp-mode-hook 'eldoc-mode)

Footnotes

(10)

More specifically, the modes which are “derived” from prog-mode (see Derived Modes in The Emacs Lisp Reference Manual).