Emacs modes

Author

Marie-Hélène Burle

At the core of Emacs functioning are modes. This section will explain what Emacs major and minor modes are.

You can also watch our recent webinar on this topic.

Major modes

Different types of text require different behaviours, syntax highlighting, formatting, functions, variables, etc. Consequently, each type of buffer (e.g. Python script, Markdown document, Julia REPL, Bash shell, directory editor, pdf) is associated with a different major mode.

File extensions, particular markers in the file, or other elements tell Emacs to automatically switch to the appropriate major mode.

Only one major mode is active at a time.

Switching to a different major mode is possible by running the corresponding major mode command (e.g. M-x python-mode will switch to Python mode).

Fundamental mode

fundamental-mode is the most basic major mode, with no particular feature. This is the mode enabled by default if Emacs cannot detect what specific major mode to enable.

Minor modes

Minor modes provide additional and optional features that can be turned on or off (e.g. spell checking, auto-completion, auto-indentation, fancy undo behaviour, fancy parenthesis matching highlighting).

Minor modes can be turned on/off by running the corresponding minor mode commands (e.g. M-x flyspell-mode will turn spell checking on/off).

The command consult-minor-mode-menu from the package consult makes this particularly easy (we will see in a later section how to install packages).

Each mode comes with a set of commands. consult’s command consult-mode-command makes it easy to search for commands within each mode.

Any number of minor modes can be active at the same time.

List of enabled modes

By default, C-h m or M-x describe-mode will open a list and description of the active modes.

The major mode can also be determined with C-h v major-mode (C-h v runs the command describe-variable).

Finally, a list of minor modes can be viewed with C-h v minor-mode-list.

Here too, the package consult makes this much nicer, thanks to the command consult-minor-mode-menu.

The mode line

Another way to get information about enabled modes is the mode line.

Remember that the mode line is that line near the bottom of the window with a series of information:

noshadow

Hooks

Minor modes can be automatically enabled when other modes (major or minor) are enabled thanks to hooks.

For example, to enable the aggressive indent minor mode whenever the ESS R major mode is enabled, you can add to your init file:

(add-hook 'ess-r-mode-hook 'aggressive-indent-mode)

Or, using use-package, now part of base Emacs:

(use-package aggressive-indent
    :hook (ess-r-mode . aggressive-indent-mode))

We will learn how to customize Emacs in a later section, so don’t worry if this doesn’t make much sense yet.

Modes source code

To see the source code of a mode, run C-h v (or M-x describe-variable) followed by the name of the mode map. This will open a help buffer with a link to the source code file.

For example C-h v text-mode-map will open a help buffer with a link to text-mode.el.

The help buffer opened by C-h m or M-x describe-mode also gives a link to the source code of the major mode.

Looking at the source code of a mode is very useful to customize it.

Polymode

While it is normally impossible to associate multiple major modes with a single buffer, Polymode allows to insert sections of a major mode within another major mode.

This is extremely convenient for instance to embed sections of code within human text, or even to have code executed within human text (e.g. R Markdown or its successor Quarto, Org Babel).

For example, here is the section of a markdown-mode buffer with snippets of julia-mode:

Julia has "assignment by operation" operators:

```{julia}
a = 2;
a += 7    # this is the same as a = a + 7
```

There is a *left* division operator:

```{julia}
2\8 == 8/2
```

It can be rendered by Quarto into the following webpage: