How to get Emacs key bindings to recognize capital letters? - emacs

How do I get Emacs to recognize M-C (meta-shift-c) rather than translating it as m-c?
Currently when I do C-h k M-C I get:
M-c (translated from M-C) runs the command capitalize-word.
How do I get it to NOT translate M-C to M-c?

Just use modifier S (Shift) in your key binding. E.g.:
(global-set-key "\M-\S-c" 'foobar)
If there is nothing explicitly bound to the shifted key then Emacs automatically translates it to the unshifted key.
From the Elisp manual, node Key Sequence Input:
If an input character is upper-case (or has the shift modifier) and has no key binding, but its lower-case equivalent has one, then read-key-sequence' converts the character to lower case. Note thatlookup-key' does not perform case conversion in this way.

To get Emacs to distinguish the two is very easy: add a binding for M-C. The translation from M-C to M-c happens as a fallback when no binding was found for M-C.

Related

FInding key bindings referenced as a text string surrounded by angle brackets

Quoting from the help buffer for the function kill-line:
It is bound to <deleteline>.
The question is how to find out exactly what key sequence <deleteline> refers to?
The subject of the question is angle-bracket notation for 'pseudo keys'.
The describe-bindings command can be used to find the function that a pseudo key is bound to (if any) by the looks...
10.8 Other Help Commands ... C-h b displays a list of all the key bindings now in effect: first the
local bindings of the current minor modes, then the local bindings
defined by the current major mode, and finally the global bindings
where-is can be used to find the key, etc., that a function is bound to:
10.2 Documentation for a Key ... C-h w command RET lists the keys that are bound to command. It displays the list in the echo area. If it
says the command is not on any key, that means you must use M-x to run
it. C-h w runs the command where-is.

Find out corresponding escape sequence for a given key combo

In Emacs, I want to bind a particular key combination to a command. However, because I am using Emacs in terminal mode within iTerm2 on OS X, I need to translate the key combo to character escape sequence and register that sequence with iTerm2 so that it will recognize the key combination.
But how do I find out the corresponding sequence given a key combination? For example, I found that something like ^[[1;8A corresponds to Ctrl+Alt+up (where I have configured Alt to function as +Esc in iTerm2), but I have no idea how that key combination translates into this particular sequence.
Is there a way to look up or work out the escape sequence for any given key combo? For example, what is the sequence for Ctrl+Alt+r?
A related question, can someone explain to me the relationship between setting up a key combo with its corresponding sequence in iTerm2 and making Emacs translate a sequence into its internal key representation using input-decode-map inside .emacs (e.g. (define-key input-decode-map "[escape_sequencehere]" [internal_key_representation_here])? It seems to me that setting it up in iTerm2 alone is sufficient to make the binding work in Emacs, so when and why do we need to set up the latter in .emacs? (and perhaps when do we need both to make something work?)
Some of the key combinations are sent directly to the application running in iTerm2. You can get it
by using ctrl+v approach (see In bash, how do I bind a function key to a command?) or
bash-3.2$ ^[[1;10A (in this case I hit ctrl + v before shift+alt+up)
sed -n l (note it's lowercase L). For instance, you can see what the application receives if you send (shift+alt+up):
bash-3.2$ sed -n l
^[[1;10A
\033[1;10A$
If some key combination does not get through, you can instruct iTerm2 to let it pass and map it to a key combination that you can detect as described above. For instance, you can send through and map ctrl+alt+cmd+R to ESC+sdf and your terminal will receive ^[sdf which you can assign to a command in Emacs.
In your terminal type showkey -a and let the key-strokes fly.

Are RET and C-m really the same exactly?

In the graphical mode, RET does the same as C-m, namely newline.
However, when I use M-a or M-e, they seem to distinguish between them. The newline produced by RET, they skip all to a double-RET point, and with the newline produced by C-m, they move the cursor line by line. Why so?
Description (C-h k) for both keys shows the same.
If it matters, it is Emacs23.3.1 on Ubuntu 12.04.
The answer could be dependent on the mode you're in as well as in the key codes generated by your operating system. When I hit Ctrl-h k Ctrl-m in the *scratch* buffer on my Windows box, I'll get the info for (newline) with the information that it is bound to RET. Theoretically, in some other mode, C-m and RET could be set to different functions. This requires, of course, that the OS doesn't generate the same key code.
What Emacs writes as RET (or as <RET>) is, yes, the same character as C-m - it is the ASCII control character 13. This is so regardless of what keyboard etc. you have.
However, the logical key (aka "key sequence") that Emacs sees when you hit the phisical (keyboard) key named Enter or Return might be different from RET (aka C-m). To see what key Emacs recognizes when you hit that keyboard key, use C-h k and hit the key. If Emacs says RET then this is the typical case, and yes, hitting that key is then identical to pressing Control and hitting m (assuming the key labeled Control does what it does typically).
In the graphical mode, the answer is yes (unless keys are redefined, of course).
Careful examination of my file showed that the effect I observed was a coincidence. It was exactly in those paragraphs which I C-m'ed that all lines would end in a dot (it was a poem).
Actually, C-h k key description says the same thing for both keys. Thanks to schaueho's answer for pushing me in this direction.

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.

emacs: remapping C-[ (currently ESC)

I'm using emacs 23 on ubuntu 10.04. I would like to remap the "C-[" binding. Currently, when I press it, it gives me "ESC". Is there a way to remap it?
This is not an Emacs binding, but has global effect in X and it's used as handier substitute for ESC for people practicing touch typing. I guess you'll have to modify the X keymap if you want to use it in another fashion, but I wouldn't advice that.
C-[ is the ASCII ESCAPE character, which Emacs usually writes as ESC.
There is no key involved here (it's a character), and there is no key binding involved here. Or rather, your keyboard's Escape key sends the control character ESCAPE (also written C-[ and ESC).