Emacs: How to Turn Major/Minor Mode on/off?

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page is a tutorial on how to turn a emacs mode on, off, or toggle.

Turn On/Switch Major Mode

Each buffer/file is associated with one major mode.

A “major mode” is a set of features, designed for specific task. Typically, for working with programing languages, or other things such as viewing directory (dired) , viewing image files, Web Browser, etc.

To turn on a major mode, just call the associated command. Typically, if a mode is named “xyz-mode”, the function to turn it on is named “xyz” or “xyz-mode”.

To turn “off” a major mode, just switch to another major mode. There's always one major mode on.

The most basic major mode is fundamental-mode. So, in a sense if you want to “turn off” all major modes, just call fundamental-mode.

Minor Modes

Minor modes are like “preference” settings. Each minor mode changes emacs behavior in some way.

Each buffer/file can have multiple minor modes on.

Turn On/Off Minor Mode

To turn on/off a minor mode, call the associated function. Typically, if a mode is named “xyz-mode”, the function to turn it on is named “xyz” or “xyz-mode”. For example, linum-mode is a command to turn on/off linum-mode.

Minor mode's on/off command is a toggle. If it is on, then calling it again will turn it off.

Global Minor Mode vs Current Buffer

Minor mode can be on globally (that is, for every buffer), or it can be just for current buffer. This depends on the minor mode.

Some minor modes have a command for turning it on/off globally, while others do not. For example, there's linum-mode for toggling it in current buffer, and global-linum-mode for global toggle.

But there's auto-fill-mode but no “global-auto-fill-mode”. The best thing is to call describe-function and or describe-variable to see their inline docs.

If you want to turn a minor mode globally but it doesn't have a global toggle, you have to write a hook. For example, see: Emacs: Avoid Lambda in Hook

Turn On/Off Minor Mode in Emacs Init

When calling a mode function in init file, give it a argument 1 to turn on, and 0 for off, like this:

(global-linum-mode 1)

Do not use {t, nil}.

Current State of a Minor Mode

Typically, if a mode has a command named “xyz-mode”, it also has a variable of the same name “xyz-mode”. Its value is t if the mode is on, else nil. You can check this variable for the mode's on/off state.

If you want to turn on/off a mode, call the function, not set the variable. For example:

(global-linum-mode 1) ; GOOD

(setq global-linum-mode t) ; WRONG!
(setq global-linum-mode 1) ; WRONG!

If you also need to check if a mode (package) is loaded, see: Emacs Lisp: Check If a {function, variable, feature} is Defined/Loaded

Writing a Toggle Mode Command

You can write a toggle command that toggles a set of minor modes or other things such as switching between 2 fonts. See: Emacs Lisp: Writing a Toggle Command

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus