Emacs seems to have all the possible keyboard combinations already randomly distributed among it's commands. :p
If I am to define new keyboard shortcuts, where should I put them? Which prefixes should I use?
For instance: I want to define shortcuts for the following functions:
Where would you put these? Why?
To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.
C- means "control key", Hold down the control key while. pressing the next key. M- means "meta key", (“Escape” in the lab, on other computers sometimes “Alt”).
I have an unconventional approach to this that I recommend highly. I have redefined the C-l ('ell') key to be a prefix key, and I use that to prefix my favorite commands. This key is very easy to type and it is bound to a function ('recenter) that isn't used that much.
Well, I don't use 'recenter much, but even if you did, it can be assigned to C-l C-l which is almost as easy to type, and a small price to pay for the possibilities opened up by the Ctrl-L-map . (Actually I prefer 'redraw-display to 'recenter, so I gave that the place of honor.)
I don't remember where I got the magic incantation that makes it work:
(global-unset-key "\C-l")
(defvar ctl-l-map (make-keymap)
"Keymap for local bindings and functions, prefixed by (^L)")
(define-key global-map "\C-l" 'Control-L-prefix)
(fset 'Control-L-prefix ctl-l-map)
Then you can define keys to your heart's content. C-l is a great place to put bindings for your own commands, as well as functions that are not bound to keys, or are bound to keys you can't remember or find hard to type. Here are some sample bindings to standard functions:
(define-key ctl-l-map "\C-l" 'redraw-display)
(define-key ctl-l-map "l" 'recenter)
(define-key ctl-l-map "g" 'goto-line)
(define-key ctl-l-map "r" 'replace-string)
(define-key ctl-l-map "R" 'replace-regexp)
(define-key ctl-l-map "q" 'query-replace)
(define-key ctl-l-map "Q" 'query-replace-regexp)
(define-key ctl-l-map "o" 'overwrite-mode)
(define-key ctl-l-map "\C-w" 'kill-rectangle)
(define-key ctl-l-map "\C-y" 'yank-rectangle)
(define-key ctl-l-map "h" 'command-history)
(define-key ctl-l-map "w" 'write-region)
(define-key ctl-l-map "r" 'electric-replace-string)
(define-key ctl-l-map "\er" 'replace-string)
(define-key ctl-l-map "T" 'delete-trailing-whitespace)
(define-key ctl-l-map "C" 'describe-char)
(define-key ctl-l-map "W" 'ediff-regions-wordwise)
(define-key ctl-l-map "L" 'ediff-regions-linewise)
(define-key ctl-l-map "\C-f" 'facemenu-remove-all)
(define-key ctl-l-map "\C-c" 'calendar)
(define-key ctl-l-map "m" 'not-modified) ;; already at M-~
(define-key ctl-l-map "\C-q" 'fill-region)
(define-key ctl-l-map "u" 'set-buffer-file-coding-system)
(define-key ctl-l-map [?\C-2] 'transient-mark-mode)
(define-key ctl-l-map "\C-@" 'transient-mark-mode)
(define-key ctl-l-map "\C-n" 'linum-mode)
(define-key ctl-l-map "\C-s" 'isearch-forward-regexp)
(define-key ctl-l-map "\C-r" 'isearch-backward-regexp)a
(define-key ctl-l-map "U" 'browse-url)
(define-key ctl-l-map "F" 'browse-url-of-file)
(define-key ctl-l-map "\C-u" 'undo-only)
Emacs actually has a very definite pattern to its bindings, this answer shows some.
As far as where you should define keys, if you take a look at the documentation for conventions, you'll see that C-c a (where a is any lower or upper case character) is reserved for users.
Plus, if you're defining a key that really only makes sense in a particular mode, then you should define it in that keymap.
For example, M-/ is bound to dabbrev-expand
, which is a handy way of autocompleting the word you're typing. It might very well make sense to use rsense-complete
instead, but only when you're in ruby. In which case, you can do this:
(add-hook 'ruby-mode-hook
(lambda () (define-key ruby-mode-map (kbd "M-/") 'rsense-complete)))
That will override the binding for M-/ only when you're in ruby-mode
, and leave it unchanged (or available) for the rest of the modes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With