Emacs: how to choose good custom key bindings?

10

4

I'm an Emacs beginner and I'd like to start adding some global custom key bindings for common tasks like replace-string, replace-regex, etc. that don't have them by default. I want to make good choices, and I see a few issues:

  • See all my options - How can I see the full range of keys that are unbound across all the modes that I regularly use?

  • Compatibility/convention - Are there common unofficial bindings or a common "user-space" key with which to begin personal custom bindings?

  • Other issues - Anything else I should be taking into consideration?

Ian Mackinnon

Posted 2010-11-23T19:11:08.263

Reputation: 3 919

Answers

9

Try taking a look at this question on SO: Are there any emacs key combinations reserved for custom commands?

Regarding seeing what is available, there's the unbound library (see the first answer to the above question).

One convention (grabbed from my answer to that question) is:

You can redefine anything, but the convention is to use C-c l (where l is any letter).

As a user, you can redefine any key; but it is usually best to stick to key sequences that consist of C-c followed by a letter (upper or lower case). These keys are "reserved for users," so they won't conflict with any properly designed Emacs extension. The function keys F5 through F9 are also reserved for users. If you redefine some other key, your definition may be overridden by certain extensions or major modes which redefine the same key.

Just remember that key bindings are different in each of the various (major and minor) modes, so what is available in one buffer isn't necessarily available in another. And make your key bindings at the most localized level possible. For example, if the functionality does something to a line of code, don't make a global key binding for it, define it in the appropriate keymaps (c++-mode-map, ruby-mode-map, etc.).

As far as your specific tasks, have you noticed:

  • M-% aka query-replace
  • C-M-% aka query-replace-regexp

Also, when you're searching (C-s or C-M-s (and the reverse variants)), you can type M-% (or C-M-%) to directly enter query-replace (query-replace-regexp) with the search string as the thing to substitute... Pretty handy.

Trey Jackson

Posted 2010-11-23T19:11:08.263

Reputation: 3 731