Emacs: Mode-Dependent Key Shortcut

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 will check 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."
   ((string-equal major-mode "x1-mode") (x1-cmd))
   ((string-equal 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: How to Define Keybinding]

[see Emacs: Find Major Mode Name]

Emacs Keys



Key Tips

Emacs Pinky