Which Function Mode

Here’s another useful feature in Emacs that lurks in the back of the cupboard, rarely seeing the light of day.

Emacs comes with a minor mode called which-function-mode that shows the current defun – Lisp lingo for a function, but Emacs-speak for anything that would count as one – in your modeline by looking at where your point is.

This mode is global and will work in most modes that implement M-x imenu support (if you have never used imenu, you should read my article on Effective Movement in Emacs.) Nevertheless, to err on the side of caution, the mode will only trigger in major modes listed in the which-func-modes variable. To add or remove items you can either use the customize interface by typing M-x customize-variable RET which-func-modes RET and alter the list there, or you can add new ones by adding a snippet like the one below to your .emacs file.

Here I permanently turn on which mode and add support for org-mode. Do note that you will have to toggle the which mode for new items to take effect. Keep that in mind and only turn on which mode after you’ve changed the list.

(require 'which-func)
(add-to-list 'which-func-modes 'org-mode)
(which-func-mode 1)

You may want to adjust the colors (faces) used in the modeline, and the easiest way to do that is to type M-x customize-group RET which-func.

This mode is a very useful (and unknown to most!) feature of Emacs, and that it will work out of the box with things like org mode is a testament to the importance of reusing existing libraries (imenu in this case) for interoperability.