How to determine whether command should be control or meta? - emacs

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.

Related

Emacs: relation between keystrokes and keys

I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.

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 preserve keybindings when using multi-term.el in line mode?

In emacs (ver. 24.3), I have my forward-paragraph and backward-paragraph mapped to M-p and M-n instead of M-{ and M-}. It is easier for me to remember and use fluidly with C-p and C-n. I've recently started using multi-term to run most of my terminal work. However, when I switch to line-mode my custom bindings for forward and backward paragraph no longer work. It says 'empty input ring'. Oddly when I'm in char-mode, the C-p and C-n do what they are supposed to do (bringing up previous prompt entries), but my paragraph movements work.
So in short, my custom forward and backward paragraph bindings work in char-mode (where I don't really need them), but not in line-mode. Any ideas?
See term-bind-key-alist, which includes C-p, C-n, M-p, and M-n by default. See also these passages from the EmacsWiki page MultiTerm. The second especially seems relevant to your problem. These do not mention term-line-mode or term-char-mode, but I think they might give you a place to start.
Note 1
‘term-unbind-key-list’ is a list of keys which emacs keeps for itself. By default it contains (“C-z” “C-c” “C-x” “C-h” “C-y” “”)
‘term-bind-key-alist’ is a list of keys and functions which you can use, for example to use Emacs style cursor movement to the multi-terminal. The default is long, so I’ll let you look it up yourself.
Note 2
Because C-r is default keystroke for isearch-backward, for avoid conflict with C-r, i binding M-r to send “C-r” character to shell.
You can use option term-bind-key-alist/term-unbind-key-list to binding/unbinding special keystroke in multi-term.el, and don’t use term-mode-hook. ☺ –- AndyStewart
Also, I don't see term-line-mode anywhere in multi-term.el. It looks as if it makes use only of term-char-mode. See, for instance, multi-term-keystroke-setup.

Find a good hotkey for new emacs commands

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)])

emacs key "deletechar"?

which is emacs key <deletechar>?
Is there any emacs command to check specified character name?
There are two keys mapped to delete-char functions: Delete, and <deletechar>.
Delete key I have remapped, so <deletechar> left, but I can't figure out which one it is.
In Emacs, "delete" and "deletechar" are not specific keyboard keys, they are mapped to specific "key sequences". If you look at the content of the local-function-key-map variable, you may see that "deletechar" is mapped to other "key sequences". Looking at the content of the function-key-map variable will show you what terminal device mapping is in place. The variables key-translation-map and input-decode-map may also have an entry for "deletechar".
If you use both terminal and windowed versions of emacs or you use different terminal types or if you use emacs on different hardware platforms or if you use different versions of emacs, you may find that pressing a keyboard key sometimes generates a "delete" key sequence and sometimes it will generate a "deletechar" key sequence depending on what the specific key sequence mappings are. Therefore, if you have set "delete" to execute one function and "deletechar" to execute another function, the actual function that is executed may vary depending on whether you are running on terminal or windowed emacs, the terminal type, and the version of emacs.
For more info, have a look at the Keymaps for Translating Sequences of Events section of the Emacs manual and the links off the Keymaps section of the Emacs manual.