Emacs Keys: Major-Mode Dependent Key

By Xah Lee. Date: . Last updated: .

You can have a key do different things depending on what's the current Major Mode .

The most simple way, is to write a wrapper command. The wrapper command checks current mode and call different commands. Then, you bind the wrapper command to a key in global map.

Here's a example of a wrapper command.

suppose you want F9 to call:

(defun my-smart-command ()
  "call different commands depending on what's current major mode."
  (interactive)
  (cond
   ((eq major-mode 'x1-mode) (x1-cmd))
   ((eq major-mode 'x2-mode) (x2-cmd))
   ;; more major-mode checking here

   ;; if nothing match, do nothing
   (t nil)))

Then, just bind this command to a key. [see Emacs Keys: Define Key]

[see Emacs: Find Major Mode Name]