Emacs: How to Define Menu Key

By Xah Lee. Date: . Last updated: .

On most PC keyboards, there's the ▤ Menu key, also known as App key. This page shows you how to use that key in emacs, on linux or Mac or Windows.

pc keyboard menu key 86821
the ▤ Menu key (to the right of Alt).

Default Behavior of Menu Key

In Linux GNU emacs, by default, pressing ▤ Menu key invokes execute-extended-commandAlt+x】.

Menu key on Microsoft Windows

On Microsoft Windows, by default, it does nothing.

you can set it to do M-x, like this:

(when (string-equal system-type "windows-nt")
  (global-set-key (kbd "<apps>") 'execute-extended-command))

Menu key on Mac OS X

On Mac OS X, if you are using a PC keyboard, the ▤ Menu key will send 【Ctrl+p】 by default.

How to fix menu key on the Mac?

You can fix by:

;; make Ctrl+p send the menu/app key
(define-key key-translation-map (kbd "C-p") (kbd "<menu>"))

or

Set a command to it like this (global-set-key (kbd "C-p") 'execute-extended-command)

How to make Mac's ⌘ command or ⌥ option key to send the ▤ Menu key?

To make Mac's ⌘ command or ⌥ option key to send the ▤ Menu key, use the util Seil (aka PCKeyboardHack). See: Mac: Key Remapping and Keybinding Tools.

But it'll still send out the C-p. So you need to remap C-p described above.

Make the Menu as Leader Key

Best is to set ▤ Menu key as the lead key of key sequences for emacs's hundreds of commands. 〔►see Banish Key Chords

Example of key sequence starting with the ▤ Menu key.

;; on Linux, the menu/apps key syntax is <menu>
;; on Windows, the menu/apps key syntax is <apps>
;; make the syntax equal
(define-key key-translation-map (kbd "<apps>") (kbd "<menu>"))

(progn
  (define-prefix-command 'my-leader-key-map)

  (define-key my-leader-key-map (kbd "RET") (if (fboundp 'smex) 'smex 'execute-extended-command ))
  (define-key my-leader-key-map (kbd "<backspace>") nil)
  (define-key my-leader-key-map (kbd "<delete>") nil)
  (define-key my-leader-key-map (kbd "SPC") xah-insertion-keymap)
  (define-key my-leader-key-map (kbd "<menu>") 'exchange-point-and-mark)
  (define-key my-leader-key-map (kbd "TAB") xah-menu-tab-keymap)

  (define-key my-leader-key-map (kbd ".") xah-highlight-keymap)

  (define-key my-leader-key-map (kbd "'") 'quoted-insert)
  (define-key my-leader-key-map (kbd ",") nil)
  (define-key my-leader-key-map (kbd "-") nil)
  (define-key my-leader-key-map (kbd "/") nil)
  (define-key my-leader-key-map (kbd ";") nil)
  (define-key my-leader-key-map (kbd "=") nil)
  (define-key my-leader-key-map (kbd "[") nil)
  (define-key my-leader-key-map (kbd "\\") nil)
  (define-key my-leader-key-map (kbd "`") nil)

  (define-key my-leader-key-map (kbd "1") nil)
  (define-key my-leader-key-map (kbd "2") 'delete-window)
  (define-key my-leader-key-map (kbd "3") 'delete-other-windows)
  (define-key my-leader-key-map (kbd "4") 'split-window-below)
  (define-key my-leader-key-map (kbd "5") 'split-window-right)

  (define-key my-leader-key-map (kbd "6") nil)
  (define-key my-leader-key-map (kbd "7") 'dired-jump)
  (define-key my-leader-key-map (kbd "8") 'xah-open-file-path-under-cursor)
  (define-key my-leader-key-map (kbd "9") 'ispell-word)
  (define-key my-leader-key-map (kbd "0") nil)

  (define-key my-leader-key-map (kbd "a") 'mark-whole-buffer)
  (define-key my-leader-key-map (kbd "b") 'end-of-buffer)
  (define-key my-leader-key-map (kbd "c") xah-menu-c-keymap)
  (define-key my-leader-key-map (kbd "d") 'beginning-of-buffer)
  (define-key my-leader-key-map (kbd "f") 'xah-search-current-word)
  (define-key my-leader-key-map (kbd "g") 'isearch-forward)
  (define-key my-leader-key-map (kbd "h") 'xah-help-keymap)
  (define-key my-leader-key-map (kbd "i") xah-menu-i-keymap)
  (define-key my-leader-key-map (kbd "j") 'xah-cut-line-or-region)
  (define-key my-leader-key-map (kbd "k") 'yank)
  (define-key my-leader-key-map (kbd "l") 'recenter-top-bottom)
  (define-key my-leader-key-map (kbd "m") 'universal-argument)
  (define-key my-leader-key-map (kbd "n") xah-harmless-keymap)
  (define-key my-leader-key-map (kbd "o") nil)
  (define-key my-leader-key-map (kbd "p") 'query-replace)
  (define-key my-leader-key-map (kbd "q") 'xah-copy-line-or-region)
  (define-key my-leader-key-map (kbd "r") xah-edit-cmds-keymap)
  (define-key my-leader-key-map (kbd "s") nil)
  (define-key my-leader-key-map (kbd "t") xah-menu-t-keymap)
  (define-key my-leader-key-map (kbd "u") nil)
  (define-key my-leader-key-map (kbd "v") xah-menu-v-keymap)
  (define-key my-leader-key-map (kbd "w") xah-danger-keymap)
  (define-key my-leader-key-map (kbd "x") nil)
  (define-key my-leader-key-map (kbd "y") nil)
  (define-key my-leader-key-map (kbd "z") 'comment-dwim))

(global-set-key (kbd "<menu>") 'my-leader-key-map)

Why should you use the menu key?

Setting the ▤ Menu key for M-x is quite convenient. You'd know if you've used it on linux.

but, emacs has hundreds of commands starting with Ctrl+x. It'd be nice, if all these are just a key sequence of 3 keys. That way, you don't have to hold down a key on the corner.

So, we can use the ▤ Menu as a lead key for sequences.

But you don't have a single key for M-x anymore? what to do? The standard PC keyboard doesn't have much convenient keys. So, you could map one of the ❖ Window key to do the M-x. Else, you can just set ▤ Menu Enter ↵ or ▤ Menu Space or ▤ Menu ▤ Menu to do M-x.

How to design key sequence to replace all C-x?

you can see example design here Emacs: Xah Fly Keys

Use of Menu Key Outside of Emacs

What's the Use of the ▤ Menu/App Key?

Emacs Keybinding How-To Topic

  1. Emacs: How to Define Keys
  2. Emacs: Change Major Mode Keys
  3. Emacs: Change Minor Mode Keys
  4. Emacs: Fix Minor Mode Key Priority
  5. Emacs: Define Key Sequence
  6. Emacs: How to Bind Super Hyper Keys
  7. Emacs: How to Define Menu Key
  8. Emacs: Remapping Keys Using key-translation-map
  9. Emacs: Bind Number Pad Keys
  10. Emacs: How to Set Mouse Buttons
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs