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:
- indent-buffer (C-c i, after I got the answer)
- comment-or-uncomment-region (C-c C)
- rdebug (ruby debugger) (C-c R)
- rsense-complete (ruby autocomplete) (C-c e)
Where would you put these? Why?
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:
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:
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 usersense-complete
instead, but only when you're in ruby. In which case, you can do this: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.This is mine.
Notice I remapped the movement keys. This was because I use the MS NS4K, which makes those particular key movements very easy.
By default,
C-x h
marks the whole buffer, andC-M-\
runsindent-region
, so doing those two one after the other will indent the whole buffer.comment-dwim
is already bound by default toM-;
, and probably does better thancomment-or-uncomment-region
. ("DWIM" stands for "do-what-I-mean")For completion, I recommend setting up auto-complete-mode, which interoperates with rsense. Auto-complete has standard keybindings for all completion.
As for rdebug, I would probably just stick it on one of the F-keys, or else something prefixed by
C-c
, since that is the sort-of designated "custom" prefix. MaybeC-c d
for "Debug". To make sure that the key is not already bound, go to a ruby buffer and pressC-h k
and then press your key combination, and make sure that Emacs tells you that is is undefined.I generally put global keybindings in a separate file(part of my configuration) and mode specific configuration in the mode-specific configuration files. That way everything is lean, tight and easy to find. Some of the things you've mentioned like comment/uncomment already have keybindings attached. I've implemented indent-buffer with a functions indent-buffer-or-region(part of EDT) that I've bound to the standard C-M-\ indentation key. There are formal rules for what keys should be used by users for their custom keybindings and there is of course common sense. Nothing is written in stone.
Here are some examples from Emacs Prelude: