Emacs: Minor Mode
What is Minor Mode
Minor modes are like preference settings. Each minor mode changes emacs behavior or add some features.
The behavior change may be keys, or mouse behavior, or editing behavior, or visual display, or it can be anything emacs does.
Each buffer can have more than one minor modes on.
Example of Minor Modes
display-line-numbers-mode
〔see Emacs Init: Show Line Numbers〕font-lock-mode
(syntax coloring) 〔see Elisp: Font Lock Mode〕abbrev-mode
〔see Emacs: Abbrev Mode〕delete-selection-mode
(typing overrides text selection if exist)electric-pair-mode
〔see Emacs Init: Auto Insert Closing Bracket (electric-pair-mode)〕global-auto-revert-mode
(automatically update buffer if files changed on disk by others)subword-mode
〔see Emacs: Move Cursor by camelCase, snake_case〕mouse-wheel-mode
(enable mouse wheel) 〔see Emacs Init: Mouse Wheel Config〕
Show Current Minor Modes
- Alt+x
describe-mode
- show a list of enabled minor modes of current buffer.
- minor-mode-list
- variable. Value is a list of all minor mode functions. 〔see Emacs: Show Variable Value〕
Toggle On/Off Minor Mode
Minor modes typically have a command to toggle it on/off.
For example,
global-display-line-numbers-mode
is a command to turn on/off line numbers in margin.
〔see Emacs Init: Show Line Numbers〕
Turn On/Off Minor Mode in Emacs Init
When calling a mode function in lisp init file, give it an argument:
- no arg → turn on
1
→ turn on-1
→ turn off
;; turn on (global-display-line-numbers-mode 1) ;; turn off (global-display-line-numbers-mode -1)
Global Minor Mode vs Current Buffer
- Some minor mode are global. There is no per buffer on/off. e.g.
show-paren-mode
. 〔see Emacs Init: Highlight Brackets〕 - Some minor mode are per buffer. e.g.
display-line-numbers-mode
vsglobal-display-line-numbers-mode
. 〔see Emacs Init: Show Line Numbers〕
For those per-buffer minor modes, it may not have a command to toggle on/off for all buffers.
For example, auto-fill-mode
will toggle for current buffer, but no global command.
Current State of a Minor Mode
Typically, if a mode has a command named “xx-mode”, it also has a variable of the same name “xx-mode”. Its value is t if the mode is on, else nil. You can check this variable for the mode's on/off state.
〔see Emacs: Show Variable Value〕
(If you want to turn on/off a mode, call the function, not set the variable.)
If you also need to check if a mode (package) is loaded, see: Elisp: Check If Function / Variable is Defined