UP | HOME

Emacs disable easy-keys minor-mode

Table of Contents

1 Description

Train yourself to use the proper Emacs movement keys.

2 News

  • 28. June 2011: I had switched around some 'b' and 'f' keys. Thanks to anonymous commenter for pointing it out.
  • 12. December 2009: Published page.

3 Description

The purpose of this is to help you learn to use the Emacs preferred keys (C-n, C-p, C-b, C-f, …).

It does this by disabling the arrow-keys, pg-up/down, home, end, delete and their ctrl'ed and meta'ed versions. When you press one of the disabled keys a message in the minibuffer will appear saying which key-binding to use instead. So if you press the left arrow key, a message in the minibuffer will say "No! use C-b instead.", and then nothing more.

4 Code Description

This is done by defining a minor-mode that redefines the disabled keys to instead print a message.

The reason a minor-mode is used is that minor-mode key-bindings override major-mode and global key-bindings.

5 Code

(defvar no-easy-keys-minor-mode-map (make-keymap) 
  "no-easy-keys-minor-mode keymap.")
(let ((f (lambda (m)
           `(lambda () (interactive) 
              (message (concat "No! use " ,m " instead."))))))
  (dolist (l '(("<left>" . "C-b") ("<right>" . "C-f") ("<up>" . "C-p")
               ("<down>" . "C-n")
               ("<C-left>" . "M-b") ("<C-right>" . "M-f") ("<C-up>" . "M-{")
               ("<C-down>" . "M-}")
               ("<M-left>" . "M-b") ("<M-right>" . "M-f") ("<M-up>" . "M-{")
               ("<M-down>" . "M-}")
               ("<delete>" . "C-d") ("<C-delete>" . "M-d")
               ("<M-delete>" . "M-d") ("<next>" . "C-v") ("<C-next>" . "M-x <")
               ("<prior>" . "M-v") ("<C-prior>" . "M-x >") 
               ("<home>" . "C-a") ("<C-home>" . "M->")
               ("<C-home>" . "M-<") ("<end>" . "C-e") ("<C-end>" . "M->")))
    (define-key no-easy-keys-minor-mode-map
      (read-kbd-macro (car l)) (funcall f (cdr l)))))
(define-minor-mode no-easy-keys-minor-mode
  "A minor mode that disables the arrow-keys, pg-up/down, delete
  and backspace."  t " no-easy-keys"
  'no-easy-keys-minor-mode-map :global t)
(no-easy-keys-minor-mode 1)

6 Installation

Copy-paste the code into your .emacs file and restart Emacs.

In order to enable or disable the mode use M-x no-easy-keys-minor-mode.

To have it be disabled by default change the last line to (no-easy-keys-minor-mode 0).

7 Comments

Date: 2012-01-29 12:48:30 CET

Author: Dan Amlund Thomsen

Org version 7.8.03 with Emacs version 23

Validate XHTML 1.0