6

Okay, this is a persistent problem for me. As I continue to use emacs more and more, I'm starting to customize it to my needs by installing the packages(and/or defining functions) and the one big problem I face when I configure a package is:- what keybinding to use for commands. I do not want to overwrite the global key bindings emacs ships with.

Right now, to check if a key-binding is being used for a command or not, I execute counsel-discbinds and enter the key-binding to see if it's already mapped to a command. If not, I proceed to map it to a command. But there is another problem, counsel-descbinds shows mapping only the current active major-mode. There have been times when I realized OH! I have overwritten another major-mode binding with this command and I had to again remap to another binding( which sucks :| ).

So, what can I do to avoid overwriting global-key-bindings. Is there something that lists all the unused key bindings that I could use for a command (which is not being used for any other major-mode). And whats a good rule of thumb whenever I'm creating new bindings for commands?

EDIT: Adding to the list of answers, I came across these two commands which I find to be very useful when you want to define key bindings:-

  • (describe-personal-keybindings) defined in bind-key (which comes with use-package). This lists all the key-bindings which are made by the enduser according to mode specific maps.
  • free-keys command defined in the package free-keys.el. This will show all the key chords which aren't bound to any command for the current buffer. You can also change the prefix key to something like C-c, to list all the unbound key chords.
  • I define a binding for every global command I use in each major mode and have my own global keymaps for that purpose. And as I don't mind about overriding bindings I never use, I have no problems defining new key bindings with the help of helm-descbinds. However I usually don't use predefined bindings, so I have to do that anyway. – bertfred Oct 19 '16 at 8:04
  • Mode-specific keybindings are, by definition, not global keybindings. Emacs has no way of knowing all the bindings in all of the packages until you load them. Due to the ability to chord keys, you'll never be able to get an exhaustive list of all possible unused keybindings, because that's an infinite set. – Dan Oct 19 '16 at 14:15
5

I find that it helps to make all your personal keybindings in a custom minor mode instead of the global map. At its most basic, that's just a mode like this:

(define-minor-mode my-keys-mode
  "Minor mode for my personal keybindings."
  :global t
  :keymap (make-sparse-keymap))

This does a few useful things:

  1. First, when you clobber a major mode's keybinding, your keybinding will take precedence, and not the other way around. This can cause problems, but at least you aren't surprised when you press a key and it doesn't do what you expect.

  2. More importantly, you aren't overwriting anything, only shadowing. If you find that you've clobbered something important, or even if you just need to check what the default binding for a key is, you can just disable my-keys-mode temporarily. (I do recommend giving my-keys-mode itself a global keybinding, as the sole exception to the above advice.)

  3. It makes it easier to revert changes. If you decide to reset a binding to default, you can just bind it to nil in your keymap, instead of having to either restart Emacs or figure out what command you overwrote.

The one big limitation of this approach is that by default there isn't really a clean way to use it for mode-specific keybindings. If you want to take a look at or use my keybindings mode, I've added support for this that alleviates most of the problems, but I still run into precedence-related issues from time to time.

0

While the practical aspects of your problem find nice solutions in the given answers. But there is an ideological point i want to address : not wanting to overwrite the default global key bindings Emacs comes with, is not a great idea.

You'll be unable to use some of the power of Emacs : both the configuration aspects and self documentation aspects if you consider the out of the box bindings to be sacred.

Make Emacs your own. If there is aa convenient binding you do not use, overwrite with another function that you do want to use. Note it down so that you remember, and remove the binding after a while if you find you don't use it as much as you planned.

  • This approach can sometimes work against you, mind. Early in my Emacs days I clobbered M-n and M-p because I didn't think I had a use for the standard bindings, and my muscle-memory for the custom bindings was rather strong before I eventually realised that I'd made a mistake. I'm all for making Emacs your own, but if you can conveniently do so without clobbering standard bindings, all the better. (Super and Hyper modifiers are pretty useful in this respect.) – phils Mar 29 '17 at 10:09
2

Another helpful shortcut if you are unsure about a specific key-binding you want to use: C-h c <key sequence> will tell you if what function is bound to a given key sequence. In addition to the answers above, this can be a good way to double-check that there isn't a binding in another mode.

  • I believe this is is already covered in @Drew's answer (number 3). – Dan Oct 19 '16 at 14:54
  • I see him mentioning describe-keymap with a different binding, the command I referenced about is describe-key-briefly bound to C-h c. – ce_nort Oct 19 '16 at 15:03
  • Just realized I forgot the c, I have edited my answer. – ce_nort Oct 19 '16 at 15:04
  • I use counsel-descbinds to get the similar functionality, except with ivy backend :) – Chakravarthy Raghunandan Oct 19 '16 at 16:33
2

I'm not sure exactly what you're asking, but maybe this will help.

  1. Command describe-keymap, bound to C-h M-k, from library help-fns+.el shows you all of the key bindings for a given keymap (whether keymap variable or the keymap itself).

  2. In Icicles, S-TAB shows you all of the keys currently available (from all accessible keymaps), together with their commands. Works for prefix keys too: C-x S-TAB shows all keys on the prefix key C-x. (Works even for menus.)

  3. Using C-h after a prefix key shows the keys bound with that prefix. For example, C-x C-h shows you all of the keys that have prefix key C-x.

4

One rule of thumb is that C-c <letter> are reserved for end users, so they should not be used by any package.

  • ^ this is what I do for about 75% of the bindings. But are you sure C-c <letter> is not used by any other major-mode? – Chakravarthy Raghunandan Oct 19 '16 at 8:07
  • 3
    You can't guarantee it, but if they do, it's a bug in the mode. See C-h i g (elisp) Key Binding Conventions – phils Oct 19 '16 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.