Editing Modes
*************

Emacs contains many editing modes, each of which alters its basic behavior in useful ways.  These are divided into major modes and minor modes.

Major modes provide specialized facilities for working on a particular file type, such as a C source file (see Programs), or a particular type of non-file buffer, such as a shell buffer (see Shell).  Major modes are mutually exclusive; each buffer has one and only one major mode at any time.

Minor modes are optional features which you can turn on or off, not necessarily specific to a type of file or buffer.  For example, Auto Fill mode is a minor mode in which SPC breaks lines between words as you type (see Auto Fill).  Minor modes are independent of one another, and of the selected major mode.

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 Mode Line.

Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer.  See Choosing 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.  Thus, you can enter Lisp mode with M-x lisp-mode.

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.  Such major modes change the meanings of some keys to become more specifically adapted to the language being edited.  The ones that are commonly changed are TAB, DEL, and C-j.  The prefix key C-c normally contains mode-specific commands.  In addition, the commands which handle comments use the mode to determine how comments are to be delimited.  Many major modes redefine the syntactical properties of characters appearing in the buffer.

The 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 remaining major modes are not intended for use on users' files; they are used in buffers created for specific purposes by Emacs, such as Dired mode for buffers made by Dired (see Dired), Message mode for buffers made by C-x m (see Sending Mail), and Shell mode for buffers used for communicating with an inferior shell process (see Interactive Shell).

Most programming-language major modes specify that only blank lines separate paragraphs.  This is to make the paragraph commands useful. (see Paragraphs.)  They also cause Auto Fill mode to use the definition of TAB to indent the new lines it creates.  This is because most lines in a program are usually indented (see Indentation).

Minor Modes
===========

A minor mode is an optional editing modes that alters the behavior of Emacs in some well-defined way.  Unlike major modes, any number of minor modes can be in effect at any time.  Some minor modes are buffer-local: they apply only to the current buffer, so you can enable the mode in certain buffers and not others.  Other minor modes are global: while enabled, they affect everything you do in the Emacs session, in all buffers.  Some global minor modes are enabled by default.

Most minor modes say in the mode line when they are enabled, just after the major mode indicator.  For example, Fill in the mode line means that Auto Fill mode is enabled.  See Mode Line.

Each minor mode is associated with a command, called the mode command, which turns it on or off.  The name of this command consists of the name of the minor mode, followed by -mode; for instance, the mode command for Auto Fill mode is auto-fill-mode.  Calling the minor mode command with no prefix argument toggles the mode, turning it on if it was off, and off if it was on.  A positive argument always turns the mode on, and a zero or negative argument always turns it off.  Mode commands are usually invoked with M-x, but you can bind keys to them if you wish (see Key Bindings).

Most minor modes also have a mode variable, with the same name as the mode command.  Its value is non-nil if the mode is enabled, and nil if it is disabled.  In some minor modes---but not all---the value of the variable alone determines whether the mode is active: the mode command works simply by setting the variable, and changing the value of the variable has the same effect as calling the mode command.  Because not all minor modes work this way, we recommend that you avoid changing the mode variables directly; use the mode commands instead.

The following is a list of some buffer-local minor modes:

  * Abbrev mode automatically expands text based on pre-defined abbreviation definitions.  See Abbrevs.

  * Auto Fill mode inserts newlines as you type to prevent lines from becoming too long.  See Filling.

  * Auto Save mode saves the buffer contents periodically to reduce the amount of work you can lose in case of a crash.  See Auto Save.

  * Enriched mode enables editing and saving of formatted text. See Formatted Text.

  * Flyspell mode automatically highlights misspelled words. See Spelling.

  * Font-Lock mode automatically highlights certain textual units found in programs.  It is enabled globally by default, but you can disable it in individual buffers.  See Faces.

  * Linum mode displays each line's line number in the window's left margin.  Its mode command is linum-mode.

  * Outline minor mode provides similar facilities to the major mode called Outline mode.  See Outline Mode.

  * Overwrite mode causes ordinary printing characters to replace existing text instead of shoving it to the right.  For example, if point is in front of the B in FOOBAR, then in Overwrite mode typing a G changes it to FOOGAR, instead of producing FOOGBAR as usual.  In Overwrite mode, the command C-q inserts the next character whatever it may be, even if it is a digit---this gives you a way to insert a character instead of replacing an existing character.  The mode command, overwrite-mode, is bound to the Insert key.

  * Binary Overwrite mode is a variant of Overwrite mode for editing binary files; it treats newlines and tabs like other characters, so that they overwrite other characters and can be overwritten by them. In Binary Overwrite mode, digits after C-q specify an octal character code, as usual.

  * Visual Line mode performs ``word wrapping'', causing long lines to be wrapped at word boundaries.  See Visual Line Mode.

Here are some useful global minor modes.  Since Line Number mode and Transient Mark mode can be enabled or disabled just by setting the value of the minor mode variable, you can set them differently for particular buffers, by explicitly making the corresponding variable local in those buffers.  See Locals.

  * Column Number mode enables display of the current column number in the mode line.  See Mode Line.

  * Delete Selection mode causes text insertion to first delete the text in the region, if the region is active.  See Using Region.

  * Icomplete mode displays an indication of available completions when you are in the minibuffer and completion is active.  See Completion Options.

  * Line Number mode enables display of the current line number in the mode line.  It is enabled by default.  See Mode Line.

  * Menu Bar mode gives each frame a menu bar.  It is enabled by default. See Menu Bars.

  * Scroll Bar mode gives each window a scroll bar.  It is enabled by default, but the scroll bar is only displayed on graphical terminals. See Scroll Bars.

  * Tool Bar mode gives each frame a tool bar.  It is enabled by default, but the tool bar is only displayed on graphical terminals.  See Tool Bars.

  * Transient Mark mode highlights the region, and makes many Emacs commands operate on the region when the mark is active.  It is enabled by default.  See Mark.

Choosing File Modes
===================

When you visit a file, Emacs chooses a 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 special text in the file. This special text can also be used to enable buffer-local minor modes.

Here is the exact procedure:

First, Emacs checks whether the file contains file-local mode variables.  See File Variables.  If there is a file-local variable that specifies a major mode, then Emacs uses that major mode, ignoring all other criteria.  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; -*-

If a file variable specifies a buffer-local minor mode, Emacs enables that minor mode in the buffer.

Second, if there is no file variable specifying a major mode, 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 GNU/Linux and other systems with case-sensitive file names, Emacs performs a case-sensitive search through auto-mode-alist; if this search fails, it performs a second case-insensitive search through the alist.  To suppress the second search, change the variable auto-mode-case-fold to nil.  On systems with case-insensitive file names, such as Microsoft Windows, Emacs performs a single case-insensitive search through auto-mode-alist.

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 (e.g., 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.