Find a good hotkey for new emacs commands - emacs

In Emacs everything is about hotkeys. Everybody who creates new shortcuts knows the problem:
Which hotkeys should be used?
Which hotkeys are used by the most popular Emacs extensions (org-mode...) and should be avoided?
Are there reserved hotkeys for "users" that will never be used by extensions?
Which hotkeys should be avoided, if the code should be public? (Some keys like right/left Win are sometimes missing on keyboards, M-TAB will be catched by the windowmanager)
Is there a list of all reserved hotkeys?

Sticking to the reserved C-c <letter> sequences is as close as you'll get to guaranteeing that you won't conflict with any other code (although it's still just a convention; sometimes you'll see people making code available that uses one of those sequences, but you can report those cases as bugs to the author).
I would suggest using some of the C-c <letter> sequences as prefix bindings for grouping related functionality together. For instance, you might use C-c w <key> as the pattern for window-related functionality. That gives you a dramatically larger number of reserved bindings, probably with better mnemonic properties, and of course the subsequent <key> can be anything at all, not just a letter.
That also lets you use C-c <letter> C-h to list everything you've bound under that prefix, which can be convenient.

The GNU Emacs Lisp Reference manual has a detailed page on key binding conventions:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html

The keys C-c <letter> are reserved for the users, so packages should normally not use them (note that "letter" is really a letter of the alphabet, not "any key" or "any character"). But most users just use which ever key they like and when that binding conflicts with some package, they handle the conflict in an ad-hoc way (either by using another key, or by specifically overriding the package's binding).

automate key-binding checks evaluating forms like
(key-binding [(control c) (delete)])

Related

Some shortcuts don't work on emacs beginning with C-c

In Haskell-mode, the shortcut C-c C-= is defined to do something.
Trying this shortcut, I realized that emacs do not recognize the shortcut C-c C-=.
Indeed, when I try the shortcut on emacs, the buffer write C-c = is not defined although I pressed C- C-=. I have the same problem with some other symbols like '.' or '§'. But shortcuts like C-c C-l or C-c C-c work.
I try to remove my .emacs but I have the same problem.
a friend have the same problem as me.
Both we are on ArchLinux (64 bits) and we use emacs in console. The keyboard is an azerty.
The problem come from emacs ? Arch Linux ?
Your terminal can't send Emacs C-= so you can't use that key sequence. (Emacs would recognise it if it received it, but that won't happen.)
Your options are:
Run GUI Emacs.
Use M-x name-of-command RET (for whatever command is bound to the key sequence you're not able to use). Use C-hm to see the major mode's bindings, or C-hb to see all current bindings, in order to learn what those command names are.
Create new custom keybindings for the commands in question (i.e. bindings which your terminal can send to Emacs).
Find a different terminal emulator with enhanced key sequence abilities. The vast majority of them will be no better than what you have, because they're all adhering to the limitations of the terminals they're emulating. The most capable one I know of is http://invisible-island.net/xterm/xterm.html but you may need to compile it yourself, and then expect to spend lots of time configuring it. (It's not a trivial solution, though, and xterm requires a GUI environment, so running GUI Emacs is much simpler.)
Use C-x#c<key> instead of C-<key>.
With that last option, you can use a sequence your terminal can send to fake a sequence that it can't send.
C-cC-= would become C-cC-x#c=
If you really wanted to use that last option, you can set a custom binding to simplify the sequence (may be necessary in some instances to avoid conflicting with existing sequences). See the end of https://stackoverflow.com/a/24804434/324105 for more information.
To add to phils' post - another option is an Emacs package (which I wrote), which can teach Emacs and terminals how to properly recognize all PC keyboard keys and modifier key combinations:
https://github.com/CyberShadow/term-keys
The default configuration already includes encoding key combinations like Ctrl=, as well as similar variations.

How to determine whether command should be control or meta?

Is there a set of criteria to determine whether a command should be a ctrl keybinding or a meta keybinding?
For example, file handling commands seem to fall under C-x bindings. Cursor movements are a mix of ctrl and meta. I don't notice much of a pattern other than built-in bindings leaning towards ctrl which suggets meta may be somewhat reserved for user bindings.
I'm not aware of a fixed set of criteria to distinguish between commands bound to Ctrl or Meta. However, the Emacs Lisp manual has a section on Key Binding Conventions that should be interesting to you.
Meta is not officially reserved for user-defined bindings:
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, [...].
If you're asking because you want to set up custom key bindings for commands that don't have any by default, I suggest you also take a look at Emacs: How to choose good custom key bindings? and Are there any emacs key combinations reserved for custom commands?
Additionally, when binding keys be aware that
A <Control>-modified alphabetical character is always considered case-insensitive: Emacs always treats C-A as C-a, C-B as C-b, and so forth. [...]
For all other modifiers, you can make the modified alphabetical characters case-sensitive when you customize Emacs. For instance, you could make M-a and M-A run different commands.
... as described here.

keybindings for a single character in emacs

I would like to use "Ctrl+'" to replace "\" (i.e., the backslash key). How should I do that? Originally I did it by kbd macro, but it is still different. For example, for the package smartparens, if one types "\{", a "\}" will be automatically typed. However, keyboard macro "ctrl+'"+"{" is not the same as "\{" for this case.
So I am just wondering if there are other ways achieving this? Some global keybinding tool (works for the whole X system) is also ok. But so far I cannot find one that can do such keybinding (two keys for one key).
Thanks!

How to remap C-c?

Given that there are so many major modes which define their own maps for this prefix (e.g. latex, org, term), is there a safe way to move these maps somewhere else, and always have the C-c key free?
I'm hoping there's a convenient way, because I noticed ErgoEmacs does exactly this.
As far as i know, there no easy way, unless you dive deep into emacs innards and create ways to remap every mode's keys on load. (I think Matthew Fidler is actually working on this)
ergoemacs-mode does not remap the C-c. It turns on cua-mode, but cua-mode doesn't remap C-c neither, it just create several clever ways so that key can be used for multiple purposes (e.g. by the speed you press the key, or whether there's a text selection, etc.). All C-c * keys are still there.
see discussion on this same question here
https://plus.google.com/103652929131043355278/posts/Nb4xn4gDB6p

How to remap keys when in Emacs?

I am looking for a way to have my control key and caps-lock key switched when I am in the emacs window, but when I go to any other program such as Firefox I want to be able to use the normal control button for new windows, tabs and such.
Additional Information: I am using Ubuntu, but answers for all systems are good since someone else may want to know how to do it on their system.
Emacs never actually sees the CapsLock keycode (under X anyway), so you can't remap the key in Lisp. Take a look at this page for details:
http://www.emacswiki.org/emacs/MovingTheCtrlKey
On windows using Auto Hot Key you can achieve this with this piece of code put in default script:
#IfWinActive, emacs#
CapsLock::Control
#IfWinActive
Auto Hot Key is a very handy utility and it is also free software like in GPL.
For globally remapping keys there is xmodmap. xkeycaps is an interactive tool. You can put control on both keys.
Since you want to remap the keys only for Emacs (which I find somewhat ill-advised) you need to look inside Emacs for a solution. It should be possible, because you can rebind all keys, but it may be too tedious to swap all keys around if the modifiers cannot be swapped.
Personally I map CapsLock to Compose, to type all kinds of funny characters like ä, ß, å, þ, «, —, ...