Why emacs confuse PageDown (<next>) key with M-[? - emacs

I found big distinction in standard emacs-nox and emacs-gtk.
I know that emacs console version (emacs-nox) has problem with some keys (eg Shift-Tab - ), but not with PageDown.
When I have empty .emacs file, and try to recognize command name run by PageDown key (by C-h c), emacs-nox emacs-gtk works normally - pushing PageDown makes scroll-up, and C-h c PageDown print scroll-up in minibuffer.
The problem arise when i try to bind "M-[" key.
In .emacs i has only one statement:
(global-set-key (kbd "M-[") 'hippie-expand)
emacs-nox does not recognize command name run by key - it does'nt print in minibuffer anything when C-h c PageDown, insted wriets to buffer "~6".
When I try
C-h k PageDown
I get: M-[ runs the command hippie-expand
emacs-gtk works normally - pushing PageDown makes scroll-up, and C-h c PageDown print scroll-up in minibuffer.
So I guess emacs nox treats PageDown as M-[ and add something extra.
Any idea how to fix this in emacs-nox?
I use emacs v23.2
EDIT:
I tested other case: In .emacs I have only:
(global-set-key (kbd "") 'hippie-expand)
and both C-h c PageDown and C-h k PageDown works properly (prints hippie-expand), and when in buffer I push PageDown also works good.

The problem has to do with the escape sequence the terminal sends to Emacs. You can check the escape sequence by typing C-v in a terminal window, followed by the key combination. So, for instance, if you type
C-v M-[
you should see something like this in the terminal window:
^[[
If you type
C-v PageDown
you should see
^[[6~
And that explains the problem: the key sequence generated by M-[ is a prefix of the key sequence generated by PageDown. Thus when you bind that prefix to a function (e.g., by globally setting M-[ to 'hippie-expand), you get the following effect when hitting PageDown:
The first two characters (^[[) of PageDown's escape sequence are interpreted as the prefix and thus 'hippie-expand is called. Then the remaining two characters are interpreters like ordinary key strokes, and are thus inserted into the buffer. That's why you see "6~" when you press PageDown.
I think the only way to change this is to convince the terminal to send different sequences for those keys. But the more painless way is just to use a different shortcut than M-[. (I would suggest M-/.)

This has to do with the terminal emulation and how Emacs-nox interprets the escape sequences sent to it by the terminal whenever you hit a key.
It thus depends on your terminal, but you could try to put the following lines in your .emacs file:
(unless window-system
(define-key input-decode-map "" [next])
(define-key input-decode-map "" [prior]))
Then move the cursor between the first two "" characters and type C-q PageDown, then move it between the "" in the row underneath and type C-q PageUp. The result should look like this:
(unless window-system
(define-key input-decode-map "^[[6~" [next])
(define-key input-decode-map "^[[5~" [prior]))
but note that the ^[ is only a single character (escape) - that's why you cannot simply copy & paste it from this answer.
Do the keys work after restarting emacs-nox?

Related

How do I rebind a key in Emacs globally?

I have this in my .emacsrc
(define-key global-map "\C-h" 'backward-delete-char) ;;previously help
however, when I do an I-search and hit C-h to delete a char, emacs uses the default binding and tries to open the help, when I cancel the search with C-g, it then executes the backward-delete-char in the document I started the I-search from.
EDIT: This makes me wonder why C-h is again mapped to help inside the minibuffer. What is overriding my global-map definition and why?
You can use this:
(keyboard-translate ?\C-h ?\C-?)
This will translate C-h to backspace everywhere.
When you do isearch or ido-find-file, they
override some keys in the minibuffer. You could re-override
these keys for each mode, but you'd have to really do it for each
new mode that you use. That's why I've suggested the code above:
modes will not typically rebind backspace to something that doesn't behave
like a backspace. The point is that the logical C-h is still
bound for help inside minibuffer, but with keyboard-translate you don't
have C-h anymore - you just have another backspace. And if you
want to bind some command to it, you can't bind to C-h -
you have to bind to ?\C-?.
My guess is that you don't really want to delete a char when you hit "control and h", but instead that for some reason Emacs receives C-h when you press the backspace key, right?
If so, the best solution is to figure out how to change your terminal emulator so that it doesn't send C-h to Emacs when you press backspace (e.g. it could send C-? as do most other terminal emulators nowadays).

emacs error: Key sequence M-x g starts with non-prefix key M-x

I have the following code in .emacs: (global-set-key (kbd "M-x g") 'gnus) to start Gnus with the keybinding M-x g. I obtain: error: Key sequence M-x g starts with non-prefix key M-x. How can I define keybindings starting with M-x? Is this a bad thing to do and should be avoided? I find it more intuitive since the "long version" is M-x gnus. Defining it as C-c g for example is no problem but then you start Gnus with C-c g and, for example, R via M-x R which is not very intuitive (in contrast to starting both via M-x + 1 letter
The key M-x is already bound to the command execute-extended-command, which then asks you to provide the name of a command to execute (in you case: gnus).
Since R is a command only one-character long, it looks like M-x R is a key sequence, but it's not: it's M-x followed by entering R in the minibuffer and you have to hit RET to validate your input.
In short:
you can not set key sequences beginning with M-x since this key is already bound to a command and is thus not a prefix (unlike C-c, which does nothing but wait for you to type another key, but should be reserved for bindings specific to the current modes).
the standard way to do things would be to continue starting gnus using M-x gnus or to rebind it to an entirely different key if you need to be very quick (you could for example use one of the F1-F12 keys)
if you really want to have a M-x + letter binding, you can define a one-letter alias to the command gnus, like this:
(defalias 'g 'gnus)

emacs - [control shift up] doesn't work

I tried to define hotkey as following
(global-set-key [(control shift up)] 'other-window)
but it doesn't work (no error, just doesn't work), neither does
(global-set-key [(control shift down)] 'other-window)
But
(global-set-key [(control shift right)] 'other-window)
and
(global-set-key [(control shift left)] 'other-window)
work!
But because the last two key combinations are used by emacs (as default), I don't wanna change them for other functions.
So how could I make control-shift-up and control-shift-down work?
I have googled "(control shift up)", it seems that control-shift-up is used by other people, (but not very few results).
The reason for this is not an Emacs problem, but comes from the fact that your terminal cannot produce a key sequence for C-S-up.
You can verify this very easily. Open a terminal and then type:
Control-v Control-Shift-right
The Control-v part will make the control sequence for the next key be inserted verbatim into your shell. In our case, it will insert the sequence for Control-Shift right, and that'll look something like this:
^[[1;6C
Now, try the same thing for C-S-up:
Control-v Control-Shift-up
You'll see that no control sequence is entered, which hints at the fact that when you press C-S-up in Emacs, it will actually never receive anything, because the terminal is not able to produce anything to pass on to Emacs.
We can double-verify this if you just start a new emacs -nw and type C-h k to invoke Emacs' describe-key function. You'll get asked in the minibuffer to type a key to see what function it is bound to. If you now type C-S-up nothing happens - of course not, since the terminal in which your Emacs runs doesn't produce anything.
However, if you're just looking for an unused key-combination, how about just Shift-up (or even Shift-right) without Control? That one should work both in a terminal emacs and in the windowed version.
Finally, with the help from grawity on superuser.com, I got it working. Please this thread
https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup
This could well be a duplicate of:
Binding M-<up> / M-<down> in emacs 23.1.1
If this is the case, Gilles' answer should sort you out (undoubtedly with different escape sequences, though.)
edit (for better visibility -- see answer below):
It turned out that gnome terminal does not transmit these key combinations, so the solution relies upon the following: https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:
(global-set-key (kbd "M-h") 'backward-kill-word)
(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)
I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character?
Best.
(I looked for libraries that may have overrided this command but I cannot find them)
On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.
If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.
This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.
There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is
(global-set-key [?\e delete] 'backward-kill-word)
The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following
C-h k M-<\delete> (to use your syntax)
or
M-x describe-key M-<\delete>
Emacs (for me) responds with:
M-DEL (translated from <M-delete>)
runs the command backward-kill-word,
which is an interactive compiled Lisp
function in `simple.el'.
It is bound to , M-DEL.
(backward-kill-word arg)
....
Which you can see shows that the representation for the key you want is M-DEL or M-delete.
Which is a long way of getting to the point that what you want is
(global-set-key (kbd "M-delete") 'backward-kill-word)
Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).
You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.
After not being able to find the library holding the conflict I found this webpage
http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding
Changing Key Bindings Interactively...
`M-x global-set-key KEY CMD '
Define KEY globally to run CMD....
Normally, C-z' is bound to the function
suspend-emacs' (when not using the X Window System), but you can
change C-z' to invoke an interactive subshell within Emacs, by binding
it toshell' as follows:
M-x global-set-key <RET> C-z shell <RET>
`global-set-key' reads the command name after the key. After you
press the key, a message like this appears so that you can confirm that
you are binding the key you want:
Set key C-z to command:...
And now the standard default is returned to by doing
M-x global-set-key M-del ...
backward-kill-word
But this is transient and must be done on each reload, any way to make this permanent?
Putting a command into the init.el is not overriding the other effect

basic shortcut (M-del) not working in Emacs and giving erratic behavior

I am using Emacs and most shortcuts work normally, but M-Del for deleting a word backwards produces either an error at the bottom of a `scan' error, and at other times moves the cursor a set of lines below. Any ideas why this may be happening? M-Del works fine for deleting forward words. (** from a comment made below it appear that the command is mapped to a down paragraph lisp function instead of delete a word backwards? How can I reset the mappings to the standard one?)
Best.
writing lisp emacs key binding and cannot specify the <delete> character
has the answer (by Gilles). It looks like there is a bug on some systems due to an overlap with a shell command shadow translating ESC-x to ESC C-d
it can be seen from running
M-x load-library edmacro
M-x edmacro-insert-key M-del
giving ESC C-d
in the folder ~/.emacs.d/ creating a file init.el and inserting
(global-set-key [escape delete] 'backward-kill-word)
this though overrides all uses of ESC from M (meta key) to be translated as escape rendering common M-d, M-w, etc all unseen except for M-del
so the solution is to remap the faulty remapping back to the correct binding.
(global-set-key (kbd "M-C-d") 'backward-kill-word)
Best