<lurwas> lawrence: May you never run out of keybindings
The EmacsManual says that the combination of C-c followed by a plain letter, and the function keys f5 through f9 are reserved for users. That means that you can expect that no other mode ever uses these -- it does not mean that you are limited to these, however. You can of course rebind any key you want. See, for instance, Lisp:unbound.el, which finds keys not already in use.
The simplest thing is probably to use the global keymap:
(global-set-key (kbd "C-c b") 'bbdb)
If you take care when choosing what keys to rebind, these will never conflict with keybindings of modes. When there is such a conflict, however, the more specific mode keymap will take precedence, and your global keybinding will be temporarily shadowed. If you do not care about the mode’s keybinding, or want a keybinding for a specific mode only, you need to guess the mode’s keymap name and add the binding to that map:
(define-key text-mode-map (kbd "'") 'maybe-open-apostrophe)
No need to guess. If you want to find out the current active keymap in a given situation, like in the gnus group buffer, use this command:
Thanks to David Kastrup for giving it to me. --memnon
you might need to wait for the correct mode to be loaded before seting the key
(eval-after-load 'text-mode '(define-key text-mode-map (kbd "'") 'maybe-open-apostrophe))
DefaultOrCustomKeyBindings – Discussion on using Emacs vanilla or to roll your own.
Keys that you often will want to press several times consecutively should be easy to repeat (doh). Chords are ideal for repeating commands – see ChordDefined.
‘ESC-f11’[two keystrokes] is harder to repeat than
‘S-f11’[one-and-a-half keystrokes]. (Unless you are eating a donut with your left hand at the moment. A hint for those who are eating donuts, consider temporarily using the right shift-key instead… :-))
See also: DoReMi for ways to use different repetitive commands with the same arrow-key and mouse-wheel key bindings.
Commands that are somewhat destructive (such as
‘kill-buffer’) should have more difficult key bindings to avoid hitting them by mistake. (Thus binding C-z to iconify-frame probably should be considered a user-interface mistake — though to be sure it originated not with Emacs, but with Unix.).
Not a good example. There’s nothing destructive about
‘iconify-frame’. – DrewAdams
C-z is also used in CUA as undo. MS Windows users (maybe Mac users too?) expect this.
Commands that are very destructive (such as
‘delete-file’) should prompt for confirmation. Some people actually think that a user interface should not provide such things (see MeatBall:HumaneInterface, for example).
I wanted to bind M-� to � for swedish typing. Binding ü posed to be difficult, though, due to funny encoding problems in the file and stuff. The solution was:
(defun fc-kbd (str) "Private kbd, the original one breaks for M-ü." (read-kbd-macro (encode-coding-string str locale-coding-system)))
(global-set-key (fc-kbd "M-ü") (lambda () (interactive) (insert "å"))) (global-set-key (fc-kbd "M-Ü") (lambda () (interactive) (insert "Å")))
There is ‘C-c’ keybinding prefix was left for users. However, I found there are many external modes use that so not so many free keys are available for user actually. Instead of ‘C-c’ I use ‘MM-’ key prefix for my own commands. I mean something like ‘ESC ESC’ prefix or ‘ESC M-’ prefix. It is really easy to “doubleclick” on ESC by left hand and then type a key by right one. Just try and probably you will like this too.
There are some keybinding I use.
(global-set-key (kbd "\e\el") 'goto-line) ; (global-set-key (kbd "\e\eu") 'user-cvs-update) ;PCL-CVS update start (global-set-key (kbd "\e\ec") 'calendar) ;calendar
Some commands are handy but not used quite frequently. By overloading multiple commands on a single key stroke, I can still bind those commands to some keysequence still not consuming too much keysequences. Actually, this is a mimicking of textmate’s facility.
(require 'cl) (defun define-key* (keymap key def) (define-key keymap key (combine-command def (lookup-key keymap key)))) (defun combine-command (def defs) (cond ((null defs) def) ((and (listp defs) (eq 'lambda (car defs)) (= (length defs) 4) (listp (fourth defs)) (eq 'command-selector (car (fourth defs)))) (setf (cdr (fourth defs)) (cons def (cdr (fourth defs)))) defs) (t `(lambda () (interactive) (command-selector ,def ,defs))))) (defun command-selector (&rest candidates) (let ((s (loop for c in candidates for i = ?1 then (1+ i) concat (format "(%c) %s\n" i (if (symbolp c) c "<anon...>")))) (b (get-buffer-create " *Command Selection*")) c) (pop-to-buffer b) (erase-buffer) (insert s) (shrink-window-if-larger-than-buffer) (loop for c = (- (read-char "choice: ") ?1) until (and (>= c 0) (< c (length candidates))) finally do (progn (kill-buffer-and-window) (call-interactively (nth c candidates))))))
If you want to rebind all the bindings for a certain command to another command, use `substitute-key-definition.’
(substitute-key-definition 'kill-buffer 'kill-buffer-and-its-windows global-map)
This works whether or not you have customized the keybindings for the command in question, and so is more portable. It also frees you from hunting every one of the potential multiple keybindings assigned toa given command.
Starting with Emacs 22, consider using command remapping instead of
‘substitute-key-definition’. See the Elisp manual, node
‘Remapping Commands’. – DrewAdams