20

What are the conventions for using C-x, C-c, or M- as prefix keys?

For example: is C-x, etc., for saving or visiting buffers? Is C-c, etc., for text editing?

Background

Maybe this is a silly question. I use a Spanish keyboard, and sometimes redefine keybindings that are common for an English (QWERTY) keyboard. For example: shell command is bound in vanilla Emacs to M-!. I bind it to M-¡ instead, because M-! is M-shift-1, which I cannot press easily. I'm wondering if there are conventions that I am overriding when I make these kinds of changes

Drew
  • 75,699
  • 9
  • 109
  • 225
anquegi
  • 739
  • 5
  • 21
  • 1
    like `M-x` is for "execute extended command,This is the gateway to a lot of the sophisticated stuff you can accomplish interactively in Emacs; it allows you to run any Elisp function by name." does `C-c` and `C-x` not have a description like that? – Charlie Parker Jan 10 '19 at 03:43

2 Answers2

27

If you are talking about binding keys for your own use, then this is the rule: You can bind any keys you like.

If you want to be sure not to bind a key that might already be bound then use C-c followed by a letter. All such keys are reserved for users (see next).


If you are talking about binding keys in code (e.g., a library) that you write, for use by others, then the rules (conventions) are described here: node Key Binding Conventions of the Elisp manual.

The main rules there, related to what you ask, are those regarding C-c:

  • Don’t define C-c LETTER as a key in Lisp programs. Sequences consisting of C-c and a letter (either upper or lower case) are reserved for users; they are the only sequences reserved for users, so do not block them.

  • Sequences consisting of C-c followed by a control character or a digit are reserved for major modes.

  • Sequences consisting of C-c followed by {, }, <, >, : or ; are also reserved for major modes.

  • Sequences consisting of C-c followed by any other ASCII punctuation or symbol character are allocated for minor modes. Using them in a major mode is not absolutely prohibited, but if you do that, the major mode binding may be shadowed from time to time by minor modes.

You'll note that there is no convention regarding C-x. An unstated convention, or just good advice/practice is this one, however: If you replace some existing, standard Emacs binding (i.e., one that you get from Emacs as distributed, without loading any 3rd-party libraries) then that might confuse or annoy some users of your code.


You can get to that doc within Emacs, this way:

  1. C-h i, to open Info, the doc browser.
  2. m el TAB RET, for menu elisp (TAB to complete), or just click the Elisp link with your mouse.
  3. key bi TAB, then n TAB RET to complete to Key Binding Conventions.

Or you can use i within the Elisp manual, to complete index entries:

  1. C-h i m el TAB RET.
  2. i key bi TAB, which completes to key binding and shows you two possibilities: key binding and key binding, conventions for. Type , TAB to complete to the second, then RET to accept it.
Drew
  • 75,699
  • 9
  • 109
  • 225
  • It is better to follow the convention to bind `C-c` and letter to user commands. That way one can be (almost) sure that one does not override some other binding from a major mode already installed or to be installed some day in the future. – Tobias Jun 21 '18 at 14:00
  • 1
    @Tobias: Not sure what you're saying. Better than what? *All* of the key-binding conventions should be followed, including not binding `C-c` + LETTER in a 3rd-party library. – Drew Jun 21 '18 at 14:23
  • You correctly stated in the first sentences of your answer that there are no stringent rules for private use. Nevertheless it is advisable to follow the conventions for user keybindings for the reasons I gave in my first comment. Maybe, you could add a note about that below your *everything goes* rule in the first section. That's all I wanted to say, Drew. Best regards, – Tobias Jun 21 '18 at 15:33
  • 3
    @Tobias: I added what I think you're saying; thanks. But there are *no* conventions for a user to follow with respect to key bindings. The *"conventions for user keybindings"* that you speak of are not conventions for users. They are conventions for coding Lisp that will be used by others. (I know that you know this. Just want to be clear, for anyone else reading this.) – Drew Jun 21 '18 at 16:55
  • What's a control character? – Didier A. Nov 05 '18 at 07:56
  • like `M-x` is for "execute extended command,This is the gateway to a lot of the sophisticated stuff you can accomplish interactively in Emacs; it allows you to run any Elisp function by name." does `C-c` and `C-x` not have a description like that? – Charlie Parker Jan 10 '19 at 03:43
  • @DidierA.: See [Wikipedia - Control Character](https://en.wikipedia.org/wiki/Control_character). – Drew Feb 01 '19 at 03:36
  • @Pinocchio: Sorry, I have no idea what your comment means. The only occurrence of `M-x` on this page are from your comment, which you repeated for each answer. – Drew Feb 01 '19 at 03:38
2

@Drew's answer is much more nuanced and detailed.

This is a perfectly valid question! There is the convention that the C-x is reserved by emacs itself whereas the C-c prefix is used for user defined keybindings.

Take a look at this, especially the "Reserved keys" section. I am pretty sure that this information is stated in the emacs manual as well, but I cannot seem to find it right now. The conventions are described in this section of the emacs lisp manual.

  • 2
    `C-c` followed by a letter (lower or upper case) without further modifiers is reserved for users. Many other key sequences starting with `C-c` are reserved for major modes -- especially those where the second key-stroke is also modified by the control key. – Tobias Jun 21 '18 at 13:25
  • You are right, of course. @Drew answered this question much better. – Panagiotis Koutsourakis Jun 21 '18 at 13:55
  • like `M-x` is for "execute extended command,This is the gateway to a lot of the sophisticated stuff you can accomplish interactively in Emacs; it allows you to run any Elisp function by name." does `C-c` and `C-x` not have a description like that? – Charlie Parker Jan 10 '19 at 03:43