Emacs: How to Turn a Minor Mode on/off/toggle?

Master emacs+lisp, benefit for life. Testimonials. Thank you for support.
, , …,

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

When setting a variable that takes {true, false}, use {t, nil}, like this:

(setq make-backup-files nil)

When calling a mode function, use 1 or 0, like this:

(global-linum-mode 1)

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) ; BAD
(setq global-linum-mode 1) ; TOTALLY WRONG!

Also, some minor mode have a command for setting it globally, while others do not. For example, there's linum-mode and global-linum-mode. 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.

Emacs 23.2 Changes

There are changes since emacs 23.2. However, the advice given above applies to any emacs version.

Before emacs 23.2, when a minor mode function is called in lisp code, no argument such as (auto-fill-mode) will toggle the mode. This is consistant behavior when the function is called interactively. But after emacs 23, it'll simply turn it on.

Though, there might still be some minor mode having the old behavior.

Here's historical note about this change, by Stefan Monnier (one of the current emacs maintainer) in a post in comp.emacs on (Source groups.google.com) , this behavior may change in future emacs because it is too confusing. “t” will always turn it on, “nil” turns it off always. For toggling, i'm guessing there'll be extra function with “toggle” in the name, ⁖ toggle-read-only, toggle-case-fold-search, toggle-debug-on-error, toggle-text-mode-auto-fill, toggle-truncate-lines, …

(thanks to YOO for correction on (auto-fill-mode t).)

bug#6611

Like what you read?
Buy Xah Emacs Tutorial
or share some
blog comments powered by Disqus