2

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.

Robert Zaremba
  • 8,081
  • 7
  • 47
  • 78

2 Answers2

7

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-/.)

Thomas
  • 17,016
  • 4
  • 46
  • 70
1

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?

Thomas
  • 17,016
  • 4
  • 46
  • 70
  • No, not working, the effect is the same :(. When I have empty .emacs file everything is ok, but when i bind a function to M-[ is not working. – Robert Zaremba Jul 12 '11 at 16:43
  • I slightly misread your original question. I posted another answer that should be more to the point. – Thomas Jul 12 '11 at 20:27
  • I tried this and it does not work. Using emacs 24.3.1 on Ubuntu 14.04. Any suggestions? – pranith Aug 03 '15 at 03:41
  • @sanatana It's hard to suggest anything with "it does not work" as the only information. What exactly does not work? Does Emacs insert a control sequence as described in the answer when you press `C-q PageDown`? – Thomas Aug 03 '15 at 07:18
  • @Thomas Yes, Emacs inserts the two keys '^[[6~' and '^[[5~' in the .emacs file. I also tried replacing [next] with [PageDown]. There is no change in behavior. I am using gtags which bind M-[ to find tag (reference). When I press PageUp it shows "Find tag (reference): (default ) 5~" in the mini-buffer. It is still not interpreting PageUp as one key. This is when I launch emacs as "emacs -nw". It works fine in window mode. – pranith Aug 03 '15 at 19:44
  • @sanatana Your problem lies not with Emacs but with your terminal emulator. See, e.g., the following questions: http://stackoverflow.com/q/6650074/261142 and http://stackoverflow.com/q/13212696/261142 – Thomas Aug 04 '15 at 08:16
  • @Thomas the first link points to this question itself. Was it meant for something else? The second link suggests what is being suggested here: use input-decode-map, which I tried and does not work :(. Could you please update the links? I will try to google the terminal problem in the mean while. Thanks! – pranith Aug 04 '15 at 16:41
  • @sanatana My bad, the first link was supposed to be this one: http://stackoverflow.com/q/13131367/261142 . The important part about the second link I posted is not the part about `input-decode-map` but that you have to combine this with making your terminal produce the correct escape sequences. Getting Emacs keybindings to work correctly in the terminal is basically a three-step procedure: make the terminal produce the correct escape sequences, make Emacs map escape sequence to the right key combos, and finally bind key combinations to whatever they're supposed to do in Emacs. – Thomas Aug 04 '15 at 19:24